sidekiq-lock 0.3.0 → 0.3.1

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
- SHA1:
3
- metadata.gz: ae0d7372658be88512d4c4f9374796c5b5722b7d
4
- data.tar.gz: a6882527481df8f118627157eab0204ef0f1a9a4
2
+ SHA256:
3
+ metadata.gz: 70c2ca23e60ff013252e111d7507b3665deaaadd31d23a6dc10e972315be335f
4
+ data.tar.gz: 67cdd99cb1e60accad3304bc13f295051b54d09553ab278503d98240bf149d21
5
5
  SHA512:
6
- metadata.gz: f56e2cbaecb3d42ad2dba3c3381f6c5ffb2b503697bfa64e49a52463dc6caa624b5581121c43b9e91f7de0efc3602174bff15dec67ba5647e1ba5e46609786f2
7
- data.tar.gz: a652570e74c4101ae7d1f56bbaff11ee8bf6673955a8ff11340bc593e0f4b28d22eedee7554a4517830f5d351816ae1242c80b1ed9739ba73f2e985b7dd4849b
6
+ metadata.gz: 3595ae9803227cbb2051908bd7ecbbbbec409a57e5f41dbae552b838f5d71decf35dab1ade805ff851c806f7aa69896b0485018401fa41f5454e2638067133ca
7
+ data.tar.gz: ea73c729c4da75a0a6c47c652376e96f5574ba2f2f93b09ecc6f12564d2d605d00e625247643cbb17462a3a0dca3049a5ed696882f53fe26ff59dda84be3f0a1
data/.gitignore CHANGED
@@ -18,3 +18,4 @@ tmp
18
18
  .rvmrc
19
19
  .ruby-version
20
20
  .ruby-gemset
21
+ *.gemfile.lock
data/.travis.yml CHANGED
@@ -1,15 +1,20 @@
1
1
  language: ruby
2
+ cache: bundler
2
3
 
3
4
  services:
4
5
  - redis-server
5
6
 
7
+ gemfile:
8
+ - gemfiles/sidekiq_2.gemfile
9
+ - gemfiles/sidekiq_3.gemfile
10
+ - gemfiles/sidekiq_4.gemfile
11
+ - gemfiles/sidekiq_5.gemfile
12
+
6
13
  rvm:
7
- - 2.0.0
8
- - 2.1.0
9
- # seems like rack 2.0.1 needs it
10
- - 2.2.2
11
- - 2.3.0
12
- - 2.3.1
14
+ - 2.2.9
15
+ - 2.3.6
16
+ - 2.4.3
17
+ - 2.5.0
13
18
 
14
19
  notifications:
15
20
  email: false
data/Appraisals ADDED
@@ -0,0 +1,15 @@
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/CHANGELOG.md CHANGED
@@ -1,4 +1,10 @@
1
- ## 0.3.0
1
+ ## 0.3.1 (March 3, 2018)
2
+
3
+ - do not assume `ActiveSupport` is loaded / or old `Sidekiq` patches are present (add own symbolize keys logic)
4
+ - 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!)
5
+ - run test on travis for sidekiq `2.17`, `3.5`, `4.2` and `>= 5.1` and all newest rubies (`2.2` - `2.5`)
6
+
7
+ ## 0.3.0 (July 28, 2016)
2
8
 
3
9
  - ability to set custom lock value. Works just like setting timeout and name (handles procs as well).
4
10
 
@@ -10,7 +16,7 @@ sidekiq_options lock: {
10
16
  }
11
17
  ```
12
18
 
13
- ## 0.2.0
19
+ ## 0.2.0 (December 08, 2013)
14
20
 
15
21
  - ability to globally configure `lock` method name
16
22
 
@@ -30,6 +36,6 @@ end
30
36
  That will setup `RedisLock` under proper thread variable.
31
37
  This can be handy if you test your workers inline (without full stack middleware)
32
38
 
33
- ## 0.0.1
39
+ ## 0.0.1 (October 14, 2013)
34
40
 
35
41
  - Initial release
data/Gemfile CHANGED
@@ -1,8 +1,2 @@
1
1
  source "https://rubygems.org"
2
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
@@ -6,6 +6,8 @@
6
6
  [![Dependency Status](https://gemnasium.com/emq/sidekiq-lock.png)](https://gemnasium.com/emq/sidekiq-lock)
7
7
  [![Gem Version](https://badge.fury.io/rb/sidekiq-lock.png)](http://badge.fury.io/rb/sidekiq-lock)
8
8
 
9
+ **Note:** This is a _complete_ piece of software, it should work across all future sidekiq & ruby versions.
10
+
9
11
  Redis-based simple locking mechanism for [sidekiq][2]. Uses [SET command][1] introduced in Redis 2.6.16.
10
12
 
11
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.
@@ -0,0 +1,7 @@
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: "../"
@@ -0,0 +1,7 @@
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: "../"
@@ -0,0 +1,7 @@
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: "../"
@@ -0,0 +1,7 @@
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/lib/sidekiq/lock.rb CHANGED
@@ -1,12 +1,11 @@
1
- require "sidekiq/lock/middleware"
2
- require "sidekiq/lock/redis_lock"
3
- require "sidekiq/lock/version"
4
- require "sidekiq/lock/worker"
1
+ require 'sidekiq/lock/middleware'
2
+ require 'sidekiq/lock/redis_lock'
3
+ require 'sidekiq/lock/version'
4
+ require 'sidekiq/lock/worker'
5
5
 
6
6
  module Sidekiq
7
-
8
7
  def self.lock_method
9
- @lock_method || :lock
8
+ @lock_method ||= :lock
10
9
  end
11
10
 
12
11
  def self.lock_method=(method)
@@ -23,4 +22,3 @@ Sidekiq.configure_server do |config|
23
22
  chain.add Sidekiq::Lock::Middleware
24
23
  end
25
24
  end
26
-
@@ -1,7 +1,6 @@
1
1
  module Sidekiq
2
2
  module Lock
3
3
  class Middleware
4
-
5
4
  def call(worker, msg, queue)
6
5
  options = lock_options(worker)
7
6
  setup_lock(options, msg['args']) unless options.nil?
@@ -11,14 +10,13 @@ module Sidekiq
11
10
 
12
11
  private
13
12
 
14
- def setup_lock(options, payload)
15
- Thread.current[Sidekiq::Lock::THREAD_KEY] = RedisLock.new(options, payload)
16
- end
17
-
18
- def lock_options(worker)
19
- worker.class.get_sidekiq_options['lock']
20
- end
13
+ def setup_lock(options, payload)
14
+ Thread.current[Sidekiq::Lock::THREAD_KEY] = 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(options, payload)
8
- @options = options.symbolize_keys
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?
@@ -36,42 +38,44 @@ module Sidekiq
36
38
  end
37
39
 
38
40
  def name
39
- raise ArgumentError, "Provide a lock name inside sidekiq_options" if options[:name].nil?
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, "Provide lock timeout inside sidekiq_options" if options[:timeout].nil?
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
- def redis_lock_script_sha
53
- @lock_script_sha ||= Digest::SHA1.hexdigest redis_lock_script
54
- end
54
+ attr_reader :options, :payload
55
55
 
56
- def redis_lock_script
57
- <<-LUA
58
- if redis.call("get", KEYS[1]) == ARGV[1]
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
- def value
68
- @value ||= set_lock_value(options[:value])
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
70
 
71
- def set_lock_value(custom_value)
72
- return SecureRandom.hex(25) unless custom_value
73
- custom_value.respond_to?(:call) ? custom_value.call(*payload) : custom_value
74
- end
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
75
79
  end
76
80
  end
77
81
  end
@@ -1,5 +1,5 @@
1
1
  module Sidekiq
2
2
  module Lock
3
- VERSION = "0.3.0"
3
+ VERSION = '0.3.1'
4
4
  end
5
5
  end
@@ -1,13 +1,11 @@
1
1
  module Sidekiq
2
2
  module Lock
3
3
  module Worker
4
-
5
4
  def self.included(base)
6
5
  base.send(:define_method, Sidekiq.lock_method) do
7
6
  Thread.current[Sidekiq::Lock::THREAD_KEY]
8
7
  end
9
8
  end
10
-
11
9
  end
12
10
  end
13
11
  end
data/sidekiq-lock.gemspec CHANGED
@@ -20,11 +20,11 @@ Gem::Specification.new do |spec|
20
20
  spec.add_dependency "sidekiq", ">= 2.14.0"
21
21
  spec.add_dependency "redis", ">= 3.0.5"
22
22
 
23
- spec.add_development_dependency "bundler", "~> 1.3"
23
+ spec.add_development_dependency "bundler"
24
24
  spec.add_development_dependency "rake"
25
25
  spec.add_development_dependency "rack-test"
26
- spec.add_development_dependency "coveralls", "~> 0.7.0"
26
+ spec.add_development_dependency "coveralls", "~> 0.8"
27
27
  spec.add_development_dependency "mocha", "~> 0.14.0"
28
28
  spec.add_development_dependency "minitest"
29
- spec.add_development_dependency "redis-namespace"
29
+ spec.add_development_dependency "appraisal"
30
30
  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
@@ -1,9 +1,8 @@
1
- require "test_helper"
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 }
@@ -1,4 +1,4 @@
1
- require "test_helper"
1
+ require 'test_helper'
2
2
 
3
3
  module Sidekiq
4
4
  module Lock
@@ -2,7 +2,6 @@ require "test_helper"
2
2
  require "sidekiq/lock/testing/inline"
3
3
 
4
4
  describe "inline test helper" do
5
-
6
5
  after { set_lock_variable! }
7
6
 
8
7
  it "has helper fuction for setting lock" do
@@ -1,9 +1,8 @@
1
- require "test_helper"
1
+ require 'test_helper'
2
2
 
3
3
  module Sidekiq
4
4
  module Lock
5
5
  describe Worker do
6
-
7
6
  after { set_lock_variable! }
8
7
 
9
8
  it 'sets lock method that points to thread variable' do
data/test/test_helper.rb CHANGED
@@ -1,22 +1,18 @@
1
1
  require 'coveralls'
2
2
  Coveralls.wear! do
3
- add_filter "/test/"
3
+ add_filter '/test/'
4
4
  end
5
5
 
6
- Encoding.default_external = Encoding::UTF_8
7
- Encoding.default_internal = Encoding::UTF_8
6
+ require 'minitest/autorun'
7
+ require 'minitest/pride'
8
+ require 'mocha/setup'
8
9
 
9
- require "minitest/autorun"
10
- require "minitest/pride"
11
- require "mocha/setup"
12
-
13
- require "sidekiq"
14
- require "sidekiq-lock"
15
- require "test_workers"
10
+ require 'sidekiq'
11
+ require 'test_workers'
16
12
 
17
13
  Sidekiq.logger.level = Logger::ERROR
18
14
 
19
- REDIS = Sidekiq::RedisConnection.create(url: "redis://localhost/15", namespace: "sidekiq_lock_test")
15
+ REDIS = Sidekiq::RedisConnection.create(url: 'redis://localhost/15')
20
16
 
21
17
  def redis(command, *args)
22
18
  Sidekiq.redis do |c|
data/test/test_workers.rb CHANGED
@@ -1,3 +1,5 @@
1
+ require 'sidekiq-lock'
2
+
1
3
  class LockWorker
2
4
  include Sidekiq::Worker
3
5
  include Sidekiq::Lock::Worker
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.0
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rafal Wojsznis
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-07-28 00:00:00.000000000 Z
11
+ date: 2018-03-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: sidekiq
@@ -42,16 +42,16 @@ dependencies:
42
42
  name: bundler
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - "~>"
45
+ - - ">="
46
46
  - !ruby/object:Gem::Version
47
- version: '1.3'
47
+ version: '0'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - "~>"
52
+ - - ">="
53
53
  - !ruby/object:Gem::Version
54
- version: '1.3'
54
+ version: '0'
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: rake
57
57
  requirement: !ruby/object:Gem::Requirement
@@ -86,14 +86,14 @@ dependencies:
86
86
  requirements:
87
87
  - - "~>"
88
88
  - !ruby/object:Gem::Version
89
- version: 0.7.0
89
+ version: '0.8'
90
90
  type: :development
91
91
  prerelease: false
92
92
  version_requirements: !ruby/object:Gem::Requirement
93
93
  requirements:
94
94
  - - "~>"
95
95
  - !ruby/object:Gem::Version
96
- version: 0.7.0
96
+ version: '0.8'
97
97
  - !ruby/object:Gem::Dependency
98
98
  name: mocha
99
99
  requirement: !ruby/object:Gem::Requirement
@@ -123,7 +123,7 @@ dependencies:
123
123
  - !ruby/object:Gem::Version
124
124
  version: '0'
125
125
  - !ruby/object:Gem::Dependency
126
- name: redis-namespace
126
+ name: appraisal
127
127
  requirement: !ruby/object:Gem::Requirement
128
128
  requirements:
129
129
  - - ">="
@@ -145,11 +145,16 @@ extra_rdoc_files: []
145
145
  files:
146
146
  - ".gitignore"
147
147
  - ".travis.yml"
148
+ - Appraisals
148
149
  - CHANGELOG.md
149
150
  - Gemfile
150
151
  - LICENSE.txt
151
152
  - README.md
152
153
  - Rakefile
154
+ - gemfiles/sidekiq_2.gemfile
155
+ - gemfiles/sidekiq_3.gemfile
156
+ - gemfiles/sidekiq_4.gemfile
157
+ - gemfiles/sidekiq_5.gemfile
153
158
  - lib/sidekiq-lock.rb
154
159
  - lib/sidekiq/lock.rb
155
160
  - lib/sidekiq/lock/middleware.rb
@@ -158,6 +163,7 @@ files:
158
163
  - lib/sidekiq/lock/version.rb
159
164
  - lib/sidekiq/lock/worker.rb
160
165
  - sidekiq-lock.gemspec
166
+ - test/lib/lock_test.rb
161
167
  - test/lib/middleware_test.rb
162
168
  - test/lib/redis_lock_test.rb
163
169
  - test/lib/testing/inline_test.rb
@@ -184,11 +190,12 @@ required_rubygems_version: !ruby/object:Gem::Requirement
184
190
  version: '0'
185
191
  requirements: []
186
192
  rubyforge_project:
187
- rubygems_version: 2.5.1
193
+ rubygems_version: 2.7.6
188
194
  signing_key:
189
195
  specification_version: 4
190
196
  summary: Simple redis-based lock mechanism for your sidekiq workers
191
197
  test_files:
198
+ - test/lib/lock_test.rb
192
199
  - test/lib/middleware_test.rb
193
200
  - test/lib/redis_lock_test.rb
194
201
  - test/lib/testing/inline_test.rb