Commit 2a0ead2c authored by Grzegorz Bizon's avatar Grzegorz Bizon

Implement draining scheduled sets of background migrations

parent 388abbd1
module Gitlab module Gitlab
module BackgroundMigration module BackgroundMigration
def self.queue def self.queue
BackgroundMigrationWorker.sidekiq_options['queue'] @queue ||= BackgroundMigrationWorker.sidekiq_options['queue']
end end
# Begins stealing jobs from the background migrations queue, blocking the # Begins stealing jobs from the background migrations queue, blocking the
...@@ -9,11 +9,14 @@ module Gitlab ...@@ -9,11 +9,14 @@ module Gitlab
# #
# steal_class - The name of the class for which to steal jobs. # steal_class - The name of the class for which to steal jobs.
def self.steal(steal_class) def self.steal(steal_class)
queue = Sidekiq::Queue.new(self.queue) enqueued = Sidekiq::Queue.new(self.queue)
scheduled = Sidekiq::ScheduledSet.new
[scheduled, enqueued].each do |queue|
queue.each do |job| queue.each do |job|
migration_class, migration_args = job.args migration_class, migration_args = job.args
next unless job.queue == self.queue
next unless migration_class == steal_class next unless migration_class == steal_class
perform(migration_class, migration_args) perform(migration_class, migration_args)
...@@ -21,6 +24,7 @@ module Gitlab ...@@ -21,6 +24,7 @@ module Gitlab
job.delete job.delete
end end
end end
end
# class_name - The name of the background migration class as defined in the # class_name - The name of the background migration class as defined in the
# Gitlab::BackgroundMigration namespace. # Gitlab::BackgroundMigration namespace.
...@@ -28,6 +32,7 @@ module Gitlab ...@@ -28,6 +32,7 @@ module Gitlab
# arguments - The arguments to pass to the background migration's "perform" # arguments - The arguments to pass to the background migration's "perform"
# method. # method.
def self.perform(class_name, arguments) def self.perform(class_name, arguments)
puts class_name
const_get(class_name).new.perform(*arguments) const_get(class_name).new.perform(*arguments)
end end
end end
......
...@@ -34,6 +34,20 @@ describe Gitlab::BackgroundMigration do ...@@ -34,6 +34,20 @@ describe Gitlab::BackgroundMigration do
described_class.steal('Bar') described_class.steal('Bar')
end end
end end
context 'when there are scheduled jobs present', :sidekiq, :redis do
it 'steals all jobs from the schedule sets' do
Sidekiq::Testing.disable! do
BackgroundMigrationWorker.perform_in(10.minutes, 'Object')
expect(Sidekiq::ScheduledSet.new).to be_one
expect(described_class).to receive(:perform).with('Object', any_args)
described_class.steal('Object')
expect(Sidekiq::ScheduledSet.new).to be_none
end
end
end
end end
describe '.perform' do describe '.perform' do
......
...@@ -8,4 +8,8 @@ RSpec.configure do |config| ...@@ -8,4 +8,8 @@ RSpec.configure do |config|
config.after(:each, :sidekiq) do config.after(:each, :sidekiq) do
Sidekiq::Worker.clear_all Sidekiq::Worker.clear_all
end end
config.after(:each, :sidekiq, :redis) do
Sidekiq.redis { |redis| redis.flushdb }
end
end end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment