dynamodb-mutex 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 6021f2684e6d453eec6b6c571dc9579680720e09
4
+ data.tar.gz: 73721ae381b3f05c1de835ceb819ee60ae5eeff3
5
+ SHA512:
6
+ metadata.gz: 6eec01b731fb9afa95fcd1352555f0be5bdd65bf141c94f6aadbe4509bc15ea04d1f67f18ea8b8aae1734753b6bfe6d6d7dd658eff979a3f932395a1f6da4122
7
+ data.tar.gz: 0ccadf258c6e395214cf9e47e85beeef348351018a480acb9348d4cbdcdcfc8e8960fbd58bf75d7a46e04c4d1a6d886afe89fea05a1753ffbb78e14b412393c0
@@ -1,4 +1,5 @@
1
- require 'aws-sdk-v1'
1
+ require 'aws-sdk'
2
+ require 'socket'
2
3
  require_relative 'logging'
3
4
 
4
5
  module DynamoDBMutex
@@ -24,7 +25,8 @@ module DynamoDBMutex
24
25
  ensure delete(name)
25
26
  end
26
27
  else
27
- raise LockError, "Unable to acquire #{name} after #{opts[:wait_for_other]} seconds"
28
+ raise LockError,
29
+ "Unable to acquire #{name} after #{opts[:wait_for_other]} seconds"
28
30
  end
29
31
  end
30
32
 
@@ -41,11 +43,14 @@ module DynamoDBMutex
41
43
  end
42
44
 
43
45
  begin
44
- table.items.put({:id => name, :created => Time.now.to_i},
45
- :unless_exists => :id)
46
+ dynamodb_client.put_item(
47
+ table_name: TABLE_NAME,
48
+ item: { :id => name, :created => Time.now.to_i },
49
+ condition_expression: 'attribute_not_exists(id)'
50
+ )
46
51
  logger.info "#{pid} acquired #{name}"
47
52
  return true
48
- rescue AWS::DynamoDB::Errors::ConditionalCheckFailedException
53
+ rescue Aws::DynamoDB::Errors::ConditionalCheckFailedException
49
54
  logger.info "#{pid} is waiting for #{name}"
50
55
  sleep opts[:polling_interval]
51
56
  end
@@ -56,7 +61,10 @@ module DynamoDBMutex
56
61
  end
57
62
 
58
63
  def delete(name)
59
- table.items.at(name).delete
64
+ dynamodb_client.delete_item(
65
+ table_name: TABLE_NAME,
66
+ key: { :id => name }
67
+ )
60
68
  logger.info "#{pid} released lock #{name}"
61
69
  end
62
70
 
@@ -69,34 +77,50 @@ module DynamoDBMutex
69
77
  def stale?(name, ttl)
70
78
  return false unless ttl
71
79
 
72
- if lock_attributes = table.items.at(name).attributes.to_h(:consistent_read => true)
73
- if time_locked = lock_attributes["created"]
74
- time_locked < (Time.now.to_i - ttl)
75
- end
76
- end
80
+ item = dynamodb_client.get_item(
81
+ table_name: TABLE_NAME,
82
+ key: { :id => name },
83
+ consistent_read: true
84
+ ).item
85
+
86
+ not item.nil? and Time.now.to_i > item['created'].to_i + ttl
77
87
  end
78
88
 
79
- def table
80
- return @table if @table
81
- dynamo_db = AWS::DynamoDB.new
89
+ def dynamodb_client
90
+ return @dynamodb_client if @dynamodb_client
91
+ @dynamodb_client = Aws::DynamoDB::Client.new
82
92
 
83
93
  begin
