Class: Marloss::Store

Inherits:
Object
  • Object
show all
Defined in:
lib/marloss/store.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(table, hash_key, ttl: 30, client_options: {}) ⇒ Store

Returns a new instance of Store.



8
9
10
11
12
13
# File 'lib/marloss/store.rb', line 8

def initialize(table, hash_key, ttl: 30, client_options: {})
  @client = Aws::DynamoDB::Client.new(client_options)
  @table = table
  @hash_key = hash_key
  @ttl = ttl
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



6
7
8
# File 'lib/marloss/store.rb', line 6

def client
  @client
end

#hash_keyObject (readonly)

Returns the value of attribute hash_key.



6
7
8
# File 'lib/marloss/store.rb', line 6

def hash_key
  @hash_key
end

#tableObject (readonly)

Returns the value of attribute table.



6
7
8
# File 'lib/marloss/store.rb', line 6

def table
  @table
end

#ttlObject (readonly)

Returns the value of attribute ttl.



6
7
8
# File 'lib/marloss/store.rb', line 6

def ttl
  @ttl
end

Instance Method Details

#create_lock(name) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/marloss/store.rb', line 55

def create_lock(name)
  client.put_item(
    table_name: table,
    item: {
      hash_key => name,
      "ProcessID" => process_id,
      "Expires" => (Time.now + ttl).to_i
    },
    expression_attribute_names: {
      "#E" => "Expires",
      "#P" => "ProcessID"
    },
    expression_attribute_values: {
      ":now" => Time.now.to_i,
      ":process_id" => process_id,
    },
    condition_expression: "attribute_not_exists(#{hash_key}) OR #E < :now OR #P = :process_id"
  )

  Marloss.logger.info("Lock for #{name} created successfully, will expire in #{ttl} seconds")
rescue Aws::DynamoDB::Errors::ConditionalCheckFailedException => e

  Marloss.logger.error("Failed to create lock for #{name}")

  raise(LockNotObtainedError, e.message)
end

#create_tableObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/marloss/store.rb', line 15

def create_table
  client.create_table(
    attribute_definitions: [
      {
        attribute_name: hash_key,
        attribute_type: "S",
      }
    ],
    key_schema: [
      {
        attribute_name: hash_key,
        key_type: "HASH",
      }
    ],
    provisioned_throughput: {
      read_capacity_units: 5,
      write_capacity_units: 5,
    },
    table_name: table
  )

  Marloss.logger.info("DynamoDB table created successfully")

  client.update_time_to_live(
    table_name: table,
    time_to_live_specification: {
      enabled: true,
      attribute_name: "Expires"
    }
  )

  Marloss.logger.info("DynamoDB table TTL configured successfully")
end

#delete_lock(name) ⇒ Object



107
108
109
110
111
# File 'lib/marloss/store.rb', line 107

def delete_lock(name)
  client.delete_item(key: { hash_key => name }, table_name: table)

  Marloss.logger.info("Lock for #{name} deleted successfully")
end

#delete_tableObject



49
50
51
52
53
# File 'lib/marloss/store.rb', line 49

def delete_table
  client.delete_table(table_name: table)

  Marloss.logger.info("DynamoDB table deleted successfully")
end

#refresh_lock(name) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/marloss/store.rb', line 82

def refresh_lock(name)
  client.update_item(
    table_name: table,
    key: { hash_key => name },
    expression_attribute_names: {
      "#E" => "Expires",
      "#P" => "ProcessID"
    },
    expression_attribute_values: {
      ":expires" => (Time.now + ttl).to_i,
      ":now" => Time.now.to_i,
      ":process_id" => process_id,
    },
    update_expression: "SET #E = :expires", 
    condition_expression: "attribute_exists(#{hash_key}) AND (#E < :now OR #P = :process_id)"
  )

  Marloss.logger.info("Lock for #{name} refreshed successfully, will expire in #{ttl} seconds")
rescue Aws::DynamoDB::Errors::ConditionalCheckFailedException => e

  Marloss.logger.error("Failed to refresh lock for #{name}")

  raise(LockNotRefreshedError, e.message)
end