2018-10-22 07:00:50 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-10-20 07:33:44 +00:00
|
|
|
module Gitlab
|
2016-10-27 11:34:09 +00:00
|
|
|
module OptimisticLocking
|
2017-02-23 10:44:49 +00:00
|
|
|
module_function
|
2016-10-27 11:34:09 +00:00
|
|
|
|
|
|
|
def retry_lock(subject, retries = 100, &block)
|
2018-04-18 09:19:40 +00:00
|
|
|
ActiveRecord::Base.transaction do
|
|
|
|
yield(subject)
|
2016-10-20 07:33:44 +00:00
|
|
|
end
|
2018-04-18 09:19:40 +00:00
|
|
|
rescue ActiveRecord::StaleObjectError
|
|
|
|
retries -= 1
|
|
|
|
raise unless retries >= 0
|
|
|
|
|
2019-04-08 13:33:36 +00:00
|
|
|
subject.reset
|
2018-04-18 09:19:40 +00:00
|
|
|
retry
|
2016-10-20 07:33:44 +00:00
|
|
|
end
|
2017-02-23 10:44:49 +00:00
|
|
|
|
2017-02-23 13:56:27 +00:00
|
|
|
alias_method :retry_optimistic_lock, :retry_lock
|
2016-10-20 07:33:44 +00:00
|
|
|
end
|
|
|
|
end
|