Class: MailManager::Lock

Inherits:
Object
  • Object
show all
Defined in:
lib/mail_manager/lock.rb

Class Method Summary collapse

Class Method Details

.with_lock(name, timeout = 5, max_attempts = 1, &block) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
# File 'lib/mail_manager/lock.rb', line 4

def self.with_lock(name, timeout=5, max_attempts=1, &block)
  ActiveRecord::Base.connection_pool.with_connection do |connection|
    begin
      lock = get_lock(connection,name,timeout,max_attempts)
      raise MailManager::LockException.new("Failed to obtain lock #{name} in #{timeout} secs") unless lock
      yield lock
    ensure
      is_released = release_lock(connection,name)
      Rails.logger.warn "Warning: lock #{name} not released!" unless is_released.values.include?('1')
    end
  end
end