2018-10-22 03:00:50 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-06-02 11:12:36 -04:00
|
|
|
module Gitlab
|
|
|
|
module BackgroundMigration
|
2017-07-11 09:42:00 -04:00
|
|
|
def self.queue
|
2017-07-13 05:44:52 -04:00
|
|
|
@queue ||= BackgroundMigrationWorker.sidekiq_options['queue']
|
2017-07-11 09:42:00 -04:00
|
|
|
end
|
|
|
|
|
2017-06-02 11:12:36 -04:00
|
|
|
# Begins stealing jobs from the background migrations queue, blocking the
|
|
|
|
# caller until all jobs have been completed.
|
|
|
|
#
|
2020-08-27 11:10:21 -04:00
|
|
|
# When a migration raises a StandardError it is going to retry up to
|
2017-07-17 04:16:42 -04:00
|
|
|
# three times, for example, to recover from a deadlock.
|
|
|
|
#
|
|
|
|
# When Exception is being raised, it enqueues the migration again, and
|
|
|
|
# re-raises the exception.
|
|
|
|
#
|
2017-06-02 11:12:36 -04:00
|
|
|
# steal_class - The name of the class for which to steal jobs.
|
2019-01-30 04:03:23 -05:00
|
|
|
def self.steal(steal_class, retry_dead_jobs: false)
|
|
|
|
queues = [
|
|
|
|
Sidekiq::ScheduledSet.new,
|
|
|
|
Sidekiq::Queue.new(self.queue)
|
|
|
|
]
|
2017-06-02 11:12:36 -04:00
|
|
|
|
2019-01-30 04:03:23 -05:00
|
|
|
if retry_dead_jobs
|
|
|
|
queues << Sidekiq::RetrySet.new
|
|
|
|
queues << Sidekiq::DeadSet.new
|
|
|
|
end
|
|
|
|
|
|
|
|
queues.each do |queue|
|
2017-07-13 05:44:52 -04:00
|
|
|
queue.each do |job|
|
|
|
|
migration_class, migration_args = job.args
|
2017-06-02 11:12:36 -04:00
|
|
|
|
2021-08-11 20:10:37 -04:00
|
|
|
next unless job.klass == 'BackgroundMigrationWorker'
|
2017-07-13 05:44:52 -04:00
|
|
|
next unless migration_class == steal_class
|
2021-01-19 19:10:39 -05:00
|
|
|
next if block_given? && !(yield job)
|
2017-06-02 11:12:36 -04:00
|
|
|
|
2017-07-14 09:40:51 -04:00
|
|
|
begin
|
2017-07-18 04:43:12 -04:00
|
|
|
perform(migration_class, migration_args) if job.delete
|
2017-07-17 08:02:12 -04:00
|
|
|
rescue Exception # rubocop:disable Lint/RescueException
|
2017-07-17 04:16:42 -04:00
|
|
|
BackgroundMigrationWorker # enqueue this migration again
|
|
|
|
.perform_async(migration_class, migration_args)
|
|
|
|
|
|
|
|
raise
|
2017-07-14 09:40:51 -04:00
|
|
|
end
|
2017-07-13 05:44:52 -04:00
|
|
|
end
|
2017-06-02 11:12:36 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-07-17 04:16:42 -04:00
|
|
|
##
|
2017-07-17 08:02:12 -04:00
|
|
|
# Performs a background migration.
|
2017-07-17 04:16:42 -04:00
|
|
|
#
|
2017-06-02 11:12:36 -04:00
|
|
|
# class_name - The name of the background migration class as defined in the
|
|
|
|
# Gitlab::BackgroundMigration namespace.
|
|
|
|
#
|
|
|
|
# arguments - The arguments to pass to the background migration's "perform"
|
|
|
|
# method.
|
2017-07-17 08:02:12 -04:00
|
|
|
def self.perform(class_name, arguments)
|
2018-07-19 11:16:47 -04:00
|
|
|
migration_class_for(class_name).new.perform(*arguments)
|
|
|
|
end
|
|
|
|
|
2020-02-12 10:09:37 -05:00
|
|
|
def self.remaining
|
2021-08-11 20:10:37 -04:00
|
|
|
enqueued = Sidekiq::Queue.new(self.queue)
|
|
|
|
scheduled = Sidekiq::ScheduledSet.new
|
2020-02-12 10:09:37 -05:00
|
|
|
|
2021-08-11 20:10:37 -04:00
|
|
|
[enqueued, scheduled].sum do |set|
|
|
|
|
set.count do |job|
|
|
|
|
job.klass == 'BackgroundMigrationWorker'
|
|
|
|
end
|
|
|
|
end
|
2020-02-12 10:09:37 -05:00
|
|
|
end
|
|
|
|
|
2019-04-08 18:47:23 -04:00
|
|
|
def self.exists?(migration_class, additional_queues = [])
|
2018-12-19 03:34:34 -05:00
|
|
|
enqueued = Sidekiq::Queue.new(self.queue)
|
|
|
|
scheduled = Sidekiq::ScheduledSet.new
|
|
|
|
|
2019-04-08 18:47:23 -04:00
|
|
|
enqueued_job?([enqueued, scheduled], migration_class)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.dead_jobs?(migration_class)
|
|
|
|
dead_set = Sidekiq::DeadSet.new
|
|
|
|
|
|
|
|
enqueued_job?([dead_set], migration_class)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.retrying_jobs?(migration_class)
|
|
|
|
retry_set = Sidekiq::RetrySet.new
|
|
|
|
|
|
|
|
enqueued_job?([retry_set], migration_class)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.migration_class_for(class_name)
|
2020-01-10 16:07:33 -05:00
|
|
|
# We don't pass class name with Gitlab::BackgroundMigration:: prefix anymore
|
|
|
|
# but some jobs could be already spawned so we need to have some backward compatibility period.
|
|
|
|
# Can be removed since 13.x
|
|
|
|
full_class_name_prefix_regexp = /\A(::)?Gitlab::BackgroundMigration::/
|
|
|
|
|
|
|
|
if class_name.match(full_class_name_prefix_regexp)
|
|
|
|
Gitlab::ErrorTracking.track_and_raise_for_dev_exception(
|
|
|
|
StandardError.new("Full class name is used"),
|
|
|
|
class_name: class_name
|
|
|
|
)
|
|
|
|
|
|
|
|
class_name = class_name.sub(full_class_name_prefix_regexp, '')
|
|
|
|
end
|
|
|
|
|
2019-10-15 23:06:12 -04:00
|
|
|
const_get(class_name, false)
|
2019-04-08 18:47:23 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.enqueued_job?(queues, migration_class)
|
2021-08-11 20:10:37 -04:00
|
|
|
queues.any? do |queue|
|
|
|
|
queue.any? do |job|
|
|
|
|
job.klass == 'BackgroundMigrationWorker' && job.args.first == migration_class
|
2018-12-19 03:34:34 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-06-02 11:12:36 -04:00
|
|
|
end
|
|
|
|
end
|