class I18n::Backend::Weeler::Lock

Public Class Methods

acquire(name) { || ... } click to toggle source
# 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) click to toggle source

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

# 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

Private Class Methods

acquired_lock(name) click to toggle source
# File lib/i18n/backend/weeler/lock.rb, line 61
def self.acquired_lock(name)
  logger.debug("Acquired lock '#{name}'")
  Thread.current[:definitely_acquired_locks] ||= {}
  Thread.current[:definitely_acquired_locks][name] = true
end
maybe_released_lock(name) click to toggle source
# File lib/i18n/backend/weeler/lock.rb, line 67
def self.maybe_released_lock(name)
  logger.debug("Released lock '#{name}' (if we are not in a bigger transaction)")
  Thread.current[:definitely_acquired_locks] ||= {}
  Thread.current[:definitely_acquired_locks].delete(name)
end