2016-10-20 03:33:44 -04:00
|
|
|
module Gitlab
|
2016-10-27 07:34:09 -04:00
|
|
|
module OptimisticLocking
|
2017-02-23 05:44:49 -05:00
|
|
|
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
|
2017-02-23 05:44:49 -05:00
|
|
|
|
2017-02-23 08:56:27 -05:00
|
|
|
alias_method :retry_optimistic_lock, :retry_lock
|
2016-10-20 03:33:44 -04:00
|
|
|
end
|
|
|
|
end
|