84
- tries ||= 10
85
- @table = dynamo_db.tables[TABLE_NAME].load_schema
86
- rescue AWS::DynamoDB::Errors::ResourceInUseException
87
- raise LockError, "Cannot load schema for table #{TABLE_NAME}" if (tries -= 1).zero?
88
-
89
- logger.info "Could not load schema for table #{TABLE_NAME}; retrying"
90
- sleep 1
91
- retry
92
- rescue AWS::DynamoDB::Errors::ResourceNotFoundException
93
- logger.info "Creating table #{TABLE_NAME}"
94
- @table = dynamo_db.tables.create(TABLE_NAME, 5, 5)
95
- sleep 1 while @table.status != :active
96
- logger.info "Table #{TABLE_NAME} created"
94
+ @dynamodb_client.describe_table(table_name: TABLE_NAME)
95
+ rescue Aws::DynamoDB::Errors::ResourceNotFoundException
96
+ logger.info "Table #{TABLE_NAME} not found; creating it."
97
+ @dynamodb_client.create_table(
98
+ table_name: TABLE_NAME,
99
+ attribute_definitions: [
100
+ { attribute_name: 'id', attribute_type: 'S' }
101
+ ],
102
+ key_schema: [
103
+ { attribute_name: 'id', key_type: 'HASH' }
104
+ ],
105
+ provisioned_throughput: {
106
+ read_capacity_units: 5,
107
+ write_capacity_units: 5
108
+ }
109
+ )
110
+ logger.info "Waiting for table #{TABLE_NAME} to be created."
111
+ begin
112
+ @dynamodb_client.wait_until(:table_exists,
113
+ table_name: TABLE_NAME) do |w|
114
+ w.max_attempts = 10
115
+ w.delay = 1
116
+ end
117
+ rescue Aws::Waiters::Errors::WaiterFailed => e
118
+ raise LockError, "Cannot create table #{TABLE_NAME}: #{e.message}"
119
+ end
120
+ logger.info "Table #{TABLE_NAME} has been created."
97
121
  end
98
122
 
99
- @table
123
+ @dynamodb_client
100
124
  end
101
125
  end
102
126
  end
@@ -1,4 +1,4 @@
1
1
 
2
2
  module DynamoDBMutex
3
- VERSION = '0.0.3'
3
+ VERSION = '0.0.4'
4
4
  end
data/spec/spec_helper.rb CHANGED
@@ -2,14 +2,15 @@ require 'rubygems'
2
2
  require 'bundler/setup'
3
3
  require 'rspec'
4
4
  require 'fake_dynamo'
5
- require 'aws-sdk-v1'
5
+ require 'aws-sdk'
6
6
 
7
- AWS.config(:use_ssl => false,
8
- :dynamo_db_endpoint => 'localhost',
9
- :dynamo_db_port => 4567,
10
- :access_key_id => 'foo',
11
- :secret_access_key => 'bar',
12
- )
7
+ Aws.config.update({
8
+ credentials: Aws::Credentials.new(
9
+ 'your_access_key_id',
10
+ 'your_secret_access_key'
11
+ ),
12
+ endpoint: 'http://localhost:4567'
13
+ })
13
14
 
14
15
  require 'dynamodb-mutex'
15
16
 
@@ -21,7 +22,8 @@ RSpec.configure do |config|
21
22
  dynamo_thread = nil
22
23
 
23
24
  config.before(:suite) do
24
- FakeDynamo::Storage.db_path = 'test.fdb'
25
+ FakeDynamo::Logger.setup(:debug)
26
+ FakeDynamo::Storage.instance.init_db('test.fdb')
25
27
  FakeDynamo::Storage.instance.load_aof
26
28
 
27
29
  dynamo_thread = Thread.new do
metadata CHANGED
@@ -1,96 +1,85 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dynamodb-mutex
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
5
- prerelease:
4
+ version: 0.0.4
6
5
  platform: ruby
7
6
  authors:
8
7
  - Dinesh Yadav
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2015-04-23 00:00:00.000000000 Z
11
+ date: 2015-12-16 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
- name: aws-sdk-v1
14
+ name: aws-sdk
16
15
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
16
  requirements:
19
- - - ! '>='
17
+ - - '>='
20
18
  - !ruby/object:Gem::Version
21
19
  version: '0'
22
20
  type: :runtime
23
21
  prerelease: false
24
22
  version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
23
  requirements:
27
- - - ! '>='
24
+ - - '>='
28
25
  - !ruby/object:Gem::Version
29
26
  version: '0'
30
27
  - !ruby/object:Gem::Dependency
31
28
  name: rspec
32
29
  requirement: !ruby/object:Gem::Requirement
33
- none: false
34
30
  requirements:
35
- - - ! '>='
31
+ - - '>='
36
32
  - !ruby/object:Gem::Version
37
33
  version: 2.0.0
38
34
  type: :development
