dynalock 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 854bd030d7806c8763eff00fc43d53ccc281c700
4
+ data.tar.gz: 9d4e150b94b91c2e32b0d909ed329c309a652fe7
5
+ SHA512:
6
+ metadata.gz: 37978f44c8b4510bf06a2ed61c5531c0254e07e0061a19aa61cbf0945cce4e36f5035fea85c692d015863308f3eef987fcc016a9ecb7c77f2f370ccc925c230f
7
+ data.tar.gz: f5e526cbf19e456995c6e521f35d8ff8d43f9cd60dc543c26820a7d224fe404a22374c5fbf09e5034cd6c2907733ef7ec88ae3666ad50eaaa4ba24b35dab3132
@@ -0,0 +1,8 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.4.0
5
+ before_install: gem install bundler -v 1.16.0
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at guillermo@cientifico.net. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source "https://rubygems.org"
2
+
3
+
4
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
5
+
6
+ # Specify your gem's dependencies in dynalock.gemspec
7
+ gemspec
@@ -0,0 +1,33 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ dynalock (0.2.0)
5
+ aws-sdk-dynamodb
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ aws-partitions (1.44.0)
11
+ aws-sdk-core (3.11.0)
12
+ aws-partitions (~> 1.0)
13
+ aws-sigv4 (~> 1.0)
14
+ jmespath (~> 1.0)
15
+ aws-sdk-dynamodb (1.3.0)
16
+ aws-sdk-core (~> 3)
17
+ aws-sigv4 (~> 1.0)
18
+ aws-sigv4 (1.0.2)
19
+ jmespath (1.3.1)
20
+ minitest (5.10.3)
21
+ rake (10.5.0)
22
+
23
+ PLATFORMS
24
+ ruby
25
+
26
+ DEPENDENCIES
27
+ bundler (~> 1.16)
28
+ dynalock!
29
+ minitest (~> 5.0)
30
+ rake (~> 10.0)
31
+
32
+ BUNDLED WITH
33
+ 1.16.0
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Guillermo Álvarez
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,106 @@
1
+ # Dynalock
2
+
3
+ Dynalock is a distributed lock that uses dynamod db.
4
+
5
+
6
+ ## Background
7
+
8
+ At tourlane we were running cronjobs through AWS ECS. Once the cluster become
9
+ too big, Cloudwatch was scheduling tasks, even if the same task was already
10
+ present. Dynalock solves this issue, ensuring that any new task exit and fails
11
+ before starting the real work.
12
+
13
+ The first assumption is that something like this should exists, but normally
14
+ not as a command line program (if you found any, please let us know). So we
15
+ created our own.
16
+
17
+ ## Installation
18
+
19
+ Add this line to your application's Gemfile:
20
+
21
+ ```ruby
22
+ gem 'dynalock'
23
+ ```
24
+
25
+ And then execute:
26
+
27
+ $ bundle
28
+
29
+ Or install it yourself as:
30
+
31
+ $ gem install dynalock
32
+
33
+ You need to create a table in dynamo db with "id" as a primary key, and "expires" as expires.
34
+ The default table name is "locks"
35
+
36
+
37
+ ## Usage
38
+
39
+
40
+ Set the environment variables to its proper values.
41
+
42
+
43
+ AWS_ACCESS_KEY_ID
44
+ AWS_REGION
45
+ AWS_SECRET_ACCESS_KEY
46
+
47
+
48
+ ### Usage inside ruby
49
+
50
+ ```ruby
51
+ require 'dynalock'
52
+
53
+ include Dynalock::Lock
54
+
55
+ adquire_lock(context: "my_lock", table: TABLE, owner: @owner, expire_time: 10)
56
+ refresh_lock(context: "my_lock", table: TABLE, owner: @owner, expire_time: 10)
57
+ with_lock(context: "my_lock", table: TABLE, owner: @owner) { "Only run this" }
58
+ ```
59
+
60
+ Most of the paramenters are optional
61
+
62
+ ```ruby
63
+ adquire_lock(context: "my_lock")
64
+ refresh_lock(context: "my_lock")
65
+ with_lock(context: "my_lock") { "Only run this" }
66
+ ```
67
+
68
+
69
+ ### Usage through the command line
70
+
71
+ ```sh
72
+ $ dynalock my_program
73
+ ```
74
+
75
+ This will try to adquire to a lock in dynamodb for 10 seconds, and refresh it every 5 seconds and run your program. The command will be context.
76
+
77
+ ## Development
78
+
79
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run
80
+ `rake test` to run the tests. You can also run `bin/console` for an interactive
81
+ prompt that will allow you to experiment.
82
+
83
+ To install this gem onto your local machine, run `bundle exec rake install`. To
84
+ release a new version, update the version number in `version.rb`, and then run
85
+ `bundle exec rake release`, which will create a git tag for the version, push
86
+ git commits and tags, and push the `.gem` file to
87
+ [rubygems.org](https://rubygems.org).
88
+
89
+ ## Contributing
90
+
91
+ Bug reports and pull requests are welcome on GitHub at
92
+ https://github.com/guillermo/dynalock. This project is intended to be a safe,
93
+ welcoming space for collaboration, and contributors are expected to adhere to
94
+ the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
95
+
96
+ ## License
97
+
98
+ The gem is available as open source under the terms of the [MIT
99
+ License](https://opensource.org/licenses/MIT).
100
+
101
+ ## Code of Conduct
102
+
103
+ Everyone interacting in the Dynalock project’s codebases, issue trackers, chat
104
+ rooms and mailing lists is expected to follow the [code of
105
+ conduct](https://github.com/guillermo/dynalock/blob/master/CODE_OF_CONDUCT.md).
106
+
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList["test/**/*_test.rb"]
8
+ end
9
+
10
+ task :default => :test
data/TODO.md ADDED
@@ -0,0 +1,8 @@
1
+ # TODO
2
+
3
+ [ ] Add an option to release a lock.
4
+ [ ] When the lock exists and its own by someone else, show by whom.
5
+ [ ] Allow to specify the aws credentials without relying in the env variables.
6
+ [ ] Allow to pass parameters to the command line.
7
+ [ ] Automate the creation of the DynamoDB table.
8
+ [ ] Add an option to remove the table.
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "dynalock"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,30 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "dynalock/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "dynalock"
8
+ spec.version = Dynalock::VERSION
9
+ spec.authors = ["Guillermo Alarez"]
10
+ spec.email = ["guillermo@cientifico.net"]
11
+
12
+ spec.summary = %q{Distributed lock using dynamodb}
13
+ spec.description = %q{dynalock is a distributed lock that uses Amazon Web Service Dynamod DB (Ruby api and command line... command)}
14
+ spec.homepage = "https://github.com/tourlane/dynalock"
15
+ spec.license = "MIT"
16
+
17
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
18
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
19
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
20
+ f.match(%r{^(test|spec|features)/})
21
+ end
22
+ spec.bindir = "exe"
23
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
24
+ spec.require_paths = ["lib"]
25
+
26
+ spec.add_dependency "aws-sdk-dynamodb"
27
+ spec.add_development_dependency "bundler", "~> 1.16"
28
+ spec.add_development_dependency "rake", "~> 10.0"
29
+ spec.add_development_dependency "minitest", "~> 5.0"
30
+ end
@@ -0,0 +1,20 @@
1
+ #!/usr/bin/env ruby
2
+
3
+
4
+ if ARGV[0].nil?
5
+ $stderr.puts "Usage: #{$0} COMMAND"
6
+ exit(-1)
7
+ end
8
+
9
+ require 'dynalock'
10
+
11
+ include Dynalock::Lock
12
+
13
+ begin
14
+ with_lock(context: ARGV[0]) do
15
+ system(*ARGV)
16
+ end
17
+ rescue Locked
18
+ $stderr.puts "This program is already running"
19
+ exit(-3)
20
+ end
@@ -0,0 +1,7 @@
1
+ require "dynalock/version"
2
+ require "dynalock/lock"
3
+
4
+ module Dynalock
5
+ end
6
+
7
+
@@ -0,0 +1,71 @@
1
+ require 'aws-sdk-dynamodb'
2
+
3
+ module Dynalock
4
+ module Lock
5
+ class Locked < Exception ; end
6
+ def adquire_lock(context:, owner: lock_default_owner, table: "locks", expire_time: 10)
7
+ dynamodb_client.put_item(
8
+ table_name: table,
9
+ item: {
10
+ id: context,
11
+ lock_owner: owner,
12
+ expires: Time.now.utc.to_i + expire_time
13
+ },
14
+ condition_expression: "attribute_not_exists(expires) OR expires < :expires",
15
+ expression_attribute_values: {
16
+ ":expires": Time.now.utc.to_i
17
+ }
18
+ )
19
+ true
20
+ rescue Aws::DynamoDB::Errors::ConditionalCheckFailedException
21
+ return false
22
+ end
23
+
24
+ def refresh_lock(context:, owner: lock_default_owner, table: "locks", expire_time: 10)
25
+ dynamodb_client.update_item({
26
+ table_name: table,
27
+ key: { id: context },
28
+ update_expression: "SET expires = :expires",
29
+ condition_expression: "attribute_exists(expires) AND expires > :now AND lock_owner = :owner",
30
+ expression_attribute_values: {
31
+ ":expires": Time.now.utc.to_i + expire_time,
32
+ ":owner": owner,
33
+ ":now": Time.now.utc.to_i
34
+ },
35
+ })
36
+ true
37
+ rescue Aws::DynamoDB::Errors::ConditionalCheckFailedException
38
+ return false
39
+ end
40
+
41
+ def with_lock(context:, owner: lock_default_owner, table: "locks")
42
+ expire_time = 5
43
+
44
+ result = adquire_lock(context: context, owner: owner, table: table, expire_time: expire_time)
45
+ raise Locked.new if result == false
46
+
47
+ thread = Thread.new {
48
+ loop do
49
+ refresh_lock(context: context, owner: owner, table: table, expire_time: expire_time)
50
+ sleep(expire_time / 2.0)
51
+ end
52
+ }
53
+ if block_given?
54
+ return yield
55
+ end
56
+ ensure
57
+ thread.kill unless thread.nil?
58
+ end
59
+
60
+ private
61
+
62
+ def lock_default_owner
63
+ @lock_default_owner ||= "#{ENV["USER"]}@#{`hostname`.strip}"
64
+ end
65
+
66
+ def dynamodb_client
67
+ Aws::DynamoDB::Client.new
68
+ end
69
+
70
+ end
71
+ end
@@ -0,0 +1,3 @@
1
+ module Dynalock
2
+ VERSION = "0.2.0"
3
+ end
metadata ADDED
@@ -0,0 +1,118 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dynalock
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - Guillermo Alarez
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-01-10 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: aws-sdk-dynamodb
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.16'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.16'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: minitest
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '5.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '5.0'
69
+ description: dynalock is a distributed lock that uses Amazon Web Service Dynamod DB
70
+ (Ruby api and command line... command)
71
+ email:
72
+ - guillermo@cientifico.net
73
+ executables:
74
+ - dynalock
75
+ extensions: []
76
+ extra_rdoc_files: []
77
+ files:
78
+ - ".gitignore"
79
+ - ".travis.yml"
80
+ - CODE_OF_CONDUCT.md
81
+ - Gemfile
82
+ - Gemfile.lock
83
+ - LICENSE.txt
84
+ - README.md
85
+ - Rakefile
86
+ - TODO.md
87
+ - bin/console
88
+ - bin/setup
89
+ - dynalock.gemspec
90
+ - exe/dynalock
91
+ - lib/dynalock.rb
92
+ - lib/dynalock/lock.rb
93
+ - lib/dynalock/version.rb
94
+ homepage: https://github.com/tourlane/dynalock
95
+ licenses:
96
+ - MIT
97
+ metadata: {}
98
+ post_install_message:
99
+ rdoc_options: []
100
+ require_paths:
101
+ - lib
102
+ required_ruby_version: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ version: '0'
107
+ required_rubygems_version: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - ">="
110
+ - !ruby/object:Gem::Version
111
+ version: '0'
112
+ requirements: []
113
+ rubyforge_project:
114
+ rubygems_version: 2.6.8
115
+ signing_key:
116
+ specification_version: 4
117
+ summary: Distributed lock using dynamodb
118
+ test_files: []