BigW Consortium Gitlab

optimistic_locking.rb 458 Bytes
Newer Older
1
module Gitlab
2
  module OptimisticLocking
3
    module_function
4 5

    def retry_lock(subject, retries = 100, &block)
6
      loop do
7
        begin
8
          ActiveRecord::Base.transaction do
9
            return yield(subject)
10 11
          end
        rescue ActiveRecord::StaleObjectError
12 13
          retries -= 1
          raise unless retries >= 0
14

15 16 17 18
          subject.reload
        end
      end
    end
19

20
    alias_method :retry_optimistic_lock, :retry_lock
21 22
  end
end