debian-mirror-gitlab/lib/gitlab/background_migration.rb

118 lines
3.5 KiB
Ruby
Raw Normal View History

2018-12-13 13:39:08 +05:30
# frozen_string_literal: true
2017-09-10 17:25:29 +05:30
module Gitlab
module BackgroundMigration
def self.queue
@queue ||= BackgroundMigrationWorker.sidekiq_options['queue']
end
# Begins stealing jobs from the background migrations queue, blocking the
# caller until all jobs have been completed.
#
2020-11-24 15:15:51 +05:30
# When a migration raises a StandardError it is going to retry up to
2017-09-10 17:25:29 +05:30
# three times, for example, to recover from a deadlock.
#
# When Exception is being raised, it enqueues the migration again, and
# re-raises the exception.
#
# steal_class - The name of the class for which to steal jobs.
2019-03-02 22:35:43 +05:30
def self.steal(steal_class, retry_dead_jobs: false)
queues = [
Sidekiq::ScheduledSet.new,
Sidekiq::Queue.new(self.queue)
]
if retry_dead_jobs
queues << Sidekiq::RetrySet.new
queues << Sidekiq::DeadSet.new
end
2017-09-10 17:25:29 +05:30
2019-03-02 22:35:43 +05:30
queues.each do |queue|
2017-09-10 17:25:29 +05:30
queue.each do |job|
migration_class, migration_args = job.args
next unless job.queue == self.queue
next unless migration_class == steal_class
2020-07-28 23:09:34 +05:30
next if block_given? && !(yield migration_args)
2017-09-10 17:25:29 +05:30
begin
perform(migration_class, migration_args) if job.delete
rescue Exception # rubocop:disable Lint/RescueException
BackgroundMigrationWorker # enqueue this migration again
.perform_async(migration_class, migration_args)
raise
end
end
end
end
##
# Performs a background migration.
#
# 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.
def self.perform(class_name, arguments)
2018-11-18 11:00:15 +05:30
migration_class_for(class_name).new.perform(*arguments)
end
2020-03-13 15:44:24 +05:30
def self.remaining
scheduled = Sidekiq::ScheduledSet.new.count do |job|
job.queue == self.queue
end
scheduled + Sidekiq::Queue.new(self.queue).size
end
2019-07-31 22:56:46 +05:30
def self.exists?(migration_class, additional_queues = [])
2019-03-02 22:35:43 +05:30
enqueued = Sidekiq::Queue.new(self.queue)
scheduled = Sidekiq::ScheduledSet.new
2019-07-31 22:56:46 +05:30
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-03-13 15:44:24 +05:30
# 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-12-21 20:55:43 +05:30
const_get(class_name, false)
2019-07-31 22:56:46 +05:30
end
def self.enqueued_job?(queues, migration_class)
queues.each do |queue|
2019-03-02 22:35:43 +05:30
queue.each do |job|
return true if job.queue == self.queue && job.args.first == migration_class
end
end
false
end
2017-09-10 17:25:29 +05:30
end
end