2016-10-20 03:33:44 -04:00
|
|
|
module Gitlab
|
2016-10-27 07:34:09 -04:00
|
|
|
module OptimisticLocking
|
|
|
|
extend self
|
|
|
|
|
|
|
|
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
|
2016-10-21 09:12:11 -04:00
|
|
|
return block.call(subject)
|
|
|
|
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
|
|
|
|
end
|
|
|
|
end
|