hedgerow 0.1.0

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: ac792d5cc41c652593955cfad286c045ba991a1b
4
+ data.tar.gz: 10ac1a6e3d25f53ac759ce3a21d251218c53c0c2
5
+ SHA512:
6
+ metadata.gz: 951ef20d52f496611d8bd82d9dbce16dfb79c16fda8560615b8fd8d9db3bd0e6867aed2c17baf839e74b197af6ead4971733c863809d9ec380701a401a3f7882
7
+ data.tar.gz: fd5f776ebd0edcf27da4184ee8c9ccaabe324370fa868ffb05f4e61e1216d49f2154a1f9a356e5554839a6585bbcc369aa1b0e2a98d9d994e78524beba2cc9d6
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ .byebug_history
10
+ Gemfile.lock
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --require spec_helper
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in hedgerow.gemspec
6
+ gemspec
data/README.md ADDED
@@ -0,0 +1,43 @@
1
+ # Hedgerow
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/hedgerow`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'hedgerow'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install hedgerow
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ Create a local mysql database and user for hedgerow:
34
+
35
+ ```
36
+ CREATE database hedgerow;
37
+ CREATE USER 'hedgerow'@'localhost' IDENTIFIED BY 'hedge_a_row';
38
+ GRANT ALL PRIVILEGES ON * . * TO 'hedgerow'@'localhost';
39
+ ```
40
+
41
+ ## Contributing
42
+
43
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/hedgerow.
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "hedgerow"
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__)
data/bin/setup ADDED
@@ -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
data/hedgerow.gemspec ADDED
@@ -0,0 +1,37 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "hedgerow/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "hedgerow"
8
+ spec.version = Hedgerow::VERSION
9
+ spec.authors = ["Adam Hallett"]
10
+ spec.email = ["adam.t.hallett@gmail.com"]
11
+
12
+ spec.summary = %q{A distributed locking system for mysql.}
13
+ spec.description = %q{A distributed locking system for mysql.}
14
+ spec.homepage = "https://github.com/atomical/hedgerow"
15
+
16
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
17
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
18
+ if spec.respond_to?(:metadata)
19
+ spec.metadata["allowed_push_host"] = "https://rubygems.org"
20
+ else
21
+ raise "RubyGems 2.0 or newer is required to protect against " \
22
+ "public gem pushes."
23
+ end
24
+
25
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
26
+ f.match(%r{^(test|spec|features)/})
27
+ end
28
+ spec.bindir = "exe"
29
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
30
+ spec.require_paths = ["lib"]
31
+
32
+ spec.add_development_dependency "bundler", "~> 1.16"
33
+ spec.add_development_dependency "rake", "~> 10.0"
34
+ spec.add_development_dependency "rspec"
35
+ spec.add_development_dependency "mysql2"
36
+ spec.add_development_dependency "byebug"
37
+ end
@@ -0,0 +1,3 @@
1
+ class Hedgerow
2
+ VERSION = "0.1.0"
3
+ end
data/lib/hedgerow.rb ADDED
@@ -0,0 +1,58 @@
1
+ require "hedgerow/version"
2
+
3
+ class Hedgerow
4
+ class << self
5
+ def with(name, opts = {})
6
+ if status = lock(name, opts[:timeout] || 10)
7
+ yield
8
+ else
9
+ raise LockFailure.new("Could not acquire lock.")
10
+ end
11
+ ensure
12
+ release(name) if status
13
+ end
14
+
15
+ def lock(name, timeout)
16
+ validate_name!(name)
17
+ parse_response connection.prepare("SELECT GET_LOCK(?, ?)").execute(name, timeout)
18
+ end
19
+
20
+ def release(name)
21
+ validate_name!(name)
22
+ parse_response connection.prepare("SELECT RELEASE_LOCK(?)").execute(name)
23
+ end
24
+
25
+ def validate_name!(name)
26
+ if name.length > 64
27
+ raise LockFailure.new("MySQL enforces a maximum length on lock names of 64 characters.")
28
+ end
29
+ end
30
+
31
+ def parse_response(r)
32
+ if defined?(ActiveRecord::Base) && connection.class == ActiveRecord::Base
33
+ val = r.to_a.flatten.first
34
+ elsif defined?(Mysql2::Client) && connection.class == Mysql2::Client
35
+ val = r.to_a.first.values.first
36
+ else
37
+ raise "Cannot find response parsing scheme."
38
+ end
39
+ val == 1 ? true : false
40
+ end
41
+
42
+ def connection=(connection)
43
+ @@connection = connection
44
+ end
45
+
46
+ def connection
47
+ @@connection ||= begin
48
+ if defined?(ActiveRecord)
49
+ ActiveRecord::Base.connection.raw_connection
50
+ else
51
+ raise "Could not find connection for hedgerow."
52
+ end
53
+ end
54
+ end
55
+ end
56
+
57
+ class LockFailure < StandardError; end
58
+ end
metadata ADDED
@@ -0,0 +1,124 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hedgerow
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Adam Hallett
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-05-18 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.16'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.16'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: mysql2
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: byebug
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: A distributed locking system for mysql.
84
+ email:
85
+ - adam.t.hallett@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".rspec"
92
+ - Gemfile
93
+ - README.md
94
+ - Rakefile
95
+ - bin/console
96
+ - bin/setup
97
+ - hedgerow.gemspec
98
+ - lib/hedgerow.rb
99
+ - lib/hedgerow/version.rb
100
+ homepage: https://github.com/atomical/hedgerow
101
+ licenses: []
102
+ metadata:
103
+ allowed_push_host: https://rubygems.org
104
+ post_install_message:
105
+ rdoc_options: []
106
+ require_paths:
107
+ - lib
108
+ required_ruby_version: !ruby/object:Gem::Requirement
109
+ requirements:
110
+ - - ">="
111
+ - !ruby/object:Gem::Version
112
+ version: '0'
113
+ required_rubygems_version: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ requirements: []
119
+ rubyforge_project:
120
+ rubygems_version: 2.6.13
121
+ signing_key:
122
+ specification_version: 4
123
+ summary: A distributed locking system for mysql.
124
+ test_files: []