Class: DBLock::Lock

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

Class Method Summary collapse

Class Method Details

.get(name, timeout = 0) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/db_lock.rb', line 17

def get(name, timeout=0)
  timeout = timeout.to_f # catches nil
  timeout = 0 if timeout < 0
  raise "Invalid lock name: #{name.inspect}" if name.empty?
  raise AlreadyLocked.new("Already lock in progress") if locked?

  name = prefixed_lock_name(name)

  if Adapter.lock(name, timeout)
    @locked = true
    yield
  else
    raise AlreadyLocked.new("Unable to obtain lock '#{name}' within #{timeout} seconds") unless locked?
  end
ensure
  if locked?
    Adapter.release(name)
  end
  @locked = false
end

.locked?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/db_lock.rb', line 38

def locked?
  @locked ||= false
end