BigW Consortium Gitlab

background_migration.rb 1.67 KB
Newer Older
1 2
module Gitlab
  module BackgroundMigration
3
    def self.queue
4
      @queue ||= BackgroundMigrationWorker.sidekiq_options['queue']
5 6
    end

7 8 9
    # Begins stealing jobs from the background migrations queue, blocking the
    # caller until all jobs have been completed.
    #
10 11 12 13 14 15
    # When a migration raises a StandardError is is going to be retries up to
    # three times, for example, to recover from a deadlock.
    #
    # When Exception is being raised, it enqueues the migration again, and
    # re-raises the exception.
    #
16 17
    # steal_class - The name of the class for which to steal jobs.
    def self.steal(steal_class)
18 19
      enqueued = Sidekiq::Queue.new(self.queue)
      scheduled = Sidekiq::ScheduledSet.new
20

21 22 23
      [scheduled, enqueued].each do |queue|
        queue.each do |job|
          migration_class, migration_args = job.args
24

25 26
          next unless job.queue == self.queue
          next unless migration_class == steal_class
27

28
          begin
29
            perform(migration_class, migration_args) if job.delete
30
          rescue Exception # rubocop:disable Lint/RescueException
31 32 33 34
            BackgroundMigrationWorker # enqueue this migration again
              .perform_async(migration_class, migration_args)

            raise
35
          end
36
        end
37 38 39
      end
    end

40
    ##
41
    # Performs a background migration.
42
    #
43 44 45 46 47
    # 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.
48
    def self.perform(class_name, arguments)
49 50 51 52
      const_get(class_name).new.perform(*arguments)
    end
  end
end