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.
7 8 9 10 11 |
# File 'lib/terrafying/dynamodb/named_lock.rb', line 7 def initialize(table_name, name) @table_name = table_name @name = name @client = Terrafying::DynamoDb.client end |
Instance Method Details
#acquire ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/terrafying/dynamodb/named_lock.rb', line 36 def acquire @client.ensure_table(table) do begin 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 end |
#release(lock_id) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/terrafying/dynamodb/named_lock.rb', line 62 def release(lock_id) @client.ensure_table(table) do begin @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 end |
#status ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/terrafying/dynamodb/named_lock.rb', line 13 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 59 60 |
# File 'lib/terrafying/dynamodb/named_lock.rb', line 48 def steal @client.ensure_table(table) do begin 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 end |