gitlab-org--gitlab-foss/lib/gitlab/optimistic_locking.rb

22 lines
457 B
Ruby
Raw Normal View History

2016-10-20 03:33:44 -04:00
module Gitlab
2016-10-27 07:34:09 -04:00
module OptimisticLocking
module_function
2016-10-27 07:34:09 -04:00
def retry_lock(subject, retries = 100, &block)
2016-10-21 09:12:11 -04:00
loop do
2016-10-20 03:33:44 -04:00
begin
2016-10-27 07:34:09 -04:00
ActiveRecord::Base.transaction do
2017-02-21 18:59:42 -05:00
return yield(subject)
2016-10-21 09:12:11 -04:00
end
rescue ActiveRecord::StaleObjectError
2016-10-27 07:34:09 -04:00
retries -= 1
raise unless retries >= 0
2016-10-20 03:33:44 -04:00
subject.reload
end
end
end
alias_method :retry_optimistic_lock, :retry_lock
2016-10-20 03:33:44 -04:00
end
end