Class: Gitlab::Cleanup::RemoteObjectStorage

Inherits:
Object
  • Object
show all
Includes:
ObjectStorage::FogHelpers
Defined in:
lib/gitlab/cleanup/remote_object_storage.rb

Constant Summary collapse

BATCH_SIZE =
100

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ObjectStorage::FogHelpers

#available?

Constructor Details

#initialize(storage_location_identifier, model_class, logger: nil) ⇒ RemoteObjectStorage

Returns a new instance of RemoteObjectStorage.



12
13
14
15
16
# File 'lib/gitlab/cleanup/remote_object_storage.rb', line 12

def initialize(storage_location_identifier, model_class, logger: nil)
  @storage_location_identifier = storage_location_identifier
  @model_class = model_class
  @logger = logger || Gitlab::AppLogger
end

Instance Attribute Details

#loggerObject (readonly)

Returns the value of attribute logger.



8
9
10
# File 'lib/gitlab/cleanup/remote_object_storage.rb', line 8

def logger
  @logger
end

#model_classObject (readonly)

Returns the value of attribute model_class.



8
9
10
# File 'lib/gitlab/cleanup/remote_object_storage.rb', line 8

def model_class
  @model_class
end

#storage_location_identifierObject (readonly)

Returns the value of attribute storage_location_identifier.



8
9
10
# File 'lib/gitlab/cleanup/remote_object_storage.rb', line 8

def storage_location_identifier
  @storage_location_identifier
end

Instance Method Details

#run!(dry_run: true, delete: false, batch_size: BATCH_SIZE) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/gitlab/cleanup/remote_object_storage.rb', line 18

def run!(dry_run: true, delete: false, batch_size: BATCH_SIZE)
  unless object_store.enabled
    logger.warn Rainbow("Object storage not enabled for #{storage_location_identifier}. Skipping.").yellow
    return
  end

  if bucket_prefix.present?
    error_message = "#{storage_location_identifier} is configured with a bucket prefix '#{bucket_prefix}'.\n"
    error_message += "Unfortunately, prefixes are not supported for this Rake task.\n"
    # At the moment, Fog does not provide a cloud-agnostic way of iterating through a bucket with a prefix.
    logger.error Rainbow(error_message).red
    return
  end

  action = delete ? 'delete' : 'move to lost and found'
  dry_run_suffix = dry_run ? '. Dry run' : ''
  logger.info "Looking for orphaned remote #{storage_location_identifier} files to #{action}#{dry_run_suffix}..."

  each_orphan_file(batch_size) do |file|
    handle_orphan_file(file, dry_run: dry_run, delete: delete)
  end
end