summaryrefslogtreecommitdiff
path: root/lib/gitlab/optimistic_locking.rb
blob: 879d46446b398e83eb0ea8374970e97cd3313562 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
module Gitlab
  module OptimisticLocking
    extend self

    def retry_lock(subject, retries = 100, &block)
      loop do
        begin
          ActiveRecord::Base.transaction do
            return block.call(subject)
          end
        rescue ActiveRecord::StaleObjectError
          retries -= 1
          raise unless retries >= 0
          subject.reload
        end
      end
    end
  end
end