39
35
  prerelease: false
40
36
  version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
37
  requirements:
43
- - - ! '>='
38
+ - - '>='
44
39
  - !ruby/object:Gem::Version
45
40
  version: 2.0.0
46
41
  - !ruby/object:Gem::Dependency
47
42
  name: rspec-mocks
48
43
  requirement: !ruby/object:Gem::Requirement
49
- none: false
50
44
  requirements:
51
- - - ! '>='
45
+ - - '>='
52
46
  - !ruby/object:Gem::Version
53
47
  version: '0'
54
48
  type: :development
55
49
  prerelease: false
56
50
  version_requirements: !ruby/object:Gem::Requirement
57
- none: false
58
51
  requirements:
59
- - - ! '>='
52
+ - - '>='
60
53
  - !ruby/object:Gem::Version
61
54
  version: '0'
62
55
  - !ruby/object:Gem::Dependency
63
56
  name: rake
64
57
  requirement: !ruby/object:Gem::Requirement
65
- none: false
66
58
  requirements:
67
- - - ! '>='
59
+ - - '>='
68
60
  - !ruby/object:Gem::Version
69
61
  version: '0'
70
62
  type: :development
71
63
  prerelease: false
72
64
  version_requirements: !ruby/object:Gem::Requirement
73
- none: false
74
65
  requirements:
75
- - - ! '>='
66
+ - - '>='
76
67
  - !ruby/object:Gem::Version
77
68
  version: '0'
78
69
  - !ruby/object:Gem::Dependency
79
70
  name: fake_dynamo
80
71
  requirement: !ruby/object:Gem::Requirement
81
- none: false
82
72
  requirements:
83
73
  - - '='
84
74
  - !ruby/object:Gem::Version
85
- version: 0.1.3
75
+ version: 0.2.5
86
76
  type: :development
87
77
  prerelease: false
88
78
  version_requirements: !ruby/object:Gem::Requirement
89
- none: false
90
79
  requirements:
91
80
  - - '='
92
81
  - !ruby/object:Gem::Version
93
- version: 0.1.3
82
+ version: 0.2.5
94
83
  description: dynamodb-mutex implements a simple mutex that can be used to coordinate
95
84
  access to shared data from multiple concurrent hosts
96
85
  email: dy@clearhaus.com
@@ -98,45 +87,38 @@ executables: []
98
87
  extensions: []
99
88
  extra_rdoc_files: []
100
89
  files:
90
+ - LICENSE
91
+ - README.rst
101
92
  - Rakefile
102
93
  - lib/dynamodb-mutex.rb
94
+ - lib/dynamodb_mutex.rb
103
95
  - lib/dynamodb_mutex/lock.rb
104
96
  - lib/dynamodb_mutex/logging.rb
105
97
  - lib/dynamodb_mutex/version.rb
106
- - lib/dynamodb_mutex.rb
107
98
  - spec/dynamodb_mutex_spec.rb
108
99
  - spec/spec_helper.rb
109
- - README.rst
110
- - LICENSE
111
100
  homepage: http://github.com/clearhaus/dynamodb-mutex
112
101
  licenses:
113
102
  - MIT
103
+ metadata: {}
114
104
  post_install_message:
115
105
  rdoc_options: []
116
106
  require_paths:
117
107
  - lib
118
108
  required_ruby_version: !ruby/object:Gem::Requirement
119
- none: false
120
109
  requirements:
121
- - - ! '>='
110
+ - - '>='
122
111
  - !ruby/object:Gem::Version
123
112
  version: '0'
124
- segments:
125
- - 0
126
- hash: 409537590926897434
127
113
  required_rubygems_version: !ruby/object:Gem::Requirement
128
- none: false
129
114
  requirements:
130
- - - ! '>='
115
+ - - '>='
131
116
  - !ruby/object:Gem::Version
132
117
  version: '0'
133
- segments:
134
- - 0
135
- hash: 409537590926897434
136
118
  requirements: []
137
119
  rubyforge_project:
138
- rubygems_version: 1.8.23.2
120
+ rubygems_version: 2.4.2
139
121
  signing_key:
140
- specification_version: 3
122
+ specification_version: 4
141
123
  summary: Distributed mutex based on AWS DynamoDB
142
124
  test_files: []