5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
# File 'lib/db_lock/locking.rb', line 5
def with_lock(name, timeout = 0)
timeout = timeout.to_f timeout = 0 if timeout.negative?
raise ArgumentError, "Invalid lock name: #{name.inspect}" if name.empty?
raise AlreadyLocked, 'Already lock in progress' if locked?
name = generate_lock_name(name)
if Adapter.lock(name, timeout)
@locked = true
yield
else
raise AlreadyLocked, "Unable to obtain lock '#{name}' within #{timeout} seconds" unless locked?
end
ensure
Adapter.release(name) if locked?
@locked = false
end
|