Class: I18n::Backend::Weeler::Lock

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/i18n/backend/weeler/lock.rb

Class Method Summary collapse

Class Method Details

.acquire(name) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/i18n/backend/weeler/lock.rb', line 28

def self.acquire(name)
  already_acquired = definitely_acquired?(name)

  if already_acquired
    yield
  else
    begin
      create(:name => name) unless find_by(name: name)
    rescue ActiveRecord::StatementInvalid
      # concurrent create is okay
    end

    begin
      result = nil
      transaction do
        self.lock(true).find_by(name: name) # this is the call that will block
        acquired_lock(name)
        result = yield
      end

      result
    ensure
      maybe_released_lock(name)
    end
  end
end

.definitely_acquired?(name) ⇒ Boolean

if true, the lock is acquired if false, the lock might still be acquired, because we were in another db transaction

Returns:

  • (Boolean)


57
58
59
# File 'lib/i18n/backend/weeler/lock.rb', line 57

def self.definitely_acquired?(name)
  !!Thread.current[:definitely_acquired_locks] and Thread.current[:definitely_acquired_locks].has_key?(name)
end