Module: Fog::AWS::DynamoDB::Locker

Extended by:
Locker
Included in:
Locker
Defined in:
lib/fog/aws/dynamodb/locker/version.rb,
lib/fog/aws/dynamodb/locker.rb

Constant Summary collapse

VERSION =
"0.0.1"

Instance Method Summary collapse

Instance Method Details

#connectObject



7
8
9
# File 'lib/fog/aws/dynamodb/locker.rb', line 7

def connect
  Fog::AWS::DynamoDB.new(aws_access_key_id: env!('AWS_ACCESS_KEY_ID'), aws_secret_access_key: env!('AWS_SECRET_ACCESS_KEY'))
end

#dbObject



11
12
13
# File 'lib/fog/aws/dynamodb/locker.rb', line 11

def db
  @db ||= connect
end

#env!(key) ⇒ Object



15
16
17
# File 'lib/fog/aws/dynamodb/locker.rb', line 15

def env!(key)
  ENV[key] || raise("Could not find #{key} in ENV")
end

#init!(opts = {}) ⇒ Object



19
20
21
22
23
24
# File 'lib/fog/aws/dynamodb/locker.rb', line 19

def init!(opts={})
  db.create_table(table_name,
                  {:HashKeyElement => {:AttributeName => "id", :AttributeType => "S"}},
                  {:ReadCapacityUnits => 1, :WriteCapacityUnits => 1}.merge(opts))
  true
end

#lock!(id, meta_data = {}) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/fog/aws/dynamodb/locker.rb', line 26

def lock!(id, ={})
  begin   
    db.put_item(table_name, {:id => {:S => id}}, {:Expected => {:id => {:Exists => false}}})
  rescue Excon::Errors::BadRequest => e
    if JSON.parse(e.response.body)['__type'] == 'com.amazonaws.dynamodb.v20111205#ConditionalCheckFailedException'
      return false
    else
      raise 
    end
  end
  true
end

#release!(id) ⇒ Object



39
40
41
42
# File 'lib/fog/aws/dynamodb/locker.rb', line 39

def release!(id)
  db.delete_item(table_name, {:HashKeyElement => {"S" => id}})
  true
end

#table_nameObject



44
45
46
# File 'lib/fog/aws/dynamodb/locker.rb', line 44

def table_name
  env!('DYNAMODB_LOCK_TABLE')
end