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 = 'default.lock', opts = {}) ⇒ Object

May raise

DynamoDBMutex::LockError
Timeout::Error


17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/dynamodb_mutex/lock.rb', line 17

def with_lock name = 'default.lock', opts = {}
  opts[:stale_after]      ||= 10  # seconds
  opts[:wait_for_other]   ||= 1   # seconds
  opts[:polling_interval] ||= 0.1 # seconds

  if create(name, opts)
    begin Timeout::timeout(opts[:stale_after]) { return(yield) }
    ensure delete(name)
    end
  else
    raise LockError, "Unable to acquire #{name} after #{opts[:wait_for_other]} seconds"
  end
end