sidekiq-lock 0.0.1 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.travis.yml +5 -0
- data/CHANGELOG.md +20 -0
- data/Gemfile +7 -3
- data/README.md +20 -0
- data/lib/sidekiq/lock.rb +12 -2
- data/lib/sidekiq/lock/testing/inline.rb +8 -0
- data/lib/sidekiq/lock/version.rb +1 -1
- data/lib/sidekiq/lock/worker.rb +4 -2
- data/sidekiq-lock.gemspec +2 -0
- data/test/lib/middleware_test.rb +5 -30
- data/test/lib/redis_lock_test.rb +5 -0
- data/test/lib/testing/inline_test.rb +22 -0
- data/test/lib/worker_test.rb +16 -1
- data/test/test_helper.rb +13 -0
- data/test/test_workers.rb +19 -0
- metadata +41 -4
data/.travis.yml
CHANGED
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,23 @@
|
|
1
|
+
## 0.2.0
|
2
|
+
|
3
|
+
- ability to globally configure `lock` method name
|
4
|
+
|
5
|
+
``` ruby
|
6
|
+
Sidekiq.configure_server do |config|
|
7
|
+
config.lock_method = :redis_lock
|
8
|
+
end
|
9
|
+
```
|
10
|
+
|
11
|
+
- added inline test helper, by requiring `sidekiq/lock/testing/inline`
|
12
|
+
you will have access to two methods:
|
13
|
+
|
14
|
+
- `set_sidekiq_lock(worker_class, payload)`
|
15
|
+
|
16
|
+
- `clear_sidekiq_lock`
|
17
|
+
|
18
|
+
That will setup `RedisLock` under proper thread variable.
|
19
|
+
This can be handy if you test your workers inline (without full stack middleware)
|
20
|
+
|
1
21
|
## 0.0.1
|
2
22
|
|
3
23
|
- Initial release
|
data/Gemfile
CHANGED
@@ -1,4 +1,8 @@
|
|
1
|
-
source
|
2
|
-
|
3
|
-
# Specify your gem's dependencies in sidekiq-lock.gemspec
|
1
|
+
source "https://rubygems.org"
|
4
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
@@ -2,6 +2,9 @@
|
|
2
2
|
|
3
3
|
[![Code Climate](https://codeclimate.com/github/emq/sidekiq-lock.png)](https://codeclimate.com/github/emq/sidekiq-lock)
|
4
4
|
[![Build Status](https://travis-ci.org/emq/sidekiq-lock.png?branch=master)](https://travis-ci.org/emq/sidekiq-lock)
|
5
|
+
[![Coverage Status](https://coveralls.io/repos/emq/sidekiq-lock/badge.png)](https://coveralls.io/r/emq/sidekiq-lock)
|
6
|
+
[![Dependency Status](https://gemnasium.com/emq/sidekiq-lock.png)](https://gemnasium.com/emq/sidekiq-lock)
|
7
|
+
[![Gem Version](https://badge.fury.io/rb/sidekiq-lock.png)](http://badge.fury.io/rb/sidekiq-lock)
|
5
8
|
|
6
9
|
Redis-based simple locking mechanism for [sidekiq][2]. Uses [SET command][1] introduced in Redis 2.6.16.
|
7
10
|
|
@@ -90,6 +93,23 @@ end
|
|
90
93
|
|
91
94
|
Just be sure to provide valid redis key as a lock name.
|
92
95
|
|
96
|
+
### Customizing lock method name
|
97
|
+
|
98
|
+
You can change `lock` to something else (globally) in sidekiq server configuration:
|
99
|
+
|
100
|
+
``` ruby
|
101
|
+
Sidekiq.configure_server do |config|
|
102
|
+
config.lock_method = :redis_lock
|
103
|
+
end
|
104
|
+
```
|
105
|
+
|
106
|
+
### Inline testing
|
107
|
+
|
108
|
+
As you know middleware is not invoked when testing jobs inline, you can require in your test/spec helper file `sidekiq/lock/testing/inline` to include two methods that will help you setting / clearing up lock manually:
|
109
|
+
|
110
|
+
- `set_sidekiq_lock(worker_class, payload)` - note: payload should be an array of worker arguments
|
111
|
+
- `clear_sidekiq_lock`
|
112
|
+
|
93
113
|
## Contributing
|
94
114
|
|
95
115
|
1. Fork it
|
data/lib/sidekiq/lock.rb
CHANGED
@@ -1,9 +1,18 @@
|
|
1
|
-
require "sidekiq/lock/version"
|
2
|
-
require "sidekiq/lock/worker"
|
3
1
|
require "sidekiq/lock/middleware"
|
4
2
|
require "sidekiq/lock/redis_lock"
|
3
|
+
require "sidekiq/lock/version"
|
4
|
+
require "sidekiq/lock/worker"
|
5
5
|
|
6
6
|
module Sidekiq
|
7
|
+
|
8
|
+
def self.lock_method
|
9
|
+
@lock_method || :lock
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.lock_method=(method)
|
13
|
+
@lock_method = method
|
14
|
+
end
|
15
|
+
|
7
16
|
module Lock
|
8
17
|
THREAD_KEY = :sidekiq_lock
|
9
18
|
end
|
@@ -14,3 +23,4 @@ Sidekiq.configure_server do |config|
|
|
14
23
|
chain.add Sidekiq::Lock::Middleware
|
15
24
|
end
|
16
25
|
end
|
26
|
+
|
@@ -0,0 +1,8 @@
|
|
1
|
+
def set_sidekiq_lock(worker_class, payload)
|
2
|
+
options = worker_class.get_sidekiq_options['lock']
|
3
|
+
Thread.current[Sidekiq::Lock::THREAD_KEY] = Sidekiq::Lock::RedisLock.new(options, payload)
|
4
|
+
end
|
5
|
+
|
6
|
+
def clear_sidekiq_lock
|
7
|
+
Thread.current[Sidekiq::Lock::THREAD_KEY] = nil
|
8
|
+
end
|
data/lib/sidekiq/lock/version.rb
CHANGED
data/lib/sidekiq/lock/worker.rb
CHANGED
data/sidekiq-lock.gemspec
CHANGED
@@ -23,4 +23,6 @@ Gem::Specification.new do |spec|
|
|
23
23
|
spec.add_development_dependency "bundler", "~> 1.3"
|
24
24
|
spec.add_development_dependency "rake"
|
25
25
|
spec.add_development_dependency "rack-test"
|
26
|
+
spec.add_development_dependency "coveralls", "~> 0.7.0"
|
27
|
+
spec.add_development_dependency "mocha", "~> 0.14.0"
|
26
28
|
end
|
data/test/lib/middleware_test.rb
CHANGED
@@ -1,38 +1,13 @@
|
|
1
1
|
require "test_helper"
|
2
2
|
|
3
|
-
class LockWorker
|
4
|
-
include Sidekiq::Worker
|
5
|
-
include Sidekiq::Lock::Worker
|
6
|
-
sidekiq_options lock: { timeout: 1, name: 'lock-worker' }
|
7
|
-
end
|
8
|
-
|
9
|
-
class DynamicLockWorker
|
10
|
-
include Sidekiq::Worker
|
11
|
-
include Sidekiq::Lock::Worker
|
12
|
-
sidekiq_options lock: {
|
13
|
-
timeout: proc { |user_id, timeout| timeout*2 },
|
14
|
-
name: proc { |user_id, timeout| "lock:#{user_id}" }
|
15
|
-
}
|
16
|
-
end
|
17
|
-
|
18
|
-
class RegularWorker
|
19
|
-
include Sidekiq::Worker
|
20
|
-
include Sidekiq::Lock::Worker
|
21
|
-
end
|
22
|
-
|
23
|
-
|
24
3
|
module Sidekiq
|
25
4
|
module Lock
|
26
5
|
describe Middleware do
|
27
6
|
|
28
|
-
def thread_variable
|
29
|
-
Thread.current[Sidekiq::Lock::THREAD_KEY]
|
30
|
-
end
|
31
|
-
|
32
7
|
before do
|
33
8
|
Sidekiq.redis = REDIS
|
34
9
|
Sidekiq.redis { |c| c.flushdb }
|
35
|
-
|
10
|
+
set_lock_variable!
|
36
11
|
end
|
37
12
|
|
38
13
|
let(:handler){ Sidekiq::Lock::Middleware.new }
|
@@ -42,7 +17,7 @@ module Sidekiq
|
|
42
17
|
true
|
43
18
|
end
|
44
19
|
|
45
|
-
assert_kind_of RedisLock,
|
20
|
+
assert_kind_of RedisLock, lock_thread_variable
|
46
21
|
end
|
47
22
|
|
48
23
|
it 'sets lock variable with provided dynamic options' do
|
@@ -50,8 +25,8 @@ module Sidekiq
|
|
50
25
|
true
|
51
26
|
end
|
52
27
|
|
53
|
-
assert_equal "lock:1234",
|
54
|
-
assert_equal 2000,
|
28
|
+
assert_equal "lock:1234", lock_thread_variable.name
|
29
|
+
assert_equal 2000, lock_thread_variable.timeout
|
55
30
|
end
|
56
31
|
|
57
32
|
it 'sets nothing for workers without lock options' do
|
@@ -59,7 +34,7 @@ module Sidekiq
|
|
59
34
|
true
|
60
35
|
end
|
61
36
|
|
62
|
-
assert_nil
|
37
|
+
assert_nil lock_thread_variable
|
63
38
|
end
|
64
39
|
|
65
40
|
end
|
data/test/lib/redis_lock_test.rb
CHANGED
@@ -32,6 +32,11 @@ module Sidekiq
|
|
32
32
|
assert RedisLock.new({ 'timeout' => 500, 'name' => 'lock-name' }, [])
|
33
33
|
end
|
34
34
|
|
35
|
+
it "is released by default" do
|
36
|
+
lock = RedisLock.new({ 'timeout' => 500, 'name' => 'lock-name' }, [])
|
37
|
+
refute lock.acquired?
|
38
|
+
end
|
39
|
+
|
35
40
|
it "can accept block as arguments" do
|
36
41
|
lock = RedisLock.new({
|
37
42
|
'timeout' => proc { |options| options['timeout'] * 2 },
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require "test_helper"
|
2
|
+
require "sidekiq/lock/testing/inline"
|
3
|
+
|
4
|
+
describe "inline test helper" do
|
5
|
+
|
6
|
+
after { set_lock_variable! }
|
7
|
+
|
8
|
+
it "has helper fuction for setting lock" do
|
9
|
+
Sidekiq::Lock::RedisLock.expects(:new).with({ timeout: 1, name: 'lock-worker' }, 'worker argument').returns('lock set')
|
10
|
+
set_sidekiq_lock(LockWorker, 'worker argument')
|
11
|
+
assert_equal 'lock set', lock_thread_variable
|
12
|
+
end
|
13
|
+
|
14
|
+
it "has helper fuction for clearing lock" do
|
15
|
+
set_lock_variable! "test"
|
16
|
+
assert_equal "test", lock_thread_variable
|
17
|
+
|
18
|
+
clear_sidekiq_lock
|
19
|
+
assert_nil lock_thread_variable
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
data/test/lib/worker_test.rb
CHANGED
@@ -4,11 +4,26 @@ module Sidekiq
|
|
4
4
|
module Lock
|
5
5
|
describe Worker do
|
6
6
|
|
7
|
+
after { set_lock_variable! }
|
8
|
+
|
7
9
|
it 'sets lock method that points to thread variable' do
|
8
|
-
|
10
|
+
set_lock_variable! "test"
|
9
11
|
assert_equal "test", LockWorker.new.lock
|
10
12
|
end
|
11
13
|
|
14
|
+
it 'allows method name configuration' do
|
15
|
+
Sidekiq.lock_method = :custom_lock_name
|
16
|
+
|
17
|
+
class WorkerWithCustomLockName
|
18
|
+
include Sidekiq::Worker
|
19
|
+
include Sidekiq::Lock::Worker
|
20
|
+
end
|
21
|
+
|
22
|
+
set_lock_variable! "custom_name"
|
23
|
+
|
24
|
+
assert_equal "custom_name", WorkerWithCustomLockName.new.custom_lock_name
|
25
|
+
end
|
26
|
+
|
12
27
|
end
|
13
28
|
end
|
14
29
|
end
|
data/test/test_helper.rb
CHANGED
@@ -1,11 +1,16 @@
|
|
1
|
+
require 'coveralls'
|
2
|
+
Coveralls.wear!
|
3
|
+
|
1
4
|
Encoding.default_external = Encoding::UTF_8
|
2
5
|
Encoding.default_internal = Encoding::UTF_8
|
3
6
|
|
4
7
|
require "minitest/autorun"
|
5
8
|
require "minitest/pride"
|
9
|
+
require "mocha/setup"
|
6
10
|
|
7
11
|
require "sidekiq"
|
8
12
|
require "sidekiq-lock"
|
13
|
+
require "test_workers"
|
9
14
|
|
10
15
|
Sidekiq.logger.level = Logger::ERROR
|
11
16
|
|
@@ -16,3 +21,11 @@ def redis(command, *args)
|
|
16
21
|
c.send(command, *args)
|
17
22
|
end
|
18
23
|
end
|
24
|
+
|
25
|
+
def set_lock_variable!(value = nil)
|
26
|
+
Thread.current[Sidekiq::Lock::THREAD_KEY] = value
|
27
|
+
end
|
28
|
+
|
29
|
+
def lock_thread_variable
|
30
|
+
Thread.current[Sidekiq::Lock::THREAD_KEY]
|
31
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
class LockWorker
|
2
|
+
include Sidekiq::Worker
|
3
|
+
include Sidekiq::Lock::Worker
|
4
|
+
sidekiq_options lock: { timeout: 1, name: 'lock-worker' }
|
5
|
+
end
|
6
|
+
|
7
|
+
class DynamicLockWorker
|
8
|
+
include Sidekiq::Worker
|
9
|
+
include Sidekiq::Lock::Worker
|
10
|
+
sidekiq_options lock: {
|
11
|
+
timeout: proc { |user_id, timeout| timeout*2 },
|
12
|
+
name: proc { |user_id, timeout| "lock:#{user_id}" }
|
13
|
+
}
|
14
|
+
end
|
15
|
+
|
16
|
+
class RegularWorker
|
17
|
+
include Sidekiq::Worker
|
18
|
+
include Sidekiq::Lock::Worker
|
19
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sidekiq-lock
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.2.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-
|
12
|
+
date: 2013-12-08 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: sidekiq
|
@@ -91,6 +91,38 @@ dependencies:
|
|
91
91
|
- - ! '>='
|
92
92
|
- !ruby/object:Gem::Version
|
93
93
|
version: '0'
|
94
|
+
- !ruby/object:Gem::Dependency
|
95
|
+
name: coveralls
|
96
|
+
requirement: !ruby/object:Gem::Requirement
|
97
|
+
none: false
|
98
|
+
requirements:
|
99
|
+
- - ~>
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
version: 0.7.0
|
102
|
+
type: :development
|
103
|
+
prerelease: false
|
104
|
+
version_requirements: !ruby/object:Gem::Requirement
|
105
|
+
none: false
|
106
|
+
requirements:
|
107
|
+
- - ~>
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: 0.7.0
|
110
|
+
- !ruby/object:Gem::Dependency
|
111
|
+
name: mocha
|
112
|
+
requirement: !ruby/object:Gem::Requirement
|
113
|
+
none: false
|
114
|
+
requirements:
|
115
|
+
- - ~>
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: 0.14.0
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
none: false
|
122
|
+
requirements:
|
123
|
+
- - ~>
|
124
|
+
- !ruby/object:Gem::Version
|
125
|
+
version: 0.14.0
|
94
126
|
description: Simple redis-based lock mechanism for your sidekiq workers
|
95
127
|
email:
|
96
128
|
- rafal.wojsznis@gmail.com
|
@@ -109,13 +141,16 @@ files:
|
|
109
141
|
- lib/sidekiq/lock.rb
|
110
142
|
- lib/sidekiq/lock/middleware.rb
|
111
143
|
- lib/sidekiq/lock/redis_lock.rb
|
144
|
+
- lib/sidekiq/lock/testing/inline.rb
|
112
145
|
- lib/sidekiq/lock/version.rb
|
113
146
|
- lib/sidekiq/lock/worker.rb
|
114
147
|
- sidekiq-lock.gemspec
|
115
148
|
- test/lib/middleware_test.rb
|
116
149
|
- test/lib/redis_lock_test.rb
|
150
|
+
- test/lib/testing/inline_test.rb
|
117
151
|
- test/lib/worker_test.rb
|
118
152
|
- test/test_helper.rb
|
153
|
+
- test/test_workers.rb
|
119
154
|
homepage: https://github.com/emq/sidekiq-lock
|
120
155
|
licenses:
|
121
156
|
- MIT
|
@@ -131,7 +166,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
131
166
|
version: '0'
|
132
167
|
segments:
|
133
168
|
- 0
|
134
|
-
hash:
|
169
|
+
hash: -2239756141932312381
|
135
170
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
136
171
|
none: false
|
137
172
|
requirements:
|
@@ -140,7 +175,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
140
175
|
version: '0'
|
141
176
|
segments:
|
142
177
|
- 0
|
143
|
-
hash:
|
178
|
+
hash: -2239756141932312381
|
144
179
|
requirements: []
|
145
180
|
rubyforge_project:
|
146
181
|
rubygems_version: 1.8.25
|
@@ -150,5 +185,7 @@ summary: Simple redis-based lock mechanism for your sidekiq workers
|
|
150
185
|
test_files:
|
151
186
|
- test/lib/middleware_test.rb
|
152
187
|
- test/lib/redis_lock_test.rb
|
188
|
+
- test/lib/testing/inline_test.rb
|
153
189
|
- test/lib/worker_test.rb
|
154
190
|
- test/test_helper.rb
|
191
|
+
- test/test_workers.rb
|