sidekiq-lock 0.3.1 → 0.6.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 70c2ca23e60ff013252e111d7507b3665deaaadd31d23a6dc10e972315be335f
4
- data.tar.gz: 67cdd99cb1e60accad3304bc13f295051b54d09553ab278503d98240bf149d21
3
+ metadata.gz: 94c0d6993d3e6f8c2acd6a63e2e78464ac974369138f6c20eeb91525d0b5ea13
4
+ data.tar.gz: 75437ca38c53bc825d0e6e77b8dc2ef64c5c42bb8802746f5fbacf1f91c9e4f5
5
5
  SHA512:
6
- metadata.gz: 3595ae9803227cbb2051908bd7ecbbbbec409a57e5f41dbae552b838f5d71decf35dab1ade805ff851c806f7aa69896b0485018401fa41f5454e2638067133ca
7
- data.tar.gz: ea73c729c4da75a0a6c47c652376e96f5574ba2f2f93b09ecc6f12564d2d605d00e625247643cbb17462a3a0dca3049a5ed696882f53fe26ff59dda84be3f0a1
6
+ metadata.gz: 463adf7d4dbf5d468a1c795105a716d94fd6f77141e7c7f3d77a84076e4e222cc2b6fb57f64d5286bca6fc8b9491c4de891190fb3d255f1766acd879253220d0
7
+ data.tar.gz: c215cce1d79e99aaa28a04a5238aac165a8e5d36271a372b39067a80040109e5f64117114fdbfae227c5cb406f86cd46e554cfa6820978573abd1f08b04ada6d
data/CHANGELOG.md CHANGED
@@ -1,3 +1,17 @@
1
+ ## 0.6.0 (May 27, 2023)
2
+
3
+ - support for Sidekiq 7 (thanks to [@stympy](https://github.com/stympy))
4
+ - move CI pipelines to Github Actions - drop tests for everything below Sidekiq 5, run tests on redis 6.2 & 7.0 and ruby 2.6 - 3.1
5
+
6
+ ## 0.5.0 (August 13, 2021)
7
+
8
+ - maintenance - test on latest ruby versions (remove outdated rubies from build), add sidekiq 6 to build matrix, remove coveralls
9
+ - fix for ruby 3 (thanks to [@ak15](https://github.com/ak15))
10
+
11
+ ## 0.4.0 (July 18, 2018)
12
+
13
+ - make lock container configurable (non breaking change) - in case you would like to something else than `Thread.current` - now you easily can
14
+
1
15
  ## 0.3.1 (March 3, 2018)
2
16
 
3
17
  - do not assume `ActiveSupport` is loaded / or old `Sidekiq` patches are present (add own symbolize keys logic)
@@ -6,7 +20,7 @@
6
20
 
7
21
  ## 0.3.0 (July 28, 2016)
8
22
 
9
- - ability to set custom lock value. Works just like setting timeout and name (handles procs as well).
23
+ - ability to set custom lock value. Works just like setting timeout and name, handles procs as well (thanks to [@piokaczm](https://github.com/piokaczm))
10
24
 
11
25
  ``` ruby
12
26
  sidekiq_options lock: {
data/README.md CHANGED
@@ -1,16 +1,18 @@
1
+ <p align="center">
2
+ <img width="300" height="210" src="https://github.com/rwojsznis/sidekiq-lock/raw/main/logo.png">
3
+ </p>
4
+
1
5
  # Sidekiq::Lock
2
6
 
3
- [![Code Climate](https://codeclimate.com/github/emq/sidekiq-lock.png)](https://codeclimate.com/github/emq/sidekiq-lock)
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
+ [![Code Climate](https://codeclimate.com/github/rwojsznis/sidekiq-lock.png)](https://codeclimate.com/github/rwojsznis/sidekiq-lock)
7
8
  [![Gem Version](https://badge.fury.io/rb/sidekiq-lock.png)](http://badge.fury.io/rb/sidekiq-lock)
8
9
 
9
10
  **Note:** This is a _complete_ piece of software, it should work across all future sidekiq & ruby versions.
10
11
 
11
12
  Redis-based simple locking mechanism for [sidekiq][2]. Uses [SET command][1] introduced in Redis 2.6.16.
12
13
 
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.
14
+ 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
+ time - it provides a `lock` method that you can use in whatever way you want.
14
16
 
15
17
  ## Installation
16
18
 
@@ -34,7 +36,8 @@ $ bundle
34
36
 
35
37
  Sidekiq-lock is a middleware/module combination, let me go through my thought process here :).
36
38
 
37
- In your worker class include `Sidekiq::Lock::Worker` module and provide `lock` attribute inside `sidekiq_options`, for example:
39
+ In your worker class include `Sidekiq::Lock::Worker` module and provide `lock` attribute inside `sidekiq_options`,
40
+ for example:
38
41
 
39
42
  ``` ruby
40
43
  class Worker
@@ -52,13 +55,17 @@ end
52
55
 
53
56
  What will happen is:
54
57
 
55
- - middleware will setup a `Sidekiq::Lock::RedisLock` object under `Thread.current[Sidekiq::Lock::THREAD_KEY]` (well, I had no better idea for this) - assuming you provided `lock` options, otherwise it will do nothing, just execute your worker's code
58
+ - middleware will setup a `Sidekiq::Lock::RedisLock` object under `Thread.current[Sidekiq::Lock::THREAD_KEY]`
59
+ (it should work in most use cases without any problems - but it's configurable, more below) - assuming you provided
60
+ `lock` options, otherwise it will do nothing, just execute your worker's code
56
61
 
57
- - `Sidekiq::Lock::Worker` module provides a `lock` method that just simply points to that thread variable, just as a convenience
62
+ - `Sidekiq::Lock::Worker` module provides a `lock` method that just simply points to that thread variable, just as
63
+ a convenience
58
64
 
59
65
  So now in your worker class you can call (whenever you need):
60
66
 
61
- - `lock.acquire!` - will try to acquire the lock, if returns false on failure (that means some other process / thread took the lock first)
67
+ - `lock.acquire!` - will try to acquire the lock, if returns false on failure (that means some other process / thread
68
+ took the lock first)
62
69
  - `lock.acquired?` - set to `true` when lock is successfully acquired
63
70
  - `lock.release!` - deletes the lock (only if it's: acquired by current thread and not already expired)
64
71
 
@@ -115,9 +122,32 @@ Sidekiq.configure_server do |config|
115
122
  end
116
123
  ```
117
124
 
125
+ ### Customizing lock _container_
126
+
127
+ If you would like to change default behavior of storing lock instance in `Thread.current` for whatever reason you
128
+ can do that as well via server configuration:
129
+
130
+ ``` ruby
131
+ # Any thread-safe class that implements .fetch and .store methods will do
132
+ class CustomStorage
133
+ def fetch
134
+ # returns stored lock instance
135
+ end
136
+
137
+ def store(lock_instance)
138
+ # store lock
139
+ end
140
+ end
141
+
142
+ Sidekiq.configure_server do |config|
143
+ config.lock_container = CustomStorage.new
144
+ end
145
+ ```
146
+
118
147
  ### Inline testing
119
148
 
120
- 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:
149
+ As you know middleware is not invoked when testing jobs inline, you can require in your test/spec helper file
150
+ `sidekiq/lock/testing/inline` to include two methods that will help you setting / clearing up lock manually:
121
151
 
122
152
  - `set_sidekiq_lock(worker_class, payload)` - note: payload should be an array of worker arguments
123
153
  - `clear_sidekiq_lock`
@@ -0,0 +1,15 @@
1
+ module Sidekiq
2
+ module Lock
3
+ class Container
4
+ THREAD_KEY = :sidekiq_lock
5
+
6
+ def fetch
7
+ Thread.current[THREAD_KEY]
8
+ end
9
+
10
+ def store(lock)
11
+ Thread.current[THREAD_KEY] = lock
12
+ end
13
+ end
14
+ end
15
+ end
@@ -1,7 +1,7 @@
1
1
  module Sidekiq
2
2
  module Lock
3
3
  class Middleware
4
- def call(worker, msg, queue)
4
+ def call(worker, msg, _queue)
5
5
  options = lock_options(worker)
6
6
  setup_lock(options, msg['args']) unless options.nil?
7
7
 
@@ -11,7 +11,7 @@ module Sidekiq
11
11
  private
12
12
 
13
13
  def setup_lock(options, payload)
14
- Thread.current[Sidekiq::Lock::THREAD_KEY] = RedisLock.new(options, payload)
14
+ Sidekiq.lock_container.store(RedisLock.new(options, payload))
15
15
  end
16
16
 
17
17
  def lock_options(worker)
@@ -23,18 +23,17 @@ module Sidekiq
23
23
  # this also requires redis-rb >= 3.0.5
24
24
  def acquire!
25
25
  @acquired ||= Sidekiq.redis do |r|
26
- r.set(name, value, { nx: true, px: timeout })
26
+ r.set(name, value, nx: true, px: timeout)
27
27
  end
28
28
  end
29
29
 
30
30
  def release!
31
- Sidekiq.redis do |r|
32
- begin
33
- r.evalsha redis_lock_script_sha, keys: [name], argv: [value]
34
- rescue Redis::CommandError
35
- r.eval redis_lock_script, keys: [name], argv: [value]
36
- end
37
- end
31
+ # even if lock expired / was take over by another process
32
+ # it still means from our perspective that we released it
33
+ @acquired = false
34
+
35
+ # https://redis.io/commands/del/#return
36
+ release_lock == 1
38
37
  end
39
38
 
40
39
  def name
@@ -53,6 +52,27 @@ module Sidekiq
53
52
 
54
53
  attr_reader :options, :payload
55
54
 
55
+ def release_lock
56
+ # Sidekiq 7 uses redis-client gem, designed for redis 6+
57
+ if Sidekiq::VERSION >= '7'
58
+ Sidekiq.redis do |r|
59
+ begin
60
+ r.evalsha redis_lock_script_sha, [name], [value]
61
+ rescue RedisClient::CommandError
62
+ r.eval redis_lock_script, 1, [name], [value]
63
+ end
64
+ end
65
+ else
66
+ Sidekiq.redis do |r|
67
+ begin
68
+ r.evalsha redis_lock_script_sha, keys: [name], argv: [value]
69
+ rescue Redis::CommandError
70
+ r.eval redis_lock_script, keys: [name], argv: [value]
71
+ end
72
+ end
73
+ end
74
+ end
75
+
56
76
  def redis_lock_script_sha
57
77
  @lock_script_sha ||= Digest::SHA1.hexdigest redis_lock_script
58
78
  end
@@ -1,8 +1,8 @@
1
1
  def set_sidekiq_lock(worker_class, payload)
2
2
  options = worker_class.get_sidekiq_options['lock']
3
- Thread.current[Sidekiq::Lock::THREAD_KEY] = Sidekiq::Lock::RedisLock.new(options, payload)
3
+ Sidekiq.lock_container.store(Sidekiq::Lock::RedisLock.new(options, payload))
4
4
  end
5
5
 
6
6
  def clear_sidekiq_lock
7
- Thread.current[Sidekiq::Lock::THREAD_KEY] = nil
7
+ Sidekiq.lock_container.store(nil)
8
8
  end
@@ -1,5 +1,5 @@
1
1
  module Sidekiq
2
2
  module Lock
3
- VERSION = '0.3.1'
3
+ VERSION = '0.6.0'
4
4
  end
5
5
  end
@@ -3,7 +3,7 @@ module Sidekiq
3
3
  module Worker
4
4
  def self.included(base)
5
5
  base.send(:define_method, Sidekiq.lock_method) do
6
- Thread.current[Sidekiq::Lock::THREAD_KEY]
6
+ Sidekiq.lock_container.fetch
7
7
  end
8
8
  end
9
9
  end
data/lib/sidekiq/lock.rb CHANGED
@@ -1,11 +1,20 @@
1
+ require 'sidekiq/lock/container'
1
2
  require 'sidekiq/lock/middleware'
2
3
  require 'sidekiq/lock/redis_lock'
3
4
  require 'sidekiq/lock/version'
4
5
  require 'sidekiq/lock/worker'
5
6
 
6
7
  module Sidekiq
8
+ def self.lock_container
9
+ @lock_container ||= Lock::Container.new
10
+ end
11
+
7
12
  def self.lock_method
8
- @lock_method ||= :lock
13
+ @lock_method ||= Lock::METHOD_NAME
14
+ end
15
+
16
+ def self.lock_container=(container)
17
+ @lock_container = container
9
18
  end
10
19
 
11
20
  def self.lock_method=(method)
@@ -14,6 +23,7 @@ module Sidekiq
14
23
 
15
24
  module Lock
16
25
  THREAD_KEY = :sidekiq_lock
26
+ METHOD_NAME = :lock
17
27
  end
18
28
  end
19
29
 
@@ -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
@@ -4,7 +4,7 @@ require 'open3'
4
4
  module Sidekiq
5
5
  describe Lock do
6
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'
7
+ skip 'Sidekiq 7+ does not print out middleware information' if Sidekiq::VERSION >= '7'
8
8
 
9
9
  cmd = 'sidekiq -r ./test/test_workers.rb -v'
10
10
  buffer = ''
@@ -4,38 +4,43 @@ module Sidekiq
4
4
  module Lock
5
5
  describe Middleware do
6
6
  before do
7
- Sidekiq.redis = REDIS
7
+ if Sidekiq::VERSION >= '7'
8
+ Sidekiq.configure_server do |config|
9
+ config.redis = { url: REDIS_URL }
10
+ end
11
+ else
12
+ Sidekiq.redis = REDIS
13
+ end
8
14
  Sidekiq.redis { |c| c.flushdb }
9
- set_lock_variable!
15
+ reset_lock_variable!
10
16
  end
11
17
 
12
- let(:handler){ Sidekiq::Lock::Middleware.new }
18
+ let(:handler) { Sidekiq::Lock::Middleware.new }
13
19
 
14
20
  it 'sets lock variable with provided static lock options' do
15
- handler.call(LockWorker.new, {'class' => LockWorker, 'args' => []}, 'default') do
21
+ handler.call(LockWorker.new, { 'class' => LockWorker, 'args' => [] }, 'default') do
16
22
  true
17
23
  end
18
24
 
19
- assert_kind_of RedisLock, lock_thread_variable
25
+ assert_kind_of RedisLock, lock_container_variable
20
26
  end
21
27
 
22
28
  it 'sets lock variable with provided dynamic options' do
23
- handler.call(DynamicLockWorker.new, {'class' => DynamicLockWorker, 'args' => [1234, 1000]}, 'default') do
29
+ handler.call(DynamicLockWorker.new, { 'class' => DynamicLockWorker, 'args' => [1234, 1000] }, 'default') do
24
30
  true
25
31
  end
26
32
 
27
- assert_equal "lock:1234", lock_thread_variable.name
28
- assert_equal 2000, lock_thread_variable.timeout
33
+ assert_equal "lock:1234", lock_container_variable.name
34
+ assert_equal 2000, lock_container_variable.timeout
29
35
  end
30
36
 
31
37
  it 'sets nothing for workers without lock options' do
32
- handler.call(RegularWorker.new, {'class' => RegularWorker, 'args' => []}, 'default') do
38
+ handler.call(RegularWorker.new, { 'class' => RegularWorker, 'args' => [] }, 'default') do
33
39
  true
34
40
  end
35
41
 
36
- assert_nil lock_thread_variable
42
+ assert_nil lock_container_variable
37
43
  end
38
-
39
44
  end
40
45
  end
41
46
  end
@@ -4,12 +4,16 @@ module Sidekiq
4
4
  module Lock
5
5
  describe RedisLock do
6
6
  before do
7
- Sidekiq.redis = REDIS
7
+ if Sidekiq::VERSION >= '7'
8
+ Sidekiq.configure_client do |config|
9
+ config.redis = { url: REDIS_URL }
10
+ end
11
+ else
12
+ Sidekiq.redis = REDIS
13
+ end
8
14
  Sidekiq.redis { |c| c.flushdb }
9
15
  end
10
16
 
11
- let(:args) { [{'timeout' => 100, 'name' => 'test-lock'}, []] }
12
-
13
17
  it "raises an error on missing timeout&name values" do
14
18
  assert_raises ArgumentError do
15
19
  RedisLock.new({},[])
@@ -52,46 +56,59 @@ module Sidekiq
52
56
  end
53
57
 
54
58
  it "can acquire a lock" do
55
- lock = RedisLock.new(*args)
59
+ lock = RedisLock.new({'timeout' => 100, 'name' => 'test-lock'}, [])
60
+ assert lock.acquire!
61
+ end
62
+
63
+ it "sets proper lock value on first and second acquire" do
64
+ lock = RedisLock.new({'timeout' => 1000, 'name' => 'test-lock', 'value' => 'lock value'}, [])
56
65
  assert lock.acquire!
66
+ assert_equal 'lock value', redis("get", lock.name)
67
+ assert lock.release!
68
+ # at this point script should be used from evalsha
69
+ assert lock.acquire!
70
+ assert_equal 'lock value', redis("get", lock.name)
71
+
72
+ redis("script", "flush")
73
+ assert lock.acquire!
74
+ assert_equal 'lock value', redis("get", lock.name)
57
75
  end
58
76
 
59
- it "cannot aquire lock if it's already taken by other process/thread" do
60
- faster_lock = RedisLock.new(*args)
77
+ it "cannot acquire lock if it's already taken by other process/thread" do
78
+ faster_lock = RedisLock.new({'timeout' => 100, 'name' => 'test-lock'}, [])
61
79
  assert faster_lock.acquire!
62
80
 
63
- slower_lock = RedisLock.new(*args)
81
+ slower_lock = RedisLock.new({'timeout' => 100, 'name' => 'test-lock'}, [])
64
82
  refute slower_lock.acquire!
65
83
  end
66
84
 
67
85
  it "releases taken lock" do
68
- lock = RedisLock.new(*args)
86
+ lock = RedisLock.new({'timeout' => 100, 'name' => 'test-lock'}, [])
69
87
  lock.acquire!
70
88
  assert redis("get", "test-lock")
71
89
 
72
- lock.release!
90
+ assert lock.release!
73
91
  assert_nil redis("get", "test-lock")
74
92
  end
75
93
 
76
94
  it "releases lock taken by another process without deleting lock key" do
77
- lock = RedisLock.new(*args)
95
+ lock = RedisLock.new({'timeout' => 100, 'name' => 'test-lock'}, [])
78
96
  lock.acquire!
79
97
  lock_value = redis("get", "test-lock")
80
98
  assert lock_value
81
99
  sleep 0.11 # timeout lock
82
100
 
83
- new_lock = RedisLock.new(*args)
101
+ new_lock = RedisLock.new({'timeout' => 100, 'name' => 'test-lock'}, [])
84
102
  new_lock.acquire!
85
103
  new_lock_value = redis("get", "test-lock")
86
104
 
87
- lock.release!
105
+ refute lock.release!
88
106
 
89
107
  assert_equal new_lock_value, redis("get", "test-lock")
90
108
  end
91
109
 
92
110
  it "releases taken lock" do
93
- custom_args = [args.first.merge('value' => 'custom_value'), []]
94
- lock = RedisLock.new(*custom_args)
111
+ lock = RedisLock.new({'timeout' => 100, 'name' => 'test-lock', 'value' => 'custom_value'}, [])
95
112
  lock.acquire!
96
113
  assert redis("get", "test-lock")
97
114
 
@@ -2,20 +2,23 @@ require "test_helper"
2
2
  require "sidekiq/lock/testing/inline"
3
3
 
4
4
  describe "inline test helper" do
5
- after { set_lock_variable! }
5
+ after { reset_lock_variable! }
6
6
 
7
7
  it "has helper fuction for setting lock" do
8
- Sidekiq::Lock::RedisLock.expects(:new).with({ timeout: 1, name: 'lock-worker' }, 'worker argument').returns('lock set')
8
+ Sidekiq::Lock::RedisLock
9
+ .expects(:new)
10
+ .with({ timeout: 1, name: 'lock-worker' }, 'worker argument')
11
+ .returns('lock set')
12
+
9
13
  set_sidekiq_lock(LockWorker, 'worker argument')
10
- assert_equal 'lock set', lock_thread_variable
14
+ assert_equal 'lock set', lock_container_variable
11
15
  end
12
16
 
13
17
  it "has helper fuction for clearing lock" do
14
18
  set_lock_variable! "test"
15
- assert_equal "test", lock_thread_variable
19
+ assert_equal "test", lock_container_variable
16
20
 
17
21
  clear_sidekiq_lock
18
- assert_nil lock_thread_variable
22
+ assert_nil lock_container_variable
19
23
  end
20
-
21
24
  end
@@ -3,26 +3,64 @@ require 'test_helper'
3
3
  module Sidekiq
4
4
  module Lock
5
5
  describe Worker do
6
- after { set_lock_variable! }
6
+ # after { }
7
7
 
8
- it 'sets lock method that points to thread variable' do
9
- set_lock_variable! "test"
10
- assert_equal "test", LockWorker.new.lock
8
+ class CustomContainer
9
+ def initialize
10
+ @lock = nil
11
+ end
12
+
13
+ def fetch
14
+ @lock
15
+ end
16
+
17
+ def store(lock)
18
+ @lock = lock
19
+ end
11
20
  end
12
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
+
13
27
  it 'allows method name configuration' do
14
- Sidekiq.lock_method = :custom_lock_name
28
+ begin
29
+ Sidekiq.lock_method = :custom_lock_name
15
30
 
16
- class WorkerWithCustomLockName
17
- include Sidekiq::Worker
18
- include Sidekiq::Lock::Worker
19
- end
31
+ class WorkerWithCustomLockName
32
+ include Sidekiq::Worker
33
+ include Sidekiq::Lock::Worker
34
+ end
35
+
36
+ set_lock_variable! "custom_name"
20
37
 
21
- set_lock_variable! "custom_name"
38
+ assert_equal "custom_name", WorkerWithCustomLockName.new.custom_lock_name
22
39
 
23
- assert_equal "custom_name", WorkerWithCustomLockName.new.custom_lock_name
40
+ reset_lock_variable!
41
+ ensure
42
+
43
+ Sidekiq.lock_method = Sidekiq::Lock::METHOD_NAME
44
+ end
24
45
  end
25
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
26
64
  end
27
65
  end
28
66
  end
data/test/test_helper.rb CHANGED
@@ -1,8 +1,3 @@
1
- require 'coveralls'
2
- Coveralls.wear! do
3
- add_filter '/test/'
4
- end
5
-
6
1
  require 'minitest/autorun'
7
2
  require 'minitest/pride'
8
3
  require 'mocha/setup'
@@ -12,7 +7,8 @@ require 'test_workers'
12
7
 
13
8
  Sidekiq.logger.level = Logger::ERROR
14
9
 
15
- REDIS = Sidekiq::RedisConnection.create(url: 'redis://localhost/15')
10
+ REDIS_URL = ENV['REDIS_URL'] || 'redis://localhost/15'
11
+ REDIS = Sidekiq::RedisConnection.create(url: REDIS_URL)
16
12
 
17
13
  def redis(command, *args)
18
14
  Sidekiq.redis do |c|
@@ -20,10 +16,14 @@ def redis(command, *args)
20
16
  end
21
17
  end
22
18
 
23
- def set_lock_variable!(value = nil)
24
- Thread.current[Sidekiq::Lock::THREAD_KEY] = value
19
+ def set_lock_variable!(value)
20
+ Sidekiq.lock_container.store(value)
21
+ end
22
+
23
+ def reset_lock_variable!
24
+ set_lock_variable!(nil)
25
25
  end
26
26
 
27
- def lock_thread_variable
28
- Thread.current[Sidekiq::Lock::THREAD_KEY]
27
+ def lock_container_variable
28
+ Sidekiq.lock_container.fetch
29
29
  end
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.1
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rafal Wojsznis
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-03-03 00:00:00.000000000 Z
11
+ date: 2023-05-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: sidekiq
@@ -66,34 +66,6 @@ dependencies:
66
66
  - - ">="
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
- - !ruby/object:Gem::Dependency
70
- name: rack-test
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
- - !ruby/object:Gem::Dependency
84
- name: coveralls
85
- requirement: !ruby/object:Gem::Requirement
86
- requirements:
87
- - - "~>"
88
- - !ruby/object:Gem::Version
89
- version: '0.8'
90
- type: :development
91
- prerelease: false
92
- version_requirements: !ruby/object:Gem::Requirement
93
- requirements:
94
- - - "~>"
95
- - !ruby/object:Gem::Version
96
- version: '0.8'
97
69
  - !ruby/object:Gem::Dependency
98
70
  name: mocha
99
71
  requirement: !ruby/object:Gem::Requirement
@@ -143,26 +115,19 @@ executables: []
143
115
  extensions: []
144
116
  extra_rdoc_files: []
145
117
  files:
146
- - ".gitignore"
147
- - ".travis.yml"
148
- - Appraisals
149
118
  - CHANGELOG.md
150
- - Gemfile
151
119
  - LICENSE.txt
152
120
  - README.md
153
121
  - Rakefile
154
- - gemfiles/sidekiq_2.gemfile
155
- - gemfiles/sidekiq_3.gemfile
156
- - gemfiles/sidekiq_4.gemfile
157
- - gemfiles/sidekiq_5.gemfile
158
122
  - lib/sidekiq-lock.rb
159
123
  - lib/sidekiq/lock.rb
124
+ - lib/sidekiq/lock/container.rb
160
125
  - lib/sidekiq/lock/middleware.rb
161
126
  - lib/sidekiq/lock/redis_lock.rb
162
127
  - lib/sidekiq/lock/testing/inline.rb
163
128
  - lib/sidekiq/lock/version.rb
164
129
  - lib/sidekiq/lock/worker.rb
165
- - sidekiq-lock.gemspec
130
+ - test/lib/container_test.rb
166
131
  - test/lib/lock_test.rb
167
132
  - test/lib/middleware_test.rb
168
133
  - test/lib/redis_lock_test.rb
@@ -174,7 +139,7 @@ homepage: https://github.com/emq/sidekiq-lock
174
139
  licenses:
175
140
  - MIT
176
141
  metadata: {}
177
- post_install_message:
142
+ post_install_message:
178
143
  rdoc_options: []
179
144
  require_paths:
180
145
  - lib
@@ -189,12 +154,12 @@ required_rubygems_version: !ruby/object:Gem::Requirement
189
154
  - !ruby/object:Gem::Version
190
155
  version: '0'
191
156
  requirements: []
192
- rubyforge_project:
193
- rubygems_version: 2.7.6
194
- signing_key:
157
+ rubygems_version: 3.3.7
158
+ signing_key:
195
159
  specification_version: 4
196
160
  summary: Simple redis-based lock mechanism for your sidekiq workers
197
161
  test_files:
162
+ - test/lib/container_test.rb
198
163
  - test/lib/lock_test.rb
199
164
  - test/lib/middleware_test.rb
200
165
  - test/lib/redis_lock_test.rb
data/.gitignore DELETED
@@ -1,21 +0,0 @@
1
- *.gem
2
- *.rbc
3
- .bundle
4
- .config
5
- .yardoc
6
- Gemfile.lock
7
- InstalledFiles
8
- _yardoc
9
- coverage
10
- doc/
11
- lib/bundler/man
12
- pkg
13
- rdoc
14
- spec/reports
15
- test/tmp
16
- test/version_tmp
17
- tmp
18
- .rvmrc
19
- .ruby-version
20
- .ruby-gemset
21
- *.gemfile.lock
data/.travis.yml DELETED
@@ -1,20 +0,0 @@
1
- language: ruby
2
- cache: bundler
3
-
4
- services:
5
- - redis-server
6
-
7
- gemfile:
8
- - gemfiles/sidekiq_2.gemfile
9
- - gemfiles/sidekiq_3.gemfile
10
- - gemfiles/sidekiq_4.gemfile
11
- - gemfiles/sidekiq_5.gemfile
12
-
13
- rvm:
14
- - 2.2.9
15
- - 2.3.6
16
- - 2.4.3
17
- - 2.5.0
18
-
19
- notifications:
20
- email: false
data/Appraisals DELETED
@@ -1,15 +0,0 @@
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/Gemfile DELETED
@@ -1,2 +0,0 @@
1
- source "https://rubygems.org"
2
- gemspec
@@ -1,7 +0,0 @@
1
- # This file was generated by Appraisal
2
-
3
- source "https://rubygems.org"
4
-
5
- gem "sidekiq", "~> 2.17", ">= 2.17.8"
6
-
7
- gemspec path: "../"
@@ -1,7 +0,0 @@
1
- # This file was generated by Appraisal
2
-
3
- source "https://rubygems.org"
4
-
5
- gem "sidekiq", "~> 3.5", ">= 3.5.4"
6
-
7
- gemspec path: "../"
@@ -1,7 +0,0 @@
1
- # This file was generated by Appraisal
2
-
3
- source "https://rubygems.org"
4
-
5
- gem "sidekiq", "~> 4.2", ">= 4.2.10"
6
-
7
- gemspec path: "../"
@@ -1,7 +0,0 @@
1
- # This file was generated by Appraisal
2
-
3
- source "https://rubygems.org"
4
-
5
- gem "sidekiq", "~> 5.1", ">= 5.1"
6
-
7
- gemspec path: "../"
data/sidekiq-lock.gemspec DELETED
@@ -1,30 +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"
24
- spec.add_development_dependency "rake"
25
- spec.add_development_dependency "rack-test"
26
- spec.add_development_dependency "coveralls", "~> 0.8"
27
- spec.add_development_dependency "mocha", "~> 0.14.0"
28
- spec.add_development_dependency "minitest"
29
- spec.add_development_dependency "appraisal"
30
- end