sidekiq-lock 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: ae0d7372658be88512d4c4f9374796c5b5722b7d
4
+ data.tar.gz: a6882527481df8f118627157eab0204ef0f1a9a4
5
+ SHA512:
6
+ metadata.gz: f56e2cbaecb3d42ad2dba3c3381f6c5ffb2b503697bfa64e49a52463dc6caa624b5581121c43b9e91f7de0efc3602174bff15dec67ba5647e1ba5e46609786f2
7
+ data.tar.gz: a652570e74c4101ae7d1f56bbaff11ee8bf6673955a8ff11340bc593e0f4b28d22eedee7554a4517830f5d351816ae1242c80b1ed9739ba73f2e985b7dd4849b
@@ -4,10 +4,12 @@ services:
4
4
  - redis-server
5
5
 
6
6
  rvm:
7
- - 1.9.3
8
- - jruby-19mode
9
- - rbx
10
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
11
13
 
12
14
  notifications:
13
15
  email: false
@@ -1,3 +1,15 @@
1
+ ## 0.3.0
2
+
3
+ - ability to set custom lock value. Works just like setting timeout and name (handles procs as well).
4
+
5
+ ``` ruby
6
+ sidekiq_options lock: {
7
+ timeout: timeout,
8
+ name: name,
9
+ value: custom_value
10
+ }
11
+ ```
12
+
1
13
  ## 0.2.0
2
14
 
3
15
  - ability to globally configure `lock` method name
data/README.md CHANGED
@@ -58,7 +58,7 @@ So now in your worker class you can call (whenever you need):
58
58
 
59
59
  - `lock.acquire!` - will try to acquire the lock, if returns false on failure (that means some other process / thread took the lock first)
60
60
  - `lock.acquired?` - set to `true` when lock is successfully acquired
61
- - `lock.release!` - deletes the lock (if not already expired / taken by another process)
61
+ - `lock.release!` - deletes the lock (only if it's: acquired by current thread and not already expired)
62
62
 
63
63
  ### Lock options
64
64
 
@@ -66,6 +66,7 @@ sidekiq_options lock will accept static values or `Proc` that will be called on
66
66
 
67
67
  - timeout - specified expire time, in milliseconds
68
68
  - name - name of the redis key that will be used as lock name
69
+ - value - (optional) value of the lock, if not provided it's set to random hex
69
70
 
70
71
  Dynamic lock example:
71
72
 
@@ -75,7 +76,8 @@ class Worker
75
76
  include Sidekiq::Lock::Worker
76
77
  sidekiq_options lock: {
77
78
  timeout: proc { |user_id, timeout| timeout * 2 },
78
- name: proc { |user_id, timeout| "lock:peruser:#{user_id}" }
79
+ name: proc { |user_id, timeout| "lock:peruser:#{user_id}" },
80
+ value: proc { |user_id, timeout| "#{user_id}" }
79
81
  }
80
82
 
81
83
  def perform(user_id, timeout)
@@ -83,7 +85,15 @@ class Worker
83
85
  # do some work
84
86
  # only at this point I want to acquire the lock
85
87
  if lock.acquire!
86
- # I can do the work
88
+ begin
89
+ # I can do the work
90
+ # ...
91
+ ensure
92
+ # You probably want to manually release lock after work is done
93
+ # This method can be safely called even if lock wasn't acquired
94
+ # by current worker (thread). For more references see RedisLock class
95
+ lock.release!
96
+ end
87
97
  else
88
98
  # reschedule, raise an error or do whatever you want
89
99
  end
@@ -65,7 +65,12 @@ module Sidekiq
65
65
  end
66
66
 
67
67
  def value
68
- @value ||= SecureRandom.hex(25)
68
+ @value ||= set_lock_value(options[:value])
69
+ end
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
69
74
  end
70
75
  end
71
76
  end
@@ -1,5 +1,5 @@
1
1
  module Sidekiq
2
2
  module Lock
3
- VERSION = "0.2.0"
3
+ VERSION = "0.3.0"
4
4
  end
5
5
  end
@@ -25,4 +25,6 @@ Gem::Specification.new do |spec|
25
25
  spec.add_development_dependency "rack-test"
26
26
  spec.add_development_dependency "coveralls", "~> 0.7.0"
27
27
  spec.add_development_dependency "mocha", "~> 0.14.0"
28
+ spec.add_development_dependency "minitest"
29
+ spec.add_development_dependency "redis-namespace"
28
30
  end
@@ -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
@@ -1,5 +1,7 @@
1
1
  require 'coveralls'
2
- Coveralls.wear!
2
+ Coveralls.wear! do
3
+ add_filter "/test/"
4
+ end
3
5
 
4
6
  Encoding.default_external = Encoding::UTF_8
5
7
  Encoding.default_internal = Encoding::UTF_8
metadata CHANGED
@@ -1,128 +1,141 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sidekiq-lock
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
5
- prerelease:
4
+ version: 0.3.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: 2013-12-08 00:00:00.000000000 Z
11
+ date: 2016-07-28 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
47
  version: '1.3'
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
54
  version: '1.3'
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
84
  name: coveralls
96
85
  requirement: !ruby/object:Gem::Requirement
97
- none: false
98
86
  requirements:
99
- - - ~>
87
+ - - "~>"
100
88
  - !ruby/object:Gem::Version
101
89
  version: 0.7.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
96
  version: 0.7.0
110
97
  - !ruby/object:Gem::Dependency
111
98
  name: mocha
112
99
  requirement: !ruby/object:Gem::Requirement
113
- none: false
114
100
  requirements:
115
- - - ~>
101
+ - - "~>"
116
102
  - !ruby/object:Gem::Version
117
103
  version: 0.14.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
110
  version: 0.14.0
111
+ - !ruby/object:Gem::Dependency
112
+ name: minitest
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'
125
+ - !ruby/object:Gem::Dependency
126
+ name: redis-namespace
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
126
139
  description: Simple redis-based lock mechanism for your sidekiq workers
127
140
  email:
128
141
  - rafal.wojsznis@gmail.com
@@ -130,8 +143,8 @@ executables: []
130
143
  extensions: []
131
144
  extra_rdoc_files: []
132
145
  files:
133
- - .gitignore
134
- - .travis.yml
146
+ - ".gitignore"
147
+ - ".travis.yml"
135
148
  - CHANGELOG.md
136
149
  - Gemfile
137
150
  - LICENSE.txt
@@ -154,33 +167,26 @@ files:
154
167
  homepage: https://github.com/emq/sidekiq-lock
155
168
  licenses:
156
169
  - MIT
170
+ metadata: {}
157
171
  post_install_message:
158
172
  rdoc_options: []
159
173
  require_paths:
160
174
  - lib
161
175
  required_ruby_version: !ruby/object:Gem::Requirement
162
- none: false
163
176
  requirements:
164
- - - ! '>='
177
+ - - ">="
165
178
  - !ruby/object:Gem::Version
166
179
  version: '0'
167
- segments:
168
- - 0
169
- hash: -2239756141932312381
170
180
  required_rubygems_version: !ruby/object:Gem::Requirement
171
- none: false
172
181
  requirements:
173
- - - ! '>='
182
+ - - ">="
174
183
  - !ruby/object:Gem::Version
175
184
  version: '0'
176
- segments:
177
- - 0
178
- hash: -2239756141932312381
179
185
  requirements: []
180
186
  rubyforge_project:
181
- rubygems_version: 1.8.25
187
+ rubygems_version: 2.5.1
182
188
  signing_key:
183
- specification_version: 3
189
+ specification_version: 4
184
190
  summary: Simple redis-based lock mechanism for your sidekiq workers
185
191
  test_files:
186
192
  - test/lib/middleware_test.rb