Class: Terrafying::DynamoDb::NamedLock
- Inherits:
-
Object
- Object
- Terrafying::DynamoDb::NamedLock
- Defined in:
- lib/terrafying/dynamodb/named_lock.rb
Instance Method Summary collapse
- #acquire ⇒ Object
-
#initialize(table_name, name) ⇒ NamedLock
constructor
A new instance of NamedLock.
- #release(lock_id) ⇒ Object
- #status ⇒ Object
- #steal ⇒ Object
Constructor Details
#initialize(table_name, name) ⇒ NamedLock
Returns a new instance of NamedLock.
9 10 11 12 13 |
# File 'lib/terrafying/dynamodb/named_lock.rb', line 9 def initialize(table_name, name) @table_name = table_name @name = name @client = Terrafying::DynamoDb.client end |
Instance Method Details
#acquire ⇒ Object
38 39 40 41 42 43 44 45 46 |
# File 'lib/terrafying/dynamodb/named_lock.rb', line 38 def acquire @client.ensure_table(table) do lock_id = SecureRandom.uuid @client.update_item(acquire_request(lock_id)) return lock_id rescue ::Aws::DynamoDB::Errors::ConditionalCheckFailedException raise "Unable to acquire lock: #{status.inspect}" # TODO end end |
#release(lock_id) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/terrafying/dynamodb/named_lock.rb', line 60 def release(lock_id) @client.ensure_table(table) do @client.delete_item( table_name: @table_name, key: { 'name' => @name }, return_values: 'NONE', condition_expression: 'lock_id = :lock_id', expression_attribute_values: { ':lock_id' => lock_id } ) nil rescue ::Aws::DynamoDB::Errors::ConditionalCheckFailedException raise "Unable to release lock: #{status.inspect}" # TODO end end |
#status ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/terrafying/dynamodb/named_lock.rb', line 15 def status @client.ensure_table(table) do resp = @client.get_item( table_name: @table_name, key: { 'name' => @name }, consistent_read: true ) if resp.item return { status: :locked, locked_at: resp.item['locked_at'], metadata: resp.item['metadata'] } else return { status: :unlocked } end end end |
#steal ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/terrafying/dynamodb/named_lock.rb', line 48 def steal @client.ensure_table(table) do lock_id = SecureRandom.uuid req = acquire_request(lock_id) req.delete(:condition_expression) @client.update_item(req) return lock_id rescue ::Aws::DynamoDB::Errors::ConditionalCheckFailedException raise "Unable to steal lock: #{status.inspect}" # TODO end end |