Class: ObjectStorage::MigrateUploadsWorker

Inherits:
Object
  • Object
show all
Includes:
ApplicationWorker, Report, ObjectStorageQueue
Defined in:
app/workers/object_storage/migrate_uploads_worker.rb

Defined Under Namespace

Modules: Report Classes: MigrationResult

Constant Summary collapse

SanityCheckError =
Class.new(StandardError)

Constants included from ApplicationWorker

ApplicationWorker::LOGGING_EXTRA_KEY, ApplicationWorker::SAFE_PUSH_BULK_LIMIT

Constants included from WorkerAttributes

WorkerAttributes::DEFAULT_DATA_CONSISTENCY, WorkerAttributes::NAMESPACE_WEIGHTS, WorkerAttributes::VALID_DATA_CONSISTENCIES, WorkerAttributes::VALID_RESOURCE_BOUNDARIES, WorkerAttributes::VALID_URGENCIES

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Report

#failures, #header, #report!

Methods included from Gitlab::SidekiqVersioning::Worker

#job_version

Methods included from WorkerContext

#with_context

Class Method Details

.enqueue!(uploads, model_class, mounted_as, to_store) ⇒ Object

rubocop: disable CodeReuse/ActiveRecord


70
71
72
73
74
# File 'app/workers/object_storage/migrate_uploads_worker.rb', line 70

def self.enqueue!(uploads, model_class, mounted_as, to_store)
  sanity_check!(uploads, model_class, mounted_as)

  perform_async(uploads.ids, model_class.to_s, mounted_as, to_store)
end

.sanity_check!(uploads, model_class, mounted_as) ⇒ Object

We need to be sure all the uploads are for the same uploader and model type and that the mount point exists if provided.

Raises:


80
81
82
83
84
85
86
87
88
89
90
# File 'app/workers/object_storage/migrate_uploads_worker.rb', line 80

def self.sanity_check!(uploads, model_class, mounted_as)
  upload = uploads.first
  uploader_class = upload.uploader.constantize
  uploader_types = uploads.map(&:uploader).uniq
  model_types = uploads.map(&:model_type).uniq
  model_has_mount = mounted_as.nil? || model_class.uploaders[mounted_as] == uploader_class

  raise(SanityCheckError, _("Multiple uploaders found: %{uploader_types}") % { uploader_types: uploader_types }) unless uploader_types.count == 1
  raise(SanityCheckError, _("Multiple model types found: %{model_types}") % { model_types: model_types }) unless model_types.count == 1
  raise(SanityCheckError, _("Mount point %{mounted_as} not found in %{model_class}.") % { mounted_as: mounted_as, model_class: model_class }) unless model_has_mount
end

Instance Method Details

#args_check!(args) ⇒ Object


118
119
120
121
122
123
124
125
126
# File 'app/workers/object_storage/migrate_uploads_worker.rb', line 118

def args_check!(args)
  return if args.count == 4

  case args.count
  when 3 then raise SanityCheckError, _("Job is missing the `model_type` argument.")
  else
    raise SanityCheckError, _("Job has wrong arguments format.")
  end
end

#build_uploaders(uploads) ⇒ Object


128
129
130
# File 'app/workers/object_storage/migrate_uploads_worker.rb', line 128

def build_uploaders(uploads)
  uploads.map { |upload| upload.retrieve_uploader(@mounted_as) }
end

#migrate(uploads) ⇒ Object


132
133
134
# File 'app/workers/object_storage/migrate_uploads_worker.rb', line 132

def migrate(uploads)
  build_uploaders(uploads).map(&method(:process_uploader))
end

#perform(*args) ⇒ Object

rubocop: disable CodeReuse/ActiveRecord


93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'app/workers/object_storage/migrate_uploads_worker.rb', line 93

def perform(*args)
  args_check!(args)

  (ids, model_type, mounted_as, to_store) = args

  @model_class = model_type.constantize
  @mounted_as = mounted_as&.to_sym
  @to_store = to_store

  uploads = Upload.preload(:model).where(id: ids)

  sanity_check!(uploads)
  results = migrate(uploads)

  report!(results)
rescue SanityCheckError => e
  # do not retry: the job is insane
  Gitlab::AppLogger.warn "#{self.class}: Sanity check error (#{e.message})"
end

#process_uploader(uploader) ⇒ Object


136
137
138
139
140
141
142
# File 'app/workers/object_storage/migrate_uploads_worker.rb', line 136

def process_uploader(uploader)
  MigrationResult.new(uploader.upload).tap do |result|
    uploader.migrate!(@to_store)
  rescue StandardError => e
    result.error = e
  end
end

#sanity_check!(uploads) ⇒ Object

rubocop: enable CodeReuse/ActiveRecord


114
115
116
# File 'app/workers/object_storage/migrate_uploads_worker.rb', line 114

def sanity_check!(uploads)
  self.class.sanity_check!(uploads, @model_class, @mounted_as)
end