sidekiq-lock 0.5.0 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 592d4bf95b4bbd2cba2444898c5f6843c0a9112c6c45e018999af361b8a5ca73
4
- data.tar.gz: 7b0a5614796a292af28fc0880915a76ac85ecfb8c669b07d57d5019d8f246342
3
+ metadata.gz: 94c0d6993d3e6f8c2acd6a63e2e78464ac974369138f6c20eeb91525d0b5ea13
4
+ data.tar.gz: 75437ca38c53bc825d0e6e77b8dc2ef64c5c42bb8802746f5fbacf1f91c9e4f5
5
5
  SHA512:
6
- metadata.gz: ff115e3d009c6b6803e20326c2d84bf2286d9e4b652fe840b743596b8257919d7c54b3f1b9e1dcf2f9f1cba16af6541490464a77d55b52ff1b9407f28a7981f3
7
- data.tar.gz: 0cf165b35136db26001b236837dcd7a831867fc81d8b81a3e14c2efe76edf41ade9cb5ba437b311b9ec1626af5aae176a3a09fde0c393ef5b949f2f5e5f8fa64
6
+ metadata.gz: 463adf7d4dbf5d468a1c795105a716d94fd6f77141e7c7f3d77a84076e4e222cc2b6fb57f64d5286bca6fc8b9491c4de891190fb3d255f1766acd879253220d0
7
+ data.tar.gz: c215cce1d79e99aaa28a04a5238aac165a8e5d36271a372b39067a80040109e5f64117114fdbfae227c5cb406f86cd46e554cfa6820978573abd1f08b04ada6d
data/CHANGELOG.md CHANGED
@@ -1,3 +1,8 @@
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
+
1
6
  ## 0.5.0 (August 13, 2021)
2
7
 
3
8
  - maintenance - test on latest ruby versions (remove outdated rubies from build), add sidekiq 6 to build matrix, remove coveralls
data/README.md CHANGED
@@ -1,11 +1,10 @@
1
1
  <p align="center">
2
- <img width="300" height="210" src="https://github.com/emq/sidekiq-lock/raw/master/logo.png">
2
+ <img width="300" height="210" src="https://github.com/rwojsznis/sidekiq-lock/raw/main/logo.png">
3
3
  </p>
4
4
 
5
5
  # Sidekiq::Lock
6
6
 
7
- [![Code Climate](https://codeclimate.com/github/emq/sidekiq-lock.png)](https://codeclimate.com/github/emq/sidekiq-lock)
8
- [![Build Status](https://travis-ci.com/rwojsznis/sidekiq-lock.svg?branch=master)](https://travis-ci.com/rwojsznis/sidekiq-lock)
7
+ [![Code Climate](https://codeclimate.com/github/rwojsznis/sidekiq-lock.png)](https://codeclimate.com/github/rwojsznis/sidekiq-lock)
9
8
  [![Gem Version](https://badge.fury.io/rb/sidekiq-lock.png)](http://badge.fury.io/rb/sidekiq-lock)
10
9
 
11
10
  **Note:** This is a _complete_ piece of software, it should work across all future sidekiq & ruby versions.
@@ -28,13 +28,12 @@ module Sidekiq
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,5 +1,5 @@
1
1
  module Sidekiq
2
2
  module Lock
3
- VERSION = '0.5.0'
3
+ VERSION = '0.6.0'
4
4
  end
5
5
  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,7 +4,13 @@ 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
15
  reset_lock_variable!
10
16
  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
 
data/test/test_helper.rb CHANGED
@@ -7,7 +7,8 @@ require 'test_workers'
7
7
 
8
8
  Sidekiq.logger.level = Logger::ERROR
9
9
 
10
- 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)
11
12
 
12
13
  def redis(command, *args)
13
14
  Sidekiq.redis do |c|
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.5.0
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: 2021-08-13 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,20 +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
69
  - !ruby/object:Gem::Dependency
84
70
  name: mocha
85
71
  requirement: !ruby/object:Gem::Requirement
@@ -153,7 +139,7 @@ homepage: https://github.com/emq/sidekiq-lock
153
139
  licenses:
154
140
  - MIT
155
141
  metadata: {}
156
- post_install_message:
142
+ post_install_message:
157
143
  rdoc_options: []
158
144
  require_paths:
159
145
  - lib
@@ -168,16 +154,16 @@ required_rubygems_version: !ruby/object:Gem::Requirement
168
154
  - !ruby/object:Gem::Version
169
155
  version: '0'
170
156
  requirements: []
171
- rubygems_version: 3.2.5
172
- signing_key:
157
+ rubygems_version: 3.3.7
158
+ signing_key:
173
159
  specification_version: 4
174
160
  summary: Simple redis-based lock mechanism for your sidekiq workers
175
161
  test_files:
176
- - test/test_workers.rb
177
- - test/lib/worker_test.rb
162
+ - test/lib/container_test.rb
163
+ - test/lib/lock_test.rb
164
+ - test/lib/middleware_test.rb
178
165
  - test/lib/redis_lock_test.rb
179
166
  - test/lib/testing/inline_test.rb
180
- - test/lib/middleware_test.rb
181
- - test/lib/lock_test.rb
182
- - test/lib/container_test.rb
167
+ - test/lib/worker_test.rb
183
168
  - test/test_helper.rb
169
+ - test/test_workers.rb