sidekiq-lock 0.4.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: 7fb91d967eb073b2fb524357b936d473f3ab03450f3a1e7f45f9ae53895a210e
4
- data.tar.gz: 10e5ac0b30ea212f675d9ce11ba63da4114b7ade9f1da89bbdcd209cfaa2f40e
3
+ metadata.gz: 94c0d6993d3e6f8c2acd6a63e2e78464ac974369138f6c20eeb91525d0b5ea13
4
+ data.tar.gz: 75437ca38c53bc825d0e6e77b8dc2ef64c5c42bb8802746f5fbacf1f91c9e4f5
5
5
  SHA512:
6
- metadata.gz: 3181d04fc0b7fbb7f9807851abf37d69b09333b38fef0c8388671a750a4087a5a185fd57dce6469095fb26ee42728b2e856f623b2643a3c15a9ed244197e6d0f
7
- data.tar.gz: b1d1bd1c051b60188853436fd62ecec774cac2e303134e12dac203d166159b51e141f4a1a626ed952b6aa4591352f11e283bd17d371174bfdd25a6c3cac9745b
6
+ metadata.gz: 463adf7d4dbf5d468a1c795105a716d94fd6f77141e7c7f3d77a84076e4e222cc2b6fb57f64d5286bca6fc8b9491c4de891190fb3d255f1766acd879253220d0
7
+ data.tar.gz: c215cce1d79e99aaa28a04a5238aac165a8e5d36271a372b39067a80040109e5f64117114fdbfae227c5cb406f86cd46e554cfa6820978573abd1f08b04ada6d
data/CHANGELOG.md CHANGED
@@ -1,3 +1,13 @@
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
+
1
11
  ## 0.4.0 (July 18, 2018)
2
12
 
3
13
  - make lock container configurable (non breaking change) - in case you would like to something else than `Thread.current` - now you easily can
@@ -10,7 +20,7 @@
10
20
 
11
21
  ## 0.3.0 (July 28, 2016)
12
22
 
13
- - 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))
14
24
 
15
25
  ``` ruby
16
26
  sidekiq_options lock: {
data/README.md CHANGED
@@ -1,8 +1,10 @@
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)
7
+ [![Code Climate](https://codeclimate.com/github/rwojsznis/sidekiq-lock.png)](https://codeclimate.com/github/rwojsznis/sidekiq-lock)
6
8
  [![Gem Version](https://badge.fury.io/rb/sidekiq-lock.png)](http://badge.fury.io/rb/sidekiq-lock)
7
9
 
8
10
  **Note:** This is a _complete_ piece of software, it should work across all future sidekiq & ruby versions.
@@ -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,5 +1,5 @@
1
1
  module Sidekiq
2
2
  module Lock
3
- VERSION = '0.4.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
@@ -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|
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.4.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: 2018-07-18 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,18 +115,10 @@ 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
160
124
  - lib/sidekiq/lock/container.rb
@@ -163,7 +127,6 @@ files:
163
127
  - lib/sidekiq/lock/testing/inline.rb
164
128
  - lib/sidekiq/lock/version.rb
165
129
  - lib/sidekiq/lock/worker.rb
166
- - sidekiq-lock.gemspec
167
130
  - test/lib/container_test.rb
168
131
  - test/lib/lock_test.rb
169
132
  - test/lib/middleware_test.rb
@@ -176,7 +139,7 @@ homepage: https://github.com/emq/sidekiq-lock
176
139
  licenses:
177
140
  - MIT
178
141
  metadata: {}
179
- post_install_message:
142
+ post_install_message:
180
143
  rdoc_options: []
181
144
  require_paths:
182
145
  - lib
@@ -191,9 +154,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
191
154
  - !ruby/object:Gem::Version
192
155
  version: '0'
193
156
  requirements: []
194
- rubyforge_project:
195
- rubygems_version: 2.7.6
196
- signing_key:
157
+ rubygems_version: 3.3.7
158
+ signing_key:
197
159
  specification_version: 4
198
160
  summary: Simple redis-based lock mechanism for your sidekiq workers
199
161
  test_files:
data/.gitignore DELETED
@@ -1,22 +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
22
- .idea/
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.10
15
- - 2.3.7
16
- - 2.4.4
17
- - 2.5.1
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