Avoid race condition when stealing a background migration
We first pop a job from the Sidekiq queue / scheduled set and only if this has been successfully deleted we process the job. This makes it possible to minimize a possibility of a race condition happening.
This commit is contained in:
parent
beffbc8aa2
commit
39b96f02dc
|
@ -19,9 +19,7 @@ module Gitlab
|
||||||
next unless job.queue == self.queue
|
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) if job.delete
|
||||||
|
|
||||||
job.delete
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -23,13 +23,21 @@ describe Gitlab::BackgroundMigration do
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'steals jobs from a queue' do
|
it 'steals jobs from a queue' do
|
||||||
expect(queue[0]).to receive(:delete)
|
expect(queue[0]).to receive(:delete).and_return(true)
|
||||||
|
|
||||||
expect(described_class).to receive(:perform).with('Foo', [10, 20])
|
expect(described_class).to receive(:perform).with('Foo', [10, 20])
|
||||||
|
|
||||||
described_class.steal('Foo')
|
described_class.steal('Foo')
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it 'does not steal job that has already been taken' do
|
||||||
|
expect(queue[0]).to receive(:delete).and_return(false)
|
||||||
|
|
||||||
|
expect(described_class).not_to receive(:perform).with('Foo', [10, 20])
|
||||||
|
|
||||||
|
described_class.steal('Foo')
|
||||||
|
end
|
||||||
|
|
||||||
it 'does not steal jobs for a different migration' do
|
it 'does not steal jobs for a different migration' do
|
||||||
expect(described_class).not_to receive(:perform)
|
expect(described_class).not_to receive(:perform)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue