Commit 2822526e authored by Kamil Trzcinski's avatar Kamil Trzcinski

Make retry_lock to not be infinite

parent 39c17ccb
...@@ -31,8 +31,8 @@ module Ci ...@@ -31,8 +31,8 @@ module Ci
if HasStatus::COMPLETED_STATUSES.include?(current_status) if HasStatus::COMPLETED_STATUSES.include?(current_status)
created_builds_in_stage(index).select do |build| created_builds_in_stage(index).select do |build|
Gitlab::OptimisticLocking.retry_lock(build) do |build| Gitlab::OptimisticLocking.retry_lock(build) do |subject|
process_build(build, current_status) process_build(subject, current_status)
end end
end end
end end
......
module Gitlab module Gitlab
class OptimisticLocking module OptimisticLocking
def self.retry_lock(subject, &block) extend self
def retry_lock(subject, retries = 100, &block)
loop do loop do
begin begin
subject.transaction do ActiveRecord::Base.transaction do
return block.call(subject) return block.call(subject)
end end
rescue ActiveRecord::StaleObjectError rescue ActiveRecord::StaleObjectError
retries -= 1
raise unless retries >= 0
subject.reload subject.reload
end end
end end
......
...@@ -24,5 +24,16 @@ describe Gitlab::OptimisticLocking, lib: true do ...@@ -24,5 +24,16 @@ describe Gitlab::OptimisticLocking, lib: true do
subject.drop subject.drop
end end
end end
it 'raises exception when too many retries' do
expect(pipeline).to receive(:drop).twice.and_call_original
expect do
described_class.retry_lock(pipeline, 1) do |subject|
subject.lock_version = 100
subject.drop
end
end.to raise_error(ActiveRecord::StaleObjectError)
end
end 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