Module: DynamoDBMutex::Lock

Extended by:
Lock
Includes:
Logging
Included in:
Lock
Defined in:
lib/dynamodb_mutex/lock.rb

Constant Summary collapse

TABLE_NAME =
'dynamodb-mutex'

Instance Attribute Summary

Attributes included from Logging

#logger

Instance Method Summary collapse

Instance Method Details

#with_lock(name, opts = {}) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/dynamodb_mutex/lock.rb', line 14

def with_lock name, opts = {}
  opts[:ttl]      ||= 10     
  opts[:block]    ||= 1
  opts[:sleep]    ||= 0.1        

  if create(name, opts)
    begin Timeout::timeout(opts[:ttl]) { return(yield) }
    ensure delete(name)
    end
  else
    raise LockError, "Unable to hold #{name} after #{opts[:block]} ms"
  end
end