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)
|
2018-04-18 05:19:40 -04:00
|
|
|
ActiveRecord::Base.transaction do
|
|
|
|
yield(subject)
|
2016-10-20 03:33:44 -04:00
|
|
|
end
|
2018-04-18 05:19:40 -04:00
|
|
|
rescue ActiveRecord::StaleObjectError
|
|
|
|
retries -= 1
|
|
|
|
raise unless retries >= 0
|
|
|
|
|
|
|
|
subject.reload
|
|
|
|
retry
|
2016-10-20 03:33:44 -04:00
|
|
|
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
|