sidekiq-lock 0.2.0 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/CHANGELOG.md +29 -2
- data/README.md +55 -12
- data/lib/sidekiq/lock.rb +14 -6
- data/lib/sidekiq/lock/container.rb +15 -0
- data/lib/sidekiq/lock/middleware.rb +7 -9
- data/lib/sidekiq/lock/redis_lock.rb +35 -26
- data/lib/sidekiq/lock/testing/inline.rb +2 -2
- data/lib/sidekiq/lock/version.rb +1 -1
- data/lib/sidekiq/lock/worker.rb +1 -3
- data/test/lib/container_test.rb +23 -0
- data/test/lib/lock_test.rb +29 -0
- data/test/lib/middleware_test.rb +10 -12
- data/test/lib/redis_lock_test.rb +16 -2
- data/test/lib/testing/inline_test.rb +9 -7
- data/test/lib/worker_test.rb +49 -12
- data/test/test_helper.rb +14 -17
- data/test/test_workers.rb +2 -0
- metadata +51 -59
- data/.gitignore +0 -20
- data/.travis.yml +0 -13
- data/Gemfile +0 -8
- data/sidekiq-lock.gemspec +0 -28
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 592d4bf95b4bbd2cba2444898c5f6843c0a9112c6c45e018999af361b8a5ca73
|
4
|
+
data.tar.gz: 7b0a5614796a292af28fc0880915a76ac85ecfb8c669b07d57d5019d8f246342
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: ff115e3d009c6b6803e20326c2d84bf2286d9e4b652fe840b743596b8257919d7c54b3f1b9e1dcf2f9f1cba16af6541490464a77d55b52ff1b9407f28a7981f3
|
7
|
+
data.tar.gz: 0cf165b35136db26001b236837dcd7a831867fc81d8b81a3e14c2efe76edf41ade9cb5ba437b311b9ec1626af5aae176a3a09fde0c393ef5b949f2f5e5f8fa64
|
data/CHANGELOG.md
CHANGED
@@ -1,4 +1,31 @@
|
|
1
|
-
## 0.
|
1
|
+
## 0.5.0 (August 13, 2021)
|
2
|
+
|
3
|
+
- maintenance - test on latest ruby versions (remove outdated rubies from build), add sidekiq 6 to build matrix, remove coveralls
|
4
|
+
- fix for ruby 3 (thanks to [@ak15](https://github.com/ak15))
|
5
|
+
|
6
|
+
## 0.4.0 (July 18, 2018)
|
7
|
+
|
8
|
+
- make lock container configurable (non breaking change) - in case you would like to something else than `Thread.current` - now you easily can
|
9
|
+
|
10
|
+
## 0.3.1 (March 3, 2018)
|
11
|
+
|
12
|
+
- do not assume `ActiveSupport` is loaded / or old `Sidekiq` patches are present (add own symbolize keys logic)
|
13
|
+
- 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!)
|
14
|
+
- run test on travis for sidekiq `2.17`, `3.5`, `4.2` and `>= 5.1` and all newest rubies (`2.2` - `2.5`)
|
15
|
+
|
16
|
+
## 0.3.0 (July 28, 2016)
|
17
|
+
|
18
|
+
- ability to set custom lock value. Works just like setting timeout and name, handles procs as well (thanks to [@piokaczm](https://github.com/piokaczm))
|
19
|
+
|
20
|
+
``` ruby
|
21
|
+
sidekiq_options lock: {
|
22
|
+
timeout: timeout,
|
23
|
+
name: name,
|
24
|
+
value: custom_value
|
25
|
+
}
|
26
|
+
```
|
27
|
+
|
28
|
+
## 0.2.0 (December 08, 2013)
|
2
29
|
|
3
30
|
- ability to globally configure `lock` method name
|
4
31
|
|
@@ -18,6 +45,6 @@ end
|
|
18
45
|
That will setup `RedisLock` under proper thread variable.
|
19
46
|
This can be handy if you test your workers inline (without full stack middleware)
|
20
47
|
|
21
|
-
## 0.0.1
|
48
|
+
## 0.0.1 (October 14, 2013)
|
22
49
|
|
23
50
|
- Initial release
|
data/README.md
CHANGED
@@ -1,14 +1,19 @@
|
|
1
|
+
<p align="center">
|
2
|
+
<img width="300" height="210" src="https://github.com/emq/sidekiq-lock/raw/master/logo.png">
|
3
|
+
</p>
|
4
|
+
|
1
5
|
# Sidekiq::Lock
|
2
6
|
|
3
7
|
[![Code Climate](https://codeclimate.com/github/emq/sidekiq-lock.png)](https://codeclimate.com/github/emq/sidekiq-lock)
|
4
|
-
[![Build Status](https://travis-ci.
|
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)
|
8
|
+
[![Build Status](https://travis-ci.com/rwojsznis/sidekiq-lock.svg?branch=master)](https://travis-ci.com/rwojsznis/sidekiq-lock)
|
7
9
|
[![Gem Version](https://badge.fury.io/rb/sidekiq-lock.png)](http://badge.fury.io/rb/sidekiq-lock)
|
8
10
|
|
11
|
+
**Note:** This is a _complete_ piece of software, it should work across all future sidekiq & ruby versions.
|
12
|
+
|
9
13
|
Redis-based simple locking mechanism for [sidekiq][2]. Uses [SET command][1] introduced in Redis 2.6.16.
|
10
14
|
|
11
|
-
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
|
15
|
+
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
|
16
|
+
time - it provides a `lock` method that you can use in whatever way you want.
|
12
17
|
|
13
18
|
## Installation
|
14
19
|
|
@@ -32,7 +37,8 @@ $ bundle
|
|
32
37
|
|
33
38
|
Sidekiq-lock is a middleware/module combination, let me go through my thought process here :).
|
34
39
|
|
35
|
-
In your worker class include `Sidekiq::Lock::Worker` module and provide `lock` attribute inside `sidekiq_options`,
|
40
|
+
In your worker class include `Sidekiq::Lock::Worker` module and provide `lock` attribute inside `sidekiq_options`,
|
41
|
+
for example:
|
36
42
|
|
37
43
|
``` ruby
|
38
44
|
class Worker
|
@@ -50,15 +56,19 @@ end
|
|
50
56
|
|
51
57
|
What will happen is:
|
52
58
|
|
53
|
-
- middleware will setup a `Sidekiq::Lock::RedisLock` object under `Thread.current[Sidekiq::Lock::THREAD_KEY]`
|
59
|
+
- middleware will setup a `Sidekiq::Lock::RedisLock` object under `Thread.current[Sidekiq::Lock::THREAD_KEY]`
|
60
|
+
(it should work in most use cases without any problems - but it's configurable, more below) - assuming you provided
|
61
|
+
`lock` options, otherwise it will do nothing, just execute your worker's code
|
54
62
|
|
55
|
-
- `Sidekiq::Lock::Worker` module provides a `lock` method that just simply points to that thread variable, just as
|
63
|
+
- `Sidekiq::Lock::Worker` module provides a `lock` method that just simply points to that thread variable, just as
|
64
|
+
a convenience
|
56
65
|
|
57
66
|
So now in your worker class you can call (whenever you need):
|
58
67
|
|
59
|
-
- `lock.acquire!` - will try to acquire the lock, if returns false on failure (that means some other process / thread
|
68
|
+
- `lock.acquire!` - will try to acquire the lock, if returns false on failure (that means some other process / thread
|
69
|
+
took the lock first)
|
60
70
|
- `lock.acquired?` - set to `true` when lock is successfully acquired
|
61
|
-
- `lock.release!` - deletes the lock (if
|
71
|
+
- `lock.release!` - deletes the lock (only if it's: acquired by current thread and not already expired)
|
62
72
|
|
63
73
|
### Lock options
|
64
74
|
|
@@ -66,6 +76,7 @@ sidekiq_options lock will accept static values or `Proc` that will be called on
|
|
66
76
|
|
67
77
|
- timeout - specified expire time, in milliseconds
|
68
78
|
- name - name of the redis key that will be used as lock name
|
79
|
+
- value - (optional) value of the lock, if not provided it's set to random hex
|
69
80
|
|
70
81
|
Dynamic lock example:
|
71
82
|
|
@@ -75,7 +86,8 @@ class Worker
|
|
75
86
|
include Sidekiq::Lock::Worker
|
76
87
|
sidekiq_options lock: {
|
77
88
|
timeout: proc { |user_id, timeout| timeout * 2 },
|
78
|
-
name: proc { |user_id, timeout| "lock:peruser:#{user_id}" }
|
89
|
+
name: proc { |user_id, timeout| "lock:peruser:#{user_id}" },
|
90
|
+
value: proc { |user_id, timeout| "#{user_id}" }
|
79
91
|
}
|
80
92
|
|
81
93
|
def perform(user_id, timeout)
|
@@ -83,7 +95,15 @@ class Worker
|
|
83
95
|
# do some work
|
84
96
|
# only at this point I want to acquire the lock
|
85
97
|
if lock.acquire!
|
86
|
-
|
98
|
+
begin
|
99
|
+
# I can do the work
|
100
|
+
# ...
|
101
|
+
ensure
|
102
|
+
# You probably want to manually release lock after work is done
|
103
|
+
# This method can be safely called even if lock wasn't acquired
|
104
|
+
# by current worker (thread). For more references see RedisLock class
|
105
|
+
lock.release!
|
106
|
+
end
|
87
107
|
else
|
88
108
|
# reschedule, raise an error or do whatever you want
|
89
109
|
end
|
@@ -103,9 +123,32 @@ Sidekiq.configure_server do |config|
|
|
103
123
|
end
|
104
124
|
```
|
105
125
|
|
126
|
+
### Customizing lock _container_
|
127
|
+
|
128
|
+
If you would like to change default behavior of storing lock instance in `Thread.current` for whatever reason you
|
129
|
+
can do that as well via server configuration:
|
130
|
+
|
131
|
+
``` ruby
|
132
|
+
# Any thread-safe class that implements .fetch and .store methods will do
|
133
|
+
class CustomStorage
|
134
|
+
def fetch
|
135
|
+
# returns stored lock instance
|
136
|
+
end
|
137
|
+
|
138
|
+
def store(lock_instance)
|
139
|
+
# store lock
|
140
|
+
end
|
141
|
+
end
|
142
|
+
|
143
|
+
Sidekiq.configure_server do |config|
|
144
|
+
config.lock_container = CustomStorage.new
|
145
|
+
end
|
146
|
+
```
|
147
|
+
|
106
148
|
### Inline testing
|
107
149
|
|
108
|
-
As you know middleware is not invoked when testing jobs inline, you can require in your test/spec helper file
|
150
|
+
As you know middleware is not invoked when testing jobs inline, you can require in your test/spec helper file
|
151
|
+
`sidekiq/lock/testing/inline` to include two methods that will help you setting / clearing up lock manually:
|
109
152
|
|
110
153
|
- `set_sidekiq_lock(worker_class, payload)` - note: payload should be an array of worker arguments
|
111
154
|
- `clear_sidekiq_lock`
|
data/lib/sidekiq/lock.rb
CHANGED
@@ -1,12 +1,20 @@
|
|
1
|
-
require
|
2
|
-
require
|
3
|
-
require
|
4
|
-
require
|
1
|
+
require 'sidekiq/lock/container'
|
2
|
+
require 'sidekiq/lock/middleware'
|
3
|
+
require 'sidekiq/lock/redis_lock'
|
4
|
+
require 'sidekiq/lock/version'
|
5
|
+
require 'sidekiq/lock/worker'
|
5
6
|
|
6
7
|
module Sidekiq
|
8
|
+
def self.lock_container
|
9
|
+
@lock_container ||= Lock::Container.new
|
10
|
+
end
|
7
11
|
|
8
12
|
def self.lock_method
|
9
|
-
@lock_method
|
13
|
+
@lock_method ||= Lock::METHOD_NAME
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.lock_container=(container)
|
17
|
+
@lock_container = container
|
10
18
|
end
|
11
19
|
|
12
20
|
def self.lock_method=(method)
|
@@ -15,6 +23,7 @@ module Sidekiq
|
|
15
23
|
|
16
24
|
module Lock
|
17
25
|
THREAD_KEY = :sidekiq_lock
|
26
|
+
METHOD_NAME = :lock
|
18
27
|
end
|
19
28
|
end
|
20
29
|
|
@@ -23,4 +32,3 @@ Sidekiq.configure_server do |config|
|
|
23
32
|
chain.add Sidekiq::Lock::Middleware
|
24
33
|
end
|
25
34
|
end
|
26
|
-
|
@@ -1,8 +1,7 @@
|
|
1
1
|
module Sidekiq
|
2
2
|
module Lock
|
3
3
|
class Middleware
|
4
|
-
|
5
|
-
def call(worker, msg, queue)
|
4
|
+
def call(worker, msg, _queue)
|
6
5
|
options = lock_options(worker)
|
7
6
|
setup_lock(options, msg['args']) unless options.nil?
|
8
7
|
|
@@ -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
|
+
Sidekiq.lock_container.store(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?
|
@@ -21,7 +23,7 @@ module Sidekiq
|
|
21
23
|
# this also requires redis-rb >= 3.0.5
|
22
24
|
def acquire!
|
23
25
|
@acquired ||= Sidekiq.redis do |r|
|
24
|
-
r.set(name, value,
|
26
|
+
r.set(name, value, nx: true, px: timeout)
|
25
27
|
end
|
26
28
|
end
|
27
29
|
|
@@ -29,44 +31,51 @@ module Sidekiq
|
|
29
31
|
Sidekiq.redis do |r|
|
30
32
|
begin
|
31
33
|
r.evalsha redis_lock_script_sha, keys: [name], argv: [value]
|
32
|
-
|
33
|
-
|
34
|
+
rescue Redis::CommandError
|
35
|
+
r.eval redis_lock_script, keys: [name], argv: [value]
|
34
36
|
end
|
35
37
|
end
|
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
|
+
|
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
|
70
79
|
end
|
71
80
|
end
|
72
81
|
end
|
@@ -1,8 +1,8 @@
|
|
1
1
|
def set_sidekiq_lock(worker_class, payload)
|
2
2
|
options = worker_class.get_sidekiq_options['lock']
|
3
|
-
|
3
|
+
Sidekiq.lock_container.store(Sidekiq::Lock::RedisLock.new(options, payload))
|
4
4
|
end
|
5
5
|
|
6
6
|
def clear_sidekiq_lock
|
7
|
-
|
7
|
+
Sidekiq.lock_container.store(nil)
|
8
8
|
end
|
data/lib/sidekiq/lock/version.rb
CHANGED
data/lib/sidekiq/lock/worker.rb
CHANGED
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
require 'open3'
|
3
|
+
|
4
|
+
module Sidekiq
|
5
|
+
module Lock
|
6
|
+
describe Container do
|
7
|
+
it 'stores and fetches given value under Thread.current' do
|
8
|
+
begin
|
9
|
+
container = Sidekiq::Lock::Container.new
|
10
|
+
thread_key = Sidekiq::Lock::Container::THREAD_KEY
|
11
|
+
|
12
|
+
Thread.current[thread_key] = 'value'
|
13
|
+
assert_equal 'value', container.fetch
|
14
|
+
|
15
|
+
container.store 'new-value'
|
16
|
+
assert_equal Thread.current[thread_key], 'new-value'
|
17
|
+
ensure
|
18
|
+
Thread.current[thread_key] = nil
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
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
@@ -1,42 +1,40 @@
|
|
1
|
-
require
|
1
|
+
require 'test_helper'
|
2
2
|
|
3
3
|
module Sidekiq
|
4
4
|
module Lock
|
5
5
|
describe Middleware do
|
6
|
-
|
7
6
|
before do
|
8
7
|
Sidekiq.redis = REDIS
|
9
8
|
Sidekiq.redis { |c| c.flushdb }
|
10
|
-
|
9
|
+
reset_lock_variable!
|
11
10
|
end
|
12
11
|
|
13
|
-
let(:handler){ Sidekiq::Lock::Middleware.new }
|
12
|
+
let(:handler) { Sidekiq::Lock::Middleware.new }
|
14
13
|
|
15
14
|
it 'sets lock variable with provided static lock options' do
|
16
|
-
handler.call(LockWorker.new, {'class' => LockWorker, 'args' => []}, 'default') do
|
15
|
+
handler.call(LockWorker.new, { 'class' => LockWorker, 'args' => [] }, 'default') do
|
17
16
|
true
|
18
17
|
end
|
19
18
|
|
20
|
-
assert_kind_of RedisLock,
|
19
|
+
assert_kind_of RedisLock, lock_container_variable
|
21
20
|
end
|
22
21
|
|
23
22
|
it 'sets lock variable with provided dynamic options' do
|
24
|
-
handler.call(DynamicLockWorker.new, {'class' => DynamicLockWorker, 'args' => [1234, 1000]}, 'default') do
|
23
|
+
handler.call(DynamicLockWorker.new, { 'class' => DynamicLockWorker, 'args' => [1234, 1000] }, 'default') do
|
25
24
|
true
|
26
25
|
end
|
27
26
|
|
28
|
-
assert_equal "lock:1234",
|
29
|
-
assert_equal 2000,
|
27
|
+
assert_equal "lock:1234", lock_container_variable.name
|
28
|
+
assert_equal 2000, lock_container_variable.timeout
|
30
29
|
end
|
31
30
|
|
32
31
|
it 'sets nothing for workers without lock options' do
|
33
|
-
handler.call(RegularWorker.new, {'class' => RegularWorker, 'args' => []}, 'default') do
|
32
|
+
handler.call(RegularWorker.new, { 'class' => RegularWorker, 'args' => [] }, 'default') do
|
34
33
|
true
|
35
34
|
end
|
36
35
|
|
37
|
-
assert_nil
|
36
|
+
assert_nil lock_container_variable
|
38
37
|
end
|
39
|
-
|
40
38
|
end
|
41
39
|
end
|
42
40
|
end
|
data/test/lib/redis_lock_test.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
require
|
1
|
+
require 'test_helper'
|
2
2
|
|
3
3
|
module Sidekiq
|
4
4
|
module Lock
|
@@ -40,11 +40,15 @@ module Sidekiq
|
|
40
40
|
it "can accept block as arguments" do
|
41
41
|
lock = RedisLock.new({
|
42
42
|
'timeout' => proc { |options| options['timeout'] * 2 },
|
43
|
-
'name' => proc { |options| "#{options['test']}-sidekiq" }
|
43
|
+
'name' => proc { |options| "#{options['test']}-sidekiq" },
|
44
|
+
'value' => proc { |options| "#{options['test']}-sidekiq" }
|
44
45
|
}, ['timeout' => 500, 'test' => 'hello'])
|
45
46
|
|
46
47
|
assert_equal 1000, lock.timeout
|
47
48
|
assert_equal 'hello-sidekiq', lock.name
|
49
|
+
lock.acquire!
|
50
|
+
assert_equal 'hello-sidekiq', redis("get", lock.name)
|
51
|
+
lock.release!
|
48
52
|
end
|
49
53
|
|
50
54
|
it "can acquire a lock" do
|
@@ -84,6 +88,16 @@ module Sidekiq
|
|
84
88
|
|
85
89
|
assert_equal new_lock_value, redis("get", "test-lock")
|
86
90
|
end
|
91
|
+
|
92
|
+
it "releases taken lock" do
|
93
|
+
custom_args = [args.first.merge('value' => 'custom_value'), []]
|
94
|
+
lock = RedisLock.new(*custom_args)
|
95
|
+
lock.acquire!
|
96
|
+
assert redis("get", "test-lock")
|
97
|
+
|
98
|
+
lock.release!
|
99
|
+
assert_nil redis("get", "test-lock")
|
100
|
+
end
|
87
101
|
end
|
88
102
|
end
|
89
103
|
end
|
@@ -2,21 +2,23 @@ require "test_helper"
|
|
2
2
|
require "sidekiq/lock/testing/inline"
|
3
3
|
|
4
4
|
describe "inline test helper" do
|
5
|
-
|
6
|
-
after { set_lock_variable! }
|
5
|
+
after { reset_lock_variable! }
|
7
6
|
|
8
7
|
it "has helper fuction for setting lock" do
|
9
|
-
Sidekiq::Lock::RedisLock
|
8
|
+
Sidekiq::Lock::RedisLock
|
9
|
+
.expects(:new)
|
10
|
+
.with({ timeout: 1, name: 'lock-worker' }, 'worker argument')
|
11
|
+
.returns('lock set')
|
12
|
+
|
10
13
|
set_sidekiq_lock(LockWorker, 'worker argument')
|
11
|
-
assert_equal 'lock set',
|
14
|
+
assert_equal 'lock set', lock_container_variable
|
12
15
|
end
|
13
16
|
|
14
17
|
it "has helper fuction for clearing lock" do
|
15
18
|
set_lock_variable! "test"
|
16
|
-
assert_equal "test",
|
19
|
+
assert_equal "test", lock_container_variable
|
17
20
|
|
18
21
|
clear_sidekiq_lock
|
19
|
-
assert_nil
|
22
|
+
assert_nil lock_container_variable
|
20
23
|
end
|
21
|
-
|
22
24
|
end
|
data/test/lib/worker_test.rb
CHANGED
@@ -1,29 +1,66 @@
|
|
1
|
-
require
|
1
|
+
require 'test_helper'
|
2
2
|
|
3
3
|
module Sidekiq
|
4
4
|
module Lock
|
5
5
|
describe Worker do
|
6
|
+
# after { }
|
6
7
|
|
7
|
-
|
8
|
+
class CustomContainer
|
9
|
+
def initialize
|
10
|
+
@lock = nil
|
11
|
+
end
|
12
|
+
|
13
|
+
def fetch
|
14
|
+
@lock
|
15
|
+
end
|
8
16
|
|
9
|
-
|
10
|
-
|
11
|
-
|
17
|
+
def store(lock)
|
18
|
+
@lock = lock
|
19
|
+
end
|
12
20
|
end
|
13
21
|
|
22
|
+
# it 'sets lock method that points to thread variable' do
|
23
|
+
# set_lock_variable! "test"
|
24
|
+
# assert_equal "test", LockWorker.new.lock
|
25
|
+
# end
|
26
|
+
|
14
27
|
it 'allows method name configuration' do
|
15
|
-
|
28
|
+
begin
|
29
|
+
Sidekiq.lock_method = :custom_lock_name
|
16
30
|
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
31
|
+
class WorkerWithCustomLockName
|
32
|
+
include Sidekiq::Worker
|
33
|
+
include Sidekiq::Lock::Worker
|
34
|
+
end
|
35
|
+
|
36
|
+
set_lock_variable! "custom_name"
|
21
37
|
|
22
|
-
|
38
|
+
assert_equal "custom_name", WorkerWithCustomLockName.new.custom_lock_name
|
23
39
|
|
24
|
-
|
40
|
+
reset_lock_variable!
|
41
|
+
ensure
|
42
|
+
|
43
|
+
Sidekiq.lock_method = Sidekiq::Lock::METHOD_NAME
|
44
|
+
end
|
25
45
|
end
|
26
46
|
|
47
|
+
it 'allows container configuration' do
|
48
|
+
begin
|
49
|
+
container = CustomContainer.new
|
50
|
+
Sidekiq.lock_container = container
|
51
|
+
|
52
|
+
class WorkerWithCustomContainer
|
53
|
+
include Sidekiq::Worker
|
54
|
+
include Sidekiq::Lock::Worker
|
55
|
+
end
|
56
|
+
|
57
|
+
container.store "lock-variable"
|
58
|
+
|
59
|
+
assert_equal "lock-variable", WorkerWithCustomContainer.new.lock
|
60
|
+
ensure
|
61
|
+
Sidekiq.lock_container = Sidekiq::Lock::Container.new
|
62
|
+
end
|
63
|
+
end
|
27
64
|
end
|
28
65
|
end
|
29
66
|
end
|
data/test/test_helper.rb
CHANGED
@@ -1,20 +1,13 @@
|
|
1
|
-
require '
|
2
|
-
|
1
|
+
require 'minitest/autorun'
|
2
|
+
require 'minitest/pride'
|
3
|
+
require 'mocha/setup'
|
3
4
|
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
require "minitest/autorun"
|
8
|
-
require "minitest/pride"
|
9
|
-
require "mocha/setup"
|
10
|
-
|
11
|
-
require "sidekiq"
|
12
|
-
require "sidekiq-lock"
|
13
|
-
require "test_workers"
|
5
|
+
require 'sidekiq'
|
6
|
+
require 'test_workers'
|
14
7
|
|
15
8
|
Sidekiq.logger.level = Logger::ERROR
|
16
9
|
|
17
|
-
REDIS = Sidekiq::RedisConnection.create(url:
|
10
|
+
REDIS = Sidekiq::RedisConnection.create(url: 'redis://localhost/15')
|
18
11
|
|
19
12
|
def redis(command, *args)
|
20
13
|
Sidekiq.redis do |c|
|
@@ -22,10 +15,14 @@ def redis(command, *args)
|
|
22
15
|
end
|
23
16
|
end
|
24
17
|
|
25
|
-
def set_lock_variable!(value
|
26
|
-
|
18
|
+
def set_lock_variable!(value)
|
19
|
+
Sidekiq.lock_container.store(value)
|
20
|
+
end
|
21
|
+
|
22
|
+
def reset_lock_variable!
|
23
|
+
set_lock_variable!(nil)
|
27
24
|
end
|
28
25
|
|
29
|
-
def
|
30
|
-
|
26
|
+
def lock_container_variable
|
27
|
+
Sidekiq.lock_container.fetch
|
31
28
|
end
|
data/test/test_workers.rb
CHANGED
metadata
CHANGED
@@ -1,128 +1,127 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sidekiq-lock
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
5
|
-
prerelease:
|
4
|
+
version: 0.5.0
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
7
|
- Rafal Wojsznis
|
9
8
|
autorequire:
|
10
9
|
bindir: bin
|
11
10
|
cert_chain: []
|
12
|
-
date:
|
11
|
+
date: 2021-08-13 00:00:00.000000000 Z
|
13
12
|
dependencies:
|
14
13
|
- !ruby/object:Gem::Dependency
|
15
14
|
name: sidekiq
|
16
15
|
requirement: !ruby/object:Gem::Requirement
|
17
|
-
none: false
|
18
16
|
requirements:
|
19
|
-
- -
|
17
|
+
- - ">="
|
20
18
|
- !ruby/object:Gem::Version
|
21
19
|
version: 2.14.0
|
22
20
|
type: :runtime
|
23
21
|
prerelease: false
|
24
22
|
version_requirements: !ruby/object:Gem::Requirement
|
25
|
-
none: false
|
26
23
|
requirements:
|
27
|
-
- -
|
24
|
+
- - ">="
|
28
25
|
- !ruby/object:Gem::Version
|
29
26
|
version: 2.14.0
|
30
27
|
- !ruby/object:Gem::Dependency
|
31
28
|
name: redis
|
32
29
|
requirement: !ruby/object:Gem::Requirement
|
33
|
-
none: false
|
34
30
|
requirements:
|
35
|
-
- -
|
31
|
+
- - ">="
|
36
32
|
- !ruby/object:Gem::Version
|
37
33
|
version: 3.0.5
|
38
34
|
type: :runtime
|
39
35
|
prerelease: false
|
40
36
|
version_requirements: !ruby/object:Gem::Requirement
|
41
|
-
none: false
|
42
37
|
requirements:
|
43
|
-
- -
|
38
|
+
- - ">="
|
44
39
|
- !ruby/object:Gem::Version
|
45
40
|
version: 3.0.5
|
46
41
|
- !ruby/object:Gem::Dependency
|
47
42
|
name: bundler
|
48
43
|
requirement: !ruby/object:Gem::Requirement
|
49
|
-
none: false
|
50
44
|
requirements:
|
51
|
-
- -
|
45
|
+
- - ">="
|
52
46
|
- !ruby/object:Gem::Version
|
53
|
-
version: '
|
47
|
+
version: '0'
|
54
48
|
type: :development
|
55
49
|
prerelease: false
|
56
50
|
version_requirements: !ruby/object:Gem::Requirement
|
57
|
-
none: false
|
58
51
|
requirements:
|
59
|
-
- -
|
52
|
+
- - ">="
|
60
53
|
- !ruby/object:Gem::Version
|
61
|
-
version: '
|
54
|
+
version: '0'
|
62
55
|
- !ruby/object:Gem::Dependency
|
63
56
|
name: rake
|
64
57
|
requirement: !ruby/object:Gem::Requirement
|
65
|
-
none: false
|
66
58
|
requirements:
|
67
|
-
- -
|
59
|
+
- - ">="
|
68
60
|
- !ruby/object:Gem::Version
|
69
61
|
version: '0'
|
70
62
|
type: :development
|
71
63
|
prerelease: false
|
72
64
|
version_requirements: !ruby/object:Gem::Requirement
|
73
|
-
none: false
|
74
65
|
requirements:
|
75
|
-
- -
|
66
|
+
- - ">="
|
76
67
|
- !ruby/object:Gem::Version
|
77
68
|
version: '0'
|
78
69
|
- !ruby/object:Gem::Dependency
|
79
70
|
name: rack-test
|
80
71
|
requirement: !ruby/object:Gem::Requirement
|
81
|
-
none: false
|
82
72
|
requirements:
|
83
|
-
- -
|
73
|
+
- - ">="
|
84
74
|
- !ruby/object:Gem::Version
|
85
75
|
version: '0'
|
86
76
|
type: :development
|
87
77
|
prerelease: false
|
88
78
|
version_requirements: !ruby/object:Gem::Requirement
|
89
|
-
none: false
|
90
79
|
requirements:
|
91
|
-
- -
|
80
|
+
- - ">="
|
92
81
|
- !ruby/object:Gem::Version
|
93
82
|
version: '0'
|
94
83
|
- !ruby/object:Gem::Dependency
|
95
|
-
name:
|
84
|
+
name: mocha
|
96
85
|
requirement: !ruby/object:Gem::Requirement
|
97
|
-
none: false
|
98
86
|
requirements:
|
99
|
-
- - ~>
|
87
|
+
- - "~>"
|
100
88
|
- !ruby/object:Gem::Version
|
101
|
-
version: 0.
|
89
|
+
version: 0.14.0
|
102
90
|
type: :development
|
103
91
|
prerelease: false
|
104
92
|
version_requirements: !ruby/object:Gem::Requirement
|
105
|
-
none: false
|
106
93
|
requirements:
|
107
|
-
- - ~>
|
94
|
+
- - "~>"
|
108
95
|
- !ruby/object:Gem::Version
|
109
|
-
version: 0.
|
96
|
+
version: 0.14.0
|
110
97
|
- !ruby/object:Gem::Dependency
|
111
|
-
name:
|
98
|
+
name: minitest
|
112
99
|
requirement: !ruby/object:Gem::Requirement
|
113
|
-
none: false
|
114
100
|
requirements:
|
115
|
-
- -
|
101
|
+
- - ">="
|
116
102
|
- !ruby/object:Gem::Version
|
117
|
-
version: 0
|
103
|
+
version: '0'
|
118
104
|
type: :development
|
119
105
|
prerelease: false
|
120
106
|
version_requirements: !ruby/object:Gem::Requirement
|
121
|
-
none: false
|
122
107
|
requirements:
|
123
|
-
- -
|
108
|
+
- - ">="
|
124
109
|
- !ruby/object:Gem::Version
|
125
|
-
version: 0
|
110
|
+
version: '0'
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: appraisal
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - ">="
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - ">="
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '0'
|
126
125
|
description: Simple redis-based lock mechanism for your sidekiq workers
|
127
126
|
email:
|
128
127
|
- rafal.wojsznis@gmail.com
|
@@ -130,21 +129,20 @@ executables: []
|
|
130
129
|
extensions: []
|
131
130
|
extra_rdoc_files: []
|
132
131
|
files:
|
133
|
-
- .gitignore
|
134
|
-
- .travis.yml
|
135
132
|
- CHANGELOG.md
|
136
|
-
- Gemfile
|
137
133
|
- LICENSE.txt
|
138
134
|
- README.md
|
139
135
|
- Rakefile
|
140
136
|
- lib/sidekiq-lock.rb
|
141
137
|
- lib/sidekiq/lock.rb
|
138
|
+
- lib/sidekiq/lock/container.rb
|
142
139
|
- lib/sidekiq/lock/middleware.rb
|
143
140
|
- lib/sidekiq/lock/redis_lock.rb
|
144
141
|
- lib/sidekiq/lock/testing/inline.rb
|
145
142
|
- lib/sidekiq/lock/version.rb
|
146
143
|
- lib/sidekiq/lock/worker.rb
|
147
|
-
-
|
144
|
+
- test/lib/container_test.rb
|
145
|
+
- test/lib/lock_test.rb
|
148
146
|
- test/lib/middleware_test.rb
|
149
147
|
- test/lib/redis_lock_test.rb
|
150
148
|
- test/lib/testing/inline_test.rb
|
@@ -154,38 +152,32 @@ files:
|
|
154
152
|
homepage: https://github.com/emq/sidekiq-lock
|
155
153
|
licenses:
|
156
154
|
- MIT
|
155
|
+
metadata: {}
|
157
156
|
post_install_message:
|
158
157
|
rdoc_options: []
|
159
158
|
require_paths:
|
160
159
|
- lib
|
161
160
|
required_ruby_version: !ruby/object:Gem::Requirement
|
162
|
-
none: false
|
163
161
|
requirements:
|
164
|
-
- -
|
162
|
+
- - ">="
|
165
163
|
- !ruby/object:Gem::Version
|
166
164
|
version: '0'
|
167
|
-
segments:
|
168
|
-
- 0
|
169
|
-
hash: -2239756141932312381
|
170
165
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
171
|
-
none: false
|
172
166
|
requirements:
|
173
|
-
- -
|
167
|
+
- - ">="
|
174
168
|
- !ruby/object:Gem::Version
|
175
169
|
version: '0'
|
176
|
-
segments:
|
177
|
-
- 0
|
178
|
-
hash: -2239756141932312381
|
179
170
|
requirements: []
|
180
|
-
|
181
|
-
rubygems_version: 1.8.25
|
171
|
+
rubygems_version: 3.2.5
|
182
172
|
signing_key:
|
183
|
-
specification_version:
|
173
|
+
specification_version: 4
|
184
174
|
summary: Simple redis-based lock mechanism for your sidekiq workers
|
185
175
|
test_files:
|
186
|
-
- test/
|
176
|
+
- test/test_workers.rb
|
177
|
+
- test/lib/worker_test.rb
|
187
178
|
- test/lib/redis_lock_test.rb
|
188
179
|
- test/lib/testing/inline_test.rb
|
189
|
-
- test/lib/
|
180
|
+
- test/lib/middleware_test.rb
|
181
|
+
- test/lib/lock_test.rb
|
182
|
+
- test/lib/container_test.rb
|
190
183
|
- test/test_helper.rb
|
191
|
-
- test/test_workers.rb
|
data/.gitignore
DELETED
data/.travis.yml
DELETED
data/Gemfile
DELETED
@@ -1,8 +0,0 @@
|
|
1
|
-
source "https://rubygems.org"
|
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/sidekiq-lock.gemspec
DELETED
@@ -1,28 +0,0 @@
|
|
1
|
-
# coding: utf-8
|
2
|
-
lib = File.expand_path('../lib', __FILE__)
|
3
|
-
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
-
require 'sidekiq/lock/version'
|
5
|
-
|
6
|
-
Gem::Specification.new do |spec|
|
7
|
-
spec.name = "sidekiq-lock"
|
8
|
-
spec.version = Sidekiq::Lock::VERSION
|
9
|
-
spec.authors = ["Rafal Wojsznis"]
|
10
|
-
spec.email = ["rafal.wojsznis@gmail.com"]
|
11
|
-
spec.description = spec.summary = "Simple redis-based lock mechanism for your sidekiq workers"
|
12
|
-
spec.homepage = "https://github.com/emq/sidekiq-lock"
|
13
|
-
spec.license = "MIT"
|
14
|
-
|
15
|
-
spec.files = `git ls-files`.split($/)
|
16
|
-
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
17
|
-
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
18
|
-
spec.require_paths = ["lib"]
|
19
|
-
|
20
|
-
spec.add_dependency "sidekiq", ">= 2.14.0"
|
21
|
-
spec.add_dependency "redis", ">= 3.0.5"
|
22
|
-
|
23
|
-
spec.add_development_dependency "bundler", "~> 1.3"
|
24
|
-
spec.add_development_dependency "rake"
|
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"
|
28
|
-
end
|