zlk 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/Gemfile +6 -0
- data/LICENSE.txt +21 -0
- data/README.md +42 -0
- data/Rakefile +2 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/zlk.rb +68 -0
- data/lib/zlk/config.rb +19 -0
- data/lib/zlk/lock.rb +58 -0
- data/lib/zlk/lockable.rb +33 -0
- data/lib/zlk/null_lock.rb +19 -0
- data/lib/zlk/version.rb +3 -0
- data/zlk.gemspec +35 -0
- metadata +101 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: abfebeccf8a57c9cb6b9e6fb3c45b435968284baaed19ce3e1d1eb7128f65362
|
4
|
+
data.tar.gz: d1e16ba95a2f76b1b9bfd21f72efb25c52d3e0e03a2cb0b59315911ecf92b851
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 8077e2850bf115df518abfbdaabbe1b3dd3b9a77b8726a5cd1cf071cc4136e9a5bf61bdc43974baabdf50dff4bc4d9c4918334b3aab0d1f87dd5e876467fe8c5
|
7
|
+
data.tar.gz: ef4a0039d783bf08f83a3c3aae759aed57830d146f4f867cda849def963f218b3e7c55400e9ff6b5d723423d35c151e2e2584a382c24f5dc15e1671fcb2c9fd6
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2017 Nikolay Markov
|
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.
|
data/README.md
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
# Zlk
|
2
|
+
|
3
|
+
Simple wrapper over `ZK` gem for Zookeeper locks.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'zlk'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install zlk
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
```ruby
|
24
|
+
Zlk.config_file = Rails.root.join('config', 'zlk.yml')
|
25
|
+
Zlk.env = Rails.env
|
26
|
+
|
27
|
+
Zlk.create_lock('lock/path').run_exclusively { <your code> }
|
28
|
+
```
|
29
|
+
|
30
|
+
## Development
|
31
|
+
|
32
|
+
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.
|
33
|
+
|
34
|
+
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).
|
35
|
+
|
36
|
+
## Contributing
|
37
|
+
|
38
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/mainameiz/zlk.
|
39
|
+
|
40
|
+
## License
|
41
|
+
|
42
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "zlk"
|
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
data/lib/zlk.rb
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
require "erb"
|
2
|
+
require "yaml"
|
3
|
+
require "zlk/version"
|
4
|
+
|
5
|
+
module Zlk
|
6
|
+
class << self
|
7
|
+
attr_accessor :mutex
|
8
|
+
Zlk.mutex = Mutex.new
|
9
|
+
|
10
|
+
def create_lock(path)
|
11
|
+
if config.use_fake_locks?
|
12
|
+
NullLock.new
|
13
|
+
else
|
14
|
+
Lock.new(path)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
# Lazy initialization
|
19
|
+
def connection_pool
|
20
|
+
return @connection_pool if @connection_pool
|
21
|
+
|
22
|
+
mutex.synchronize do
|
23
|
+
hosts = config.hosts.join(',')
|
24
|
+
|
25
|
+
@connection_pool = ZK::Pool::Simple.new(
|
26
|
+
hosts,
|
27
|
+
config.pool,
|
28
|
+
chroot: config.chroot,
|
29
|
+
timeout: config.connection_timeout,
|
30
|
+
ephemeral: true
|
31
|
+
)
|
32
|
+
end
|
33
|
+
|
34
|
+
@connection_pool
|
35
|
+
end
|
36
|
+
|
37
|
+
def env=(env)
|
38
|
+
@env = env
|
39
|
+
@config = nil
|
40
|
+
end
|
41
|
+
|
42
|
+
def env
|
43
|
+
@env
|
44
|
+
end
|
45
|
+
|
46
|
+
def config_file=(filepath)
|
47
|
+
@config_file = filepath
|
48
|
+
@config = nil
|
49
|
+
end
|
50
|
+
|
51
|
+
def config_file
|
52
|
+
@config_file
|
53
|
+
end
|
54
|
+
|
55
|
+
def config
|
56
|
+
@config ||= begin
|
57
|
+
yaml = ERB.new(File.read(config_file)).result
|
58
|
+
hash = YAML.load(yaml)
|
59
|
+
Zlk::Config.new(hash[env]) if hash
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
require "zlk/config"
|
66
|
+
require "zlk/lock"
|
67
|
+
require "zlk/null_lock"
|
68
|
+
require "zlk/lockable"
|
data/lib/zlk/config.rb
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
module Zlk
|
2
|
+
class Config
|
3
|
+
attr_reader :hash
|
4
|
+
|
5
|
+
def initialize(hash)
|
6
|
+
@hash = hash
|
7
|
+
end
|
8
|
+
|
9
|
+
def use_fake_locks?
|
10
|
+
!!hash['use_fake_locks'.freeze]
|
11
|
+
end
|
12
|
+
|
13
|
+
%w(hosts pool chroot connection_timeout).each do |config_key|
|
14
|
+
define_method(config_key) do
|
15
|
+
hash[config_key]
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
data/lib/zlk/lock.rb
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
module Zlk
|
2
|
+
class Lock
|
3
|
+
LockWaitTimeoutError = Class.new(StandardError)
|
4
|
+
|
5
|
+
attr_reader :path
|
6
|
+
|
7
|
+
def initialize(path)
|
8
|
+
@path = sanitize_path(path)
|
9
|
+
end
|
10
|
+
|
11
|
+
def run_exclusively
|
12
|
+
with_locker do |locker|
|
13
|
+
locker.with_lock(wait: true) { yield }
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def with_timeout(timeout)
|
18
|
+
with_locker do |locker|
|
19
|
+
begin
|
20
|
+
locker.with_lock(wait: timeout) { yield(true) }
|
21
|
+
rescue ZK::Exceptions::LockWaitTimeoutError => e
|
22
|
+
yield(false)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def with_timeout!(timeout)
|
28
|
+
with_timeout(timeout) do |locked|
|
29
|
+
if locked
|
30
|
+
yield
|
31
|
+
else
|
32
|
+
fail Zlk::Lock::LockWaitTimeoutError
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def acquirable?
|
38
|
+
with_locker do |locker|
|
39
|
+
locker.acquirable?
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
private
|
44
|
+
|
45
|
+
def with_locker
|
46
|
+
Zlk.connection_pool.with_connection do |connection|
|
47
|
+
locker = connection.locker(path)
|
48
|
+
yield(locker)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
# NOTE: it can be reimplemented really precisely (https://github.com/apache/zookeeper/blob/release-3.5.2/src/java/main/org/apache/zookeeper/common/PathUtils.java#L43-L102)
|
53
|
+
# but simple is better. Leave only word characters and substitute the rest with an underscore
|
54
|
+
def sanitize_path(path)
|
55
|
+
I18n.transliterate(path).scan(/\w+/).join('_')
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
data/lib/zlk/lockable.rb
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
module Zlk
|
2
|
+
module Lockable
|
3
|
+
def lock_key
|
4
|
+
suffix = if respond_to?(:id)
|
5
|
+
id
|
6
|
+
else
|
7
|
+
respond_to?(:uuid) ? uuid : fail("Define #{self.class}#lock_key")
|
8
|
+
end
|
9
|
+
|
10
|
+
"#{self.class}-#{suffix}"
|
11
|
+
end
|
12
|
+
|
13
|
+
def run_exclusively
|
14
|
+
lock.run_exclusively { yield }
|
15
|
+
end
|
16
|
+
|
17
|
+
def run_with_timeout!
|
18
|
+
lock.with_timeout!(lock_timeout) { yield }
|
19
|
+
end
|
20
|
+
|
21
|
+
def lock_timeout
|
22
|
+
10
|
23
|
+
end
|
24
|
+
|
25
|
+
def locked?
|
26
|
+
!lock.acquirable?
|
27
|
+
end
|
28
|
+
|
29
|
+
def lock
|
30
|
+
Zlk.create_lock(lock_key)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
data/lib/zlk/version.rb
ADDED
data/zlk.gemspec
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "zlk/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "zlk"
|
8
|
+
spec.version = Zlk::VERSION
|
9
|
+
spec.authors = ["Nikolay Markov"]
|
10
|
+
spec.email = ["mainameiz@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Simple Zookeeper locks for ruby}
|
13
|
+
spec.homepage = "https://github.com/mainameiz/zlk"
|
14
|
+
spec.license = "MIT"
|
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_runtime_dependency "zk", "~> 1.9"
|
33
|
+
spec.add_development_dependency "bundler", "~> 1.15"
|
34
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
35
|
+
end
|
metadata
ADDED
@@ -0,0 +1,101 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: zlk
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.2.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Nikolay Markov
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-03-22 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: zk
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.9'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.9'
|
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.15'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.15'
|
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
|
+
description:
|
56
|
+
email:
|
57
|
+
- mainameiz@gmail.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- Gemfile
|
64
|
+
- LICENSE.txt
|
65
|
+
- README.md
|
66
|
+
- Rakefile
|
67
|
+
- bin/console
|
68
|
+
- bin/setup
|
69
|
+
- lib/zlk.rb
|
70
|
+
- lib/zlk/config.rb
|
71
|
+
- lib/zlk/lock.rb
|
72
|
+
- lib/zlk/lockable.rb
|
73
|
+
- lib/zlk/null_lock.rb
|
74
|
+
- lib/zlk/version.rb
|
75
|
+
- zlk.gemspec
|
76
|
+
homepage: https://github.com/mainameiz/zlk
|
77
|
+
licenses:
|
78
|
+
- MIT
|
79
|
+
metadata:
|
80
|
+
allowed_push_host: https://rubygems.org
|
81
|
+
post_install_message:
|
82
|
+
rdoc_options: []
|
83
|
+
require_paths:
|
84
|
+
- lib
|
85
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
91
|
+
requirements:
|
92
|
+
- - ">="
|
93
|
+
- !ruby/object:Gem::Version
|
94
|
+
version: '0'
|
95
|
+
requirements: []
|
96
|
+
rubyforge_project:
|
97
|
+
rubygems_version: 2.7.6
|
98
|
+
signing_key:
|
99
|
+
specification_version: 4
|
100
|
+
summary: Simple Zookeeper locks for ruby
|
101
|
+
test_files: []
|