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

Constants included from WorkerAttributes

WorkerAttributes::NAMESPACE_WEIGHTS, 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


65
66
67
68
69
# File 'app/workers/object_storage/migrate_uploads_worker.rb', line 65

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:


75
76
77
78
79
80
81
82
83
84
85
# File 'app/workers/object_storage/migrate_uploads_worker.rb', line 75

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


113
114
115
116
117
118
119
120
121
# File 'app/workers/object_storage/migrate_uploads_worker.rb', line 113

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


123
124
125
# File 'app/workers/object_storage/migrate_uploads_worker.rb', line 123

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

#migrate(uploads) ⇒ Object


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

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

#perform(*args) ⇒ Object

rubocop: disable CodeReuse/ActiveRecord


88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'app/workers/object_storage/migrate_uploads_worker.rb', line 88

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


131
132
133
134
135
136
137
# File 'app/workers/object_storage/migrate_uploads_worker.rb', line 131

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

#sanity_check!(uploads) ⇒ Object

rubocop: enable CodeReuse/ActiveRecord


109
110
111
# File 'app/workers/object_storage/migrate_uploads_worker.rb', line 109

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