sidekiq-lock 0.3.0 → 0.3.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.gitignore +1 -0
- data/.travis.yml +11 -6
- data/Appraisals +15 -0
- data/CHANGELOG.md +9 -3
- data/Gemfile +0 -6
- data/README.md +2 -0
- data/gemfiles/sidekiq_2.gemfile +7 -0
- data/gemfiles/sidekiq_3.gemfile +7 -0
- data/gemfiles/sidekiq_4.gemfile +7 -0
- data/gemfiles/sidekiq_5.gemfile +7 -0
- data/lib/sidekiq/lock.rb +5 -7
- data/lib/sidekiq/lock/middleware.rb +6 -8
- data/lib/sidekiq/lock/redis_lock.rb +31 -27
- data/lib/sidekiq/lock/version.rb +1 -1
- data/lib/sidekiq/lock/worker.rb +0 -2
- data/sidekiq-lock.gemspec +3 -3
- data/test/lib/lock_test.rb +29 -0
- data/test/lib/middleware_test.rb +1 -2
- data/test/lib/redis_lock_test.rb +1 -1
- data/test/lib/testing/inline_test.rb +0 -1
- data/test/lib/worker_test.rb +1 -2
- data/test/test_helper.rb +7 -11
- data/test/test_workers.rb +2 -0
- metadata +17 -10
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 70c2ca23e60ff013252e111d7507b3665deaaadd31d23a6dc10e972315be335f
|
4
|
+
data.tar.gz: 67cdd99cb1e60accad3304bc13f295051b54d09553ab278503d98240bf149d21
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3595ae9803227cbb2051908bd7ecbbbbec409a57e5f41dbae552b838f5d71decf35dab1ade805ff851c806f7aa69896b0485018401fa41f5454e2638067133ca
|
7
|
+
data.tar.gz: ea73c729c4da75a0a6c47c652376e96f5574ba2f2f93b09ecc6f12564d2d605d00e625247643cbb17462a3a0dca3049a5ed696882f53fe26ff59dda84be3f0a1
|
data/.gitignore
CHANGED
data/.travis.yml
CHANGED
@@ -1,15 +1,20 @@
|
|
1
1
|
language: ruby
|
2
|
+
cache: bundler
|
2
3
|
|
3
4
|
services:
|
4
5
|
- redis-server
|
5
6
|
|
7
|
+
gemfile:
|
8
|
+
- gemfiles/sidekiq_2.gemfile
|
9
|
+
- gemfiles/sidekiq_3.gemfile
|
10
|
+
- gemfiles/sidekiq_4.gemfile
|
11
|
+
- gemfiles/sidekiq_5.gemfile
|
12
|
+
|
6
13
|
rvm:
|
7
|
-
- 2.
|
8
|
-
- 2.
|
9
|
-
|
10
|
-
- 2.
|
11
|
-
- 2.3.0
|
12
|
-
- 2.3.1
|
14
|
+
- 2.2.9
|
15
|
+
- 2.3.6
|
16
|
+
- 2.4.3
|
17
|
+
- 2.5.0
|
13
18
|
|
14
19
|
notifications:
|
15
20
|
email: false
|
data/Appraisals
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
appraise 'sidekiq-2' do
|
2
|
+
gem 'sidekiq', '~> 2.17', '>= 2.17.8'
|
3
|
+
end
|
4
|
+
|
5
|
+
appraise 'sidekiq-3' do
|
6
|
+
gem 'sidekiq', '~> 3.5', '>= 3.5.4'
|
7
|
+
end
|
8
|
+
|
9
|
+
appraise 'sidekiq-4' do
|
10
|
+
gem 'sidekiq', '~> 4.2', '>= 4.2.10'
|
11
|
+
end
|
12
|
+
|
13
|
+
appraise 'sidekiq-5' do
|
14
|
+
gem 'sidekiq', '~> 5.1', '>= 5.1'
|
15
|
+
end
|
data/CHANGELOG.md
CHANGED
@@ -1,4 +1,10 @@
|
|
1
|
-
## 0.3.
|
1
|
+
## 0.3.1 (March 3, 2018)
|
2
|
+
|
3
|
+
- do not assume `ActiveSupport` is loaded / or old `Sidekiq` patches are present (add own symbolize keys logic)
|
4
|
+
- make `options` and `payload` attr readers as `private` in `RedisLock` as it should be - **potentially breaking change** if you were accessing those (abusing) somehow for whatever reason (that shouldn't happen in the first place!)
|
5
|
+
- run test on travis for sidekiq `2.17`, `3.5`, `4.2` and `>= 5.1` and all newest rubies (`2.2` - `2.5`)
|
6
|
+
|
7
|
+
## 0.3.0 (July 28, 2016)
|
2
8
|
|
3
9
|
- ability to set custom lock value. Works just like setting timeout and name (handles procs as well).
|
4
10
|
|
@@ -10,7 +16,7 @@ sidekiq_options lock: {
|
|
10
16
|
}
|
11
17
|
```
|
12
18
|
|
13
|
-
## 0.2.0
|
19
|
+
## 0.2.0 (December 08, 2013)
|
14
20
|
|
15
21
|
- ability to globally configure `lock` method name
|
16
22
|
|
@@ -30,6 +36,6 @@ end
|
|
30
36
|
That will setup `RedisLock` under proper thread variable.
|
31
37
|
This can be handy if you test your workers inline (without full stack middleware)
|
32
38
|
|
33
|
-
## 0.0.1
|
39
|
+
## 0.0.1 (October 14, 2013)
|
34
40
|
|
35
41
|
- Initial release
|
data/Gemfile
CHANGED
@@ -1,8 +1,2 @@
|
|
1
1
|
source "https://rubygems.org"
|
2
2
|
gemspec
|
3
|
-
|
4
|
-
platforms :rbx do
|
5
|
-
gem "rubysl", "~> 2.0" # if using anything in the ruby standard library
|
6
|
-
gem "rubinius-developer_tools", "~> 2.0.0" # if using any of coverage, debugger, profiler
|
7
|
-
gem "minitest" # if using minitest
|
8
|
-
end
|
data/README.md
CHANGED
@@ -6,6 +6,8 @@
|
|
6
6
|
[![Dependency Status](https://gemnasium.com/emq/sidekiq-lock.png)](https://gemnasium.com/emq/sidekiq-lock)
|
7
7
|
[![Gem Version](https://badge.fury.io/rb/sidekiq-lock.png)](http://badge.fury.io/rb/sidekiq-lock)
|
8
8
|
|
9
|
+
**Note:** This is a _complete_ piece of software, it should work across all future sidekiq & ruby versions.
|
10
|
+
|
9
11
|
Redis-based simple locking mechanism for [sidekiq][2]. Uses [SET command][1] introduced in Redis 2.6.16.
|
10
12
|
|
11
13
|
It can be handy if you push a lot of jobs into the queue(s), but you don't want to execute specific jobs at the same time - it provides a `lock` method that you can use in whatever way you want.
|
data/lib/sidekiq/lock.rb
CHANGED
@@ -1,12 +1,11 @@
|
|
1
|
-
require
|
2
|
-
require
|
3
|
-
require
|
4
|
-
require
|
1
|
+
require 'sidekiq/lock/middleware'
|
2
|
+
require 'sidekiq/lock/redis_lock'
|
3
|
+
require 'sidekiq/lock/version'
|
4
|
+
require 'sidekiq/lock/worker'
|
5
5
|
|
6
6
|
module Sidekiq
|
7
|
-
|
8
7
|
def self.lock_method
|
9
|
-
@lock_method
|
8
|
+
@lock_method ||= :lock
|
10
9
|
end
|
11
10
|
|
12
11
|
def self.lock_method=(method)
|
@@ -23,4 +22,3 @@ Sidekiq.configure_server do |config|
|
|
23
22
|
chain.add Sidekiq::Lock::Middleware
|
24
23
|
end
|
25
24
|
end
|
26
|
-
|
@@ -1,7 +1,6 @@
|
|
1
1
|
module Sidekiq
|
2
2
|
module Lock
|
3
3
|
class Middleware
|
4
|
-
|
5
4
|
def call(worker, msg, queue)
|
6
5
|
options = lock_options(worker)
|
7
6
|
setup_lock(options, msg['args']) unless options.nil?
|
@@ -11,14 +10,13 @@ module Sidekiq
|
|
11
10
|
|
12
11
|
private
|
13
12
|
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
def lock_options(worker)
|
19
|
-
worker.class.get_sidekiq_options['lock']
|
20
|
-
end
|
13
|
+
def setup_lock(options, payload)
|
14
|
+
Thread.current[Sidekiq::Lock::THREAD_KEY] = RedisLock.new(options, payload)
|
15
|
+
end
|
21
16
|
|
17
|
+
def lock_options(worker)
|
18
|
+
worker.class.get_sidekiq_options['lock']
|
19
|
+
end
|
22
20
|
end
|
23
21
|
end
|
24
22
|
end
|
@@ -1,16 +1,18 @@
|
|
1
1
|
module Sidekiq
|
2
2
|
module Lock
|
3
3
|
class RedisLock
|
4
|
-
attr_reader :options, :payload
|
5
|
-
|
6
4
|
# checks for configuration
|
7
|
-
def initialize(
|
8
|
-
@options
|
5
|
+
def initialize(options_hash, payload)
|
6
|
+
@options = {}
|
7
|
+
|
8
|
+
options_hash.each_key do |key|
|
9
|
+
@options[key.to_sym] = options_hash[key]
|
10
|
+
end
|
11
|
+
|
9
12
|
@payload = payload
|
10
13
|
@acquired = false
|
11
14
|
|
12
|
-
timeout
|
13
|
-
name
|
15
|
+
timeout && name
|
14
16
|
end
|
15
17
|
|
16
18
|
def acquired?
|
@@ -36,42 +38,44 @@ module Sidekiq
|
|
36
38
|
end
|
37
39
|
|
38
40
|
def name
|
39
|
-
raise ArgumentError,
|
41
|
+
raise ArgumentError, 'Provide a lock name inside sidekiq_options' if options[:name].nil?
|
40
42
|
|
41
43
|
@name ||= (options[:name].respond_to?(:call) ? options[:name].call(*payload) : options[:name])
|
42
44
|
end
|
43
45
|
|
44
46
|
def timeout
|
45
|
-
raise ArgumentError,
|
47
|
+
raise ArgumentError, 'Provide lock timeout inside sidekiq_options' if options[:timeout].nil?
|
46
48
|
|
47
49
|
@timeout ||= (options[:timeout].respond_to?(:call) ? options[:timeout].call(*payload) : options[:timeout]).to_i
|
48
50
|
end
|
49
51
|
|
50
52
|
private
|
51
53
|
|
52
|
-
|
53
|
-
@lock_script_sha ||= Digest::SHA1.hexdigest redis_lock_script
|
54
|
-
end
|
54
|
+
attr_reader :options, :payload
|
55
55
|
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
then
|
60
|
-
return redis.call("del",KEYS[1])
|
61
|
-
else
|
62
|
-
return 0
|
63
|
-
end
|
64
|
-
LUA
|
65
|
-
end
|
56
|
+
def redis_lock_script_sha
|
57
|
+
@lock_script_sha ||= Digest::SHA1.hexdigest redis_lock_script
|
58
|
+
end
|
66
59
|
|
67
|
-
|
68
|
-
|
60
|
+
def redis_lock_script
|
61
|
+
<<-LUA
|
62
|
+
if redis.call("get", KEYS[1]) == ARGV[1]
|
63
|
+
then
|
64
|
+
return redis.call("del",KEYS[1])
|
65
|
+
else
|
66
|
+
return 0
|
69
67
|
end
|
68
|
+
LUA
|
69
|
+
end
|
70
70
|
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
71
|
+
def value
|
72
|
+
@value ||= set_lock_value(options[:value])
|
73
|
+
end
|
74
|
+
|
75
|
+
def set_lock_value(custom_value)
|
76
|
+
return SecureRandom.hex(25) unless custom_value
|
77
|
+
custom_value.respond_to?(:call) ? custom_value.call(*payload) : custom_value
|
78
|
+
end
|
75
79
|
end
|
76
80
|
end
|
77
81
|
end
|
data/lib/sidekiq/lock/version.rb
CHANGED
data/lib/sidekiq/lock/worker.rb
CHANGED
data/sidekiq-lock.gemspec
CHANGED
@@ -20,11 +20,11 @@ Gem::Specification.new do |spec|
|
|
20
20
|
spec.add_dependency "sidekiq", ">= 2.14.0"
|
21
21
|
spec.add_dependency "redis", ">= 3.0.5"
|
22
22
|
|
23
|
-
spec.add_development_dependency "bundler"
|
23
|
+
spec.add_development_dependency "bundler"
|
24
24
|
spec.add_development_dependency "rake"
|
25
25
|
spec.add_development_dependency "rack-test"
|
26
|
-
spec.add_development_dependency "coveralls", "~> 0.
|
26
|
+
spec.add_development_dependency "coveralls", "~> 0.8"
|
27
27
|
spec.add_development_dependency "mocha", "~> 0.14.0"
|
28
28
|
spec.add_development_dependency "minitest"
|
29
|
-
spec.add_development_dependency "
|
29
|
+
spec.add_development_dependency "appraisal"
|
30
30
|
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
require 'open3'
|
3
|
+
|
4
|
+
module Sidekiq
|
5
|
+
describe Lock do
|
6
|
+
it 'automatically loads lock middleware for sidekiq server' do
|
7
|
+
skip 'Sidekiq 2 does not print out middleware information' if Sidekiq::VERSION < '3.0.0'
|
8
|
+
|
9
|
+
cmd = 'sidekiq -r ./test/test_workers.rb -v'
|
10
|
+
buffer = ''
|
11
|
+
|
12
|
+
# very not fancy (https://78.media.tumblr.com/tumblr_lzkpw7DAl21qhy6c9o2_400.gif)
|
13
|
+
# solution, but should do the job
|
14
|
+
Open3.popen3(cmd) do |stdin, stdout, stderr, thread|
|
15
|
+
begin
|
16
|
+
Timeout.timeout(5) do
|
17
|
+
until stdout.eof? do
|
18
|
+
buffer << stdout.read_nonblock(16)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
rescue Timeout::Error
|
22
|
+
Process.kill('KILL', thread.pid)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
assert_match(/\s?Middleware:.*Sidekiq::Lock::Middleware/i, buffer)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
data/test/lib/middleware_test.rb
CHANGED
data/test/lib/redis_lock_test.rb
CHANGED
data/test/lib/worker_test.rb
CHANGED
data/test/test_helper.rb
CHANGED
@@ -1,22 +1,18 @@
|
|
1
1
|
require 'coveralls'
|
2
2
|
Coveralls.wear! do
|
3
|
-
add_filter
|
3
|
+
add_filter '/test/'
|
4
4
|
end
|
5
5
|
|
6
|
-
|
7
|
-
|
6
|
+
require 'minitest/autorun'
|
7
|
+
require 'minitest/pride'
|
8
|
+
require 'mocha/setup'
|
8
9
|
|
9
|
-
require
|
10
|
-
require
|
11
|
-
require "mocha/setup"
|
12
|
-
|
13
|
-
require "sidekiq"
|
14
|
-
require "sidekiq-lock"
|
15
|
-
require "test_workers"
|
10
|
+
require 'sidekiq'
|
11
|
+
require 'test_workers'
|
16
12
|
|
17
13
|
Sidekiq.logger.level = Logger::ERROR
|
18
14
|
|
19
|
-
REDIS = Sidekiq::RedisConnection.create(url:
|
15
|
+
REDIS = Sidekiq::RedisConnection.create(url: 'redis://localhost/15')
|
20
16
|
|
21
17
|
def redis(command, *args)
|
22
18
|
Sidekiq.redis do |c|
|
data/test/test_workers.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sidekiq-lock
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Rafal Wojsznis
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2018-03-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: sidekiq
|
@@ -42,16 +42,16 @@ dependencies:
|
|
42
42
|
name: bundler
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
|
-
- - "
|
45
|
+
- - ">="
|
46
46
|
- !ruby/object:Gem::Version
|
47
|
-
version: '
|
47
|
+
version: '0'
|
48
48
|
type: :development
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
|
-
- - "
|
52
|
+
- - ">="
|
53
53
|
- !ruby/object:Gem::Version
|
54
|
-
version: '
|
54
|
+
version: '0'
|
55
55
|
- !ruby/object:Gem::Dependency
|
56
56
|
name: rake
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
@@ -86,14 +86,14 @@ dependencies:
|
|
86
86
|
requirements:
|
87
87
|
- - "~>"
|
88
88
|
- !ruby/object:Gem::Version
|
89
|
-
version: 0.
|
89
|
+
version: '0.8'
|
90
90
|
type: :development
|
91
91
|
prerelease: false
|
92
92
|
version_requirements: !ruby/object:Gem::Requirement
|
93
93
|
requirements:
|
94
94
|
- - "~>"
|
95
95
|
- !ruby/object:Gem::Version
|
96
|
-
version: 0.
|
96
|
+
version: '0.8'
|
97
97
|
- !ruby/object:Gem::Dependency
|
98
98
|
name: mocha
|
99
99
|
requirement: !ruby/object:Gem::Requirement
|
@@ -123,7 +123,7 @@ dependencies:
|
|
123
123
|
- !ruby/object:Gem::Version
|
124
124
|
version: '0'
|
125
125
|
- !ruby/object:Gem::Dependency
|
126
|
-
name:
|
126
|
+
name: appraisal
|
127
127
|
requirement: !ruby/object:Gem::Requirement
|
128
128
|
requirements:
|
129
129
|
- - ">="
|
@@ -145,11 +145,16 @@ extra_rdoc_files: []
|
|
145
145
|
files:
|
146
146
|
- ".gitignore"
|
147
147
|
- ".travis.yml"
|
148
|
+
- Appraisals
|
148
149
|
- CHANGELOG.md
|
149
150
|
- Gemfile
|
150
151
|
- LICENSE.txt
|
151
152
|
- README.md
|
152
153
|
- Rakefile
|
154
|
+
- gemfiles/sidekiq_2.gemfile
|
155
|
+
- gemfiles/sidekiq_3.gemfile
|
156
|
+
- gemfiles/sidekiq_4.gemfile
|
157
|
+
- gemfiles/sidekiq_5.gemfile
|
153
158
|
- lib/sidekiq-lock.rb
|
154
159
|
- lib/sidekiq/lock.rb
|
155
160
|
- lib/sidekiq/lock/middleware.rb
|
@@ -158,6 +163,7 @@ files:
|
|
158
163
|
- lib/sidekiq/lock/version.rb
|
159
164
|
- lib/sidekiq/lock/worker.rb
|
160
165
|
- sidekiq-lock.gemspec
|
166
|
+
- test/lib/lock_test.rb
|
161
167
|
- test/lib/middleware_test.rb
|
162
168
|
- test/lib/redis_lock_test.rb
|
163
169
|
- test/lib/testing/inline_test.rb
|
@@ -184,11 +190,12 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
184
190
|
version: '0'
|
185
191
|
requirements: []
|
186
192
|
rubyforge_project:
|
187
|
-
rubygems_version: 2.
|
193
|
+
rubygems_version: 2.7.6
|
188
194
|
signing_key:
|
189
195
|
specification_version: 4
|
190
196
|
summary: Simple redis-based lock mechanism for your sidekiq workers
|
191
197
|
test_files:
|
198
|
+
- test/lib/lock_test.rb
|
192
199
|
- test/lib/middleware_test.rb
|
193
200
|
- test/lib/redis_lock_test.rb
|
194
201
|
- test/lib/testing/inline_test.rb
|