fog-aws-dynamodb-locker 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in fog-aws-dynamodb-locker.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Michael Gorsuch
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,65 @@
1
+ # Fog::AWS:DynamoDB::Locker
2
+
3
+ This is an attempt to use DynamoDB as a lock store.
4
+
5
+ We don't have a 'Locks as a Service' thing yet, so this is an experiment to make the best of what we do have.
6
+
7
+ Would love to hear of any use of this or if you think it is just a terrible idea to begin with.
8
+
9
+ Inspired by the simplicity of [sequel-pg-locker](https://github.com/dylanegan/sequel-pg-locker) by [@dylanegan](https://github.com/dylanegan).
10
+
11
+ ## Installation
12
+
13
+ Add this line to your application's Gemfile:
14
+
15
+ gem 'fog-aws-dynamodb-locker'
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install fog-aws-dynamodb-locker
24
+
25
+ ## Usage
26
+
27
+ ```bash
28
+ export AWS_ACCESS_KEY_ID=...
29
+ export AWS_SECRET_ACCESS_KEY=...
30
+ export DYNAMODB_LOCK_TABLE='my-lock-table'
31
+ ```
32
+
33
+ ```ruby
34
+ require 'fog/aws/dynamodb/locker'
35
+
36
+ # first run only
37
+ Fog::AWS::DynamoDB::Locker.init!
38
+
39
+ # create a lock
40
+ Fog::AWS::DynamoDB::Locker.lock!('my lock')
41
+ # => true
42
+
43
+ # try to claim it a second time
44
+ Fog::AWS::DynamoDB::Locker.lock!('my lock')
45
+ # => false
46
+
47
+ # release it
48
+ Fog::AWS::DynamoDB::Locker.release!('my lock')
49
+ # => true
50
+ ```
51
+
52
+ ## TODO
53
+
54
+ * real specs
55
+ * set a timestamp on each lock
56
+ * allow metadata on each lock
57
+ * `Fog::AWS::DynamoDB::Locker.sweep!(n)` to remove all locks that are `n` old
58
+
59
+ ## Contributing
60
+
61
+ 1. Fork it
62
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
63
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
64
+ 4. Push to the branch (`git push origin my-new-feature`)
65
+ 5. Create new Pull Request
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'fog/aws/dynamodb/locker'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "fog-aws-dynamodb-locker"
8
+ gem.version = Fog::AWS::DynamoDB::Locker::VERSION
9
+ gem.authors = ["Michael Gorsuch"]
10
+ gem.email = ["michael.gorsuch@gmail.com"]
11
+ gem.description = %q{an attempt to leverage DynamoDB as an HA lock store}
12
+ gem.summary = gem.description
13
+ gem.homepage = ""
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+ gem.add_dependency('fog')
20
+ gem.add_development_dependency('rspec')
21
+ end
@@ -0,0 +1,47 @@
1
+ require 'fog'
2
+ require 'fog/aws/dynamodb/locker/version'
3
+
4
+ module Fog::AWS::DynamoDB::Locker
5
+ extend self
6
+
7
+ def connect
8
+ Fog::AWS::DynamoDB.new(aws_access_key_id: env!('AWS_ACCESS_KEY_ID'), aws_secret_access_key: env!('AWS_SECRET_ACCESS_KEY'))
9
+ end
10
+
11
+ def db
12
+ @db ||= connect
13
+ end
14
+
15
+ def env!(key)
16
+ ENV[key] || raise("Could not find #{key} in ENV")
17
+ end
18
+
19
+ def init!(opts={})
20
+ db.create_table(table_name,
21
+ {:HashKeyElement => {:AttributeName => "id", :AttributeType => "S"}},
22
+ {:ReadCapacityUnits => 1, :WriteCapacityUnits => 1}.merge(opts))
23
+ true
24
+ end
25
+
26
+ def lock!(id, meta_data={})
27
+ begin
28
+ db.put_item(table_name, {:id => {:S => id}}, {:Expected => {:id => {:Exists => false}}})
29
+ rescue Excon::Errors::BadRequest => e
30
+ if JSON.parse(e.response.body)['__type'] == 'com.amazonaws.dynamodb.v20111205#ConditionalCheckFailedException'
31
+ return false
32
+ else
33
+ raise
34
+ end
35
+ end
36
+ true
37
+ end
38
+
39
+ def release!(id)
40
+ db.delete_item(table_name, {:HashKeyElement => {"S" => id}})
41
+ true
42
+ end
43
+
44
+ def table_name
45
+ env!('DYNAMODB_LOCK_TABLE')
46
+ end
47
+ end
@@ -0,0 +1,3 @@
1
+ module Fog::AWS::DynamoDB::Locker
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,17 @@
1
+ # This file was generated by the `rspec --init` command. Conventionally, all
2
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
+ # Require this file using `require "spec_helper"` to ensure that it is only
4
+ # loaded once.
5
+ #
6
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
7
+ RSpec.configure do |config|
8
+ config.treat_symbols_as_metadata_keys_with_true_values = true
9
+ config.run_all_when_everything_filtered = true
10
+ config.filter_run :focus
11
+
12
+ # Run specs in random order to surface order dependencies. If you find an
13
+ # order dependency and want to debug it, you can fix the order by providing
14
+ # the seed, which is printed after each run.
15
+ # --seed 1234
16
+ config.order = 'random'
17
+ end
metadata ADDED
@@ -0,0 +1,88 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fog-aws-dynamodb-locker
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Michael Gorsuch
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-08-21 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: fog
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rspec
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ description: an attempt to leverage DynamoDB as an HA lock store
47
+ email:
48
+ - michael.gorsuch@gmail.com
49
+ executables: []
50
+ extensions: []
51
+ extra_rdoc_files: []
52
+ files:
53
+ - .gitignore
54
+ - .rspec
55
+ - Gemfile
56
+ - LICENSE.txt
57
+ - README.md
58
+ - Rakefile
59
+ - fog-aws-dynamodb-locker.gemspec
60
+ - lib/fog/aws/dynamodb/locker.rb
61
+ - lib/fog/aws/dynamodb/locker/version.rb
62
+ - spec/spec_helper.rb
63
+ homepage: ''
64
+ licenses: []
65
+ post_install_message:
66
+ rdoc_options: []
67
+ require_paths:
68
+ - lib
69
+ required_ruby_version: !ruby/object:Gem::Requirement
70
+ none: false
71
+ requirements:
72
+ - - ! '>='
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ required_rubygems_version: !ruby/object:Gem::Requirement
76
+ none: false
77
+ requirements:
78
+ - - ! '>='
79
+ - !ruby/object:Gem::Version
80
+ version: '0'
81
+ requirements: []
82
+ rubyforge_project:
83
+ rubygems_version: 1.8.23
84
+ signing_key:
85
+ specification_version: 3
86
+ summary: an attempt to leverage DynamoDB as an HA lock store
87
+ test_files:
88
+ - spec/spec_helper.rb