sidekiq-unique-jobs 6.0.25 → 7.0.0.beta2

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of sidekiq-unique-jobs might be problematic. Click here for more details.

Files changed (113) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +155 -20
  3. data/README.md +349 -112
  4. data/lib/sidekiq-unique-jobs.rb +2 -0
  5. data/lib/sidekiq_unique_jobs.rb +43 -6
  6. data/lib/sidekiq_unique_jobs/batch_delete.rb +121 -0
  7. data/lib/sidekiq_unique_jobs/changelog.rb +71 -0
  8. data/lib/sidekiq_unique_jobs/cli.rb +20 -29
  9. data/lib/sidekiq_unique_jobs/config.rb +193 -0
  10. data/lib/sidekiq_unique_jobs/connection.rb +5 -4
  11. data/lib/sidekiq_unique_jobs/constants.rb +36 -24
  12. data/lib/sidekiq_unique_jobs/core_ext.rb +38 -0
  13. data/lib/sidekiq_unique_jobs/digests.rb +78 -93
  14. data/lib/sidekiq_unique_jobs/exceptions.rb +152 -8
  15. data/lib/sidekiq_unique_jobs/job.rb +3 -3
  16. data/lib/sidekiq_unique_jobs/json.rb +34 -0
  17. data/lib/sidekiq_unique_jobs/key.rb +93 -0
  18. data/lib/sidekiq_unique_jobs/lock.rb +295 -0
  19. data/lib/sidekiq_unique_jobs/lock/base_lock.rb +49 -43
  20. data/lib/sidekiq_unique_jobs/lock/client_validator.rb +28 -0
  21. data/lib/sidekiq_unique_jobs/lock/server_validator.rb +27 -0
  22. data/lib/sidekiq_unique_jobs/lock/until_and_while_executing.rb +8 -17
  23. data/lib/sidekiq_unique_jobs/lock/until_executed.rb +5 -5
  24. data/lib/sidekiq_unique_jobs/lock/until_expired.rb +1 -23
  25. data/lib/sidekiq_unique_jobs/lock/validator.rb +65 -0
  26. data/lib/sidekiq_unique_jobs/lock/while_executing.rb +12 -8
  27. data/lib/sidekiq_unique_jobs/lock/while_executing_reject.rb +1 -1
  28. data/lib/sidekiq_unique_jobs/lock_config.rb +95 -0
  29. data/lib/sidekiq_unique_jobs/lock_info.rb +68 -0
  30. data/lib/sidekiq_unique_jobs/locksmith.rb +255 -99
  31. data/lib/sidekiq_unique_jobs/logging.rb +148 -22
  32. data/lib/sidekiq_unique_jobs/logging/middleware_context.rb +44 -0
  33. data/lib/sidekiq_unique_jobs/lua/delete.lua +51 -0
  34. data/lib/sidekiq_unique_jobs/lua/delete_by_digest.lua +46 -0
  35. data/lib/sidekiq_unique_jobs/lua/delete_job_by_digest.lua +38 -0
  36. data/lib/sidekiq_unique_jobs/lua/find_digest_in_queues.lua +26 -0
  37. data/lib/sidekiq_unique_jobs/lua/find_digest_in_sorted_set.lua +24 -0
  38. data/lib/sidekiq_unique_jobs/lua/lock.lua +91 -0
  39. data/lib/sidekiq_unique_jobs/lua/locked.lua +35 -0
  40. data/lib/sidekiq_unique_jobs/lua/queue.lua +83 -0
  41. data/lib/sidekiq_unique_jobs/lua/reap_orphans.lua +86 -0
  42. data/lib/sidekiq_unique_jobs/lua/shared/_common.lua +40 -0
  43. data/lib/sidekiq_unique_jobs/lua/shared/_current_time.lua +8 -0
  44. data/lib/sidekiq_unique_jobs/lua/shared/_delete_from_queue.lua +19 -0
  45. data/lib/sidekiq_unique_jobs/lua/shared/_delete_from_sorted_set.lua +18 -0
  46. data/lib/sidekiq_unique_jobs/lua/shared/_find_digest_in_queues.lua +46 -0
  47. data/lib/sidekiq_unique_jobs/lua/shared/_find_digest_in_sorted_set.lua +24 -0
  48. data/lib/sidekiq_unique_jobs/lua/shared/_hgetall.lua +13 -0
  49. data/lib/sidekiq_unique_jobs/lua/shared/_upgrades.lua +3 -0
  50. data/lib/sidekiq_unique_jobs/lua/shared/find_digest_in_sorted_set.lua +24 -0
  51. data/lib/sidekiq_unique_jobs/lua/unlock.lua +99 -0
  52. data/lib/sidekiq_unique_jobs/lua/update_version.lua +40 -0
  53. data/lib/sidekiq_unique_jobs/lua/upgrade.lua +68 -0
  54. data/lib/sidekiq_unique_jobs/middleware.rb +62 -31
  55. data/lib/sidekiq_unique_jobs/middleware/client.rb +42 -0
  56. data/lib/sidekiq_unique_jobs/middleware/server.rb +27 -0
  57. data/lib/sidekiq_unique_jobs/normalizer.rb +3 -3
  58. data/lib/sidekiq_unique_jobs/on_conflict.rb +22 -9
  59. data/lib/sidekiq_unique_jobs/on_conflict/log.rb +8 -4
  60. data/lib/sidekiq_unique_jobs/on_conflict/reject.rb +59 -13
  61. data/lib/sidekiq_unique_jobs/on_conflict/replace.rb +42 -13
  62. data/lib/sidekiq_unique_jobs/on_conflict/reschedule.rb +4 -4
  63. data/lib/sidekiq_unique_jobs/on_conflict/strategy.rb +24 -5
  64. data/lib/sidekiq_unique_jobs/options_with_fallback.rb +47 -23
  65. data/lib/sidekiq_unique_jobs/orphans/manager.rb +100 -0
  66. data/lib/sidekiq_unique_jobs/orphans/observer.rb +42 -0
  67. data/lib/sidekiq_unique_jobs/orphans/reaper.rb +201 -0
  68. data/lib/sidekiq_unique_jobs/profiler.rb +51 -0
  69. data/lib/sidekiq_unique_jobs/redis.rb +11 -0
  70. data/lib/sidekiq_unique_jobs/redis/entity.rb +94 -0
  71. data/lib/sidekiq_unique_jobs/redis/hash.rb +56 -0
  72. data/lib/sidekiq_unique_jobs/redis/list.rb +32 -0
  73. data/lib/sidekiq_unique_jobs/redis/set.rb +32 -0
  74. data/lib/sidekiq_unique_jobs/redis/sorted_set.rb +59 -0
  75. data/lib/sidekiq_unique_jobs/redis/string.rb +49 -0
  76. data/lib/sidekiq_unique_jobs/rspec/matchers.rb +19 -0
  77. data/lib/sidekiq_unique_jobs/rspec/matchers/have_valid_sidekiq_options.rb +43 -0
  78. data/lib/sidekiq_unique_jobs/{scripts.rb → script.rb} +43 -29
  79. data/lib/sidekiq_unique_jobs/script/caller.rb +125 -0
  80. data/lib/sidekiq_unique_jobs/script/template.rb +41 -0
  81. data/lib/sidekiq_unique_jobs/sidekiq_unique_ext.rb +92 -65
  82. data/lib/sidekiq_unique_jobs/sidekiq_unique_jobs.rb +166 -28
  83. data/lib/sidekiq_unique_jobs/sidekiq_worker_methods.rb +10 -11
  84. data/lib/sidekiq_unique_jobs/testing.rb +47 -15
  85. data/lib/sidekiq_unique_jobs/time_calculator.rb +103 -0
  86. data/lib/sidekiq_unique_jobs/timing.rb +58 -0
  87. data/lib/sidekiq_unique_jobs/unique_args.rb +19 -21
  88. data/lib/sidekiq_unique_jobs/unlockable.rb +11 -2
  89. data/lib/sidekiq_unique_jobs/update_version.rb +25 -0
  90. data/lib/sidekiq_unique_jobs/upgrade_locks.rb +151 -0
  91. data/lib/sidekiq_unique_jobs/version.rb +3 -1
  92. data/lib/sidekiq_unique_jobs/version_check.rb +1 -1
  93. data/lib/sidekiq_unique_jobs/web.rb +25 -19
  94. data/lib/sidekiq_unique_jobs/web/helpers.rb +98 -6
  95. data/lib/sidekiq_unique_jobs/web/views/lock.erb +108 -0
  96. data/lib/sidekiq_unique_jobs/web/views/locks.erb +52 -0
  97. data/lib/tasks/changelog.rake +4 -3
  98. metadata +70 -35
  99. data/lib/sidekiq_unique_jobs/client/middleware.rb +0 -56
  100. data/lib/sidekiq_unique_jobs/server/middleware.rb +0 -46
  101. data/lib/sidekiq_unique_jobs/timeout.rb +0 -8
  102. data/lib/sidekiq_unique_jobs/timeout/calculator.rb +0 -63
  103. data/lib/sidekiq_unique_jobs/util.rb +0 -103
  104. data/lib/sidekiq_unique_jobs/web/views/unique_digest.erb +0 -28
  105. data/lib/sidekiq_unique_jobs/web/views/unique_digests.erb +0 -46
  106. data/redis/acquire_lock.lua +0 -21
  107. data/redis/convert_legacy_lock.lua +0 -13
  108. data/redis/delete.lua +0 -14
  109. data/redis/delete_by_digest.lua +0 -23
  110. data/redis/delete_job_by_digest.lua +0 -60
  111. data/redis/lock.lua +0 -62
  112. data/redis/release_stale_locks.lua +0 -90
  113. data/redis/unlock.lua +0 -35
@@ -17,9 +17,9 @@ module SidekiqUniqueJobs
17
17
  private
18
18
 
19
19
  def add_timeout_and_expiration(item)
20
- calculator = SidekiqUniqueJobs::Timeout::Calculator.new(item)
21
- item[LOCK_TIMEOUT_KEY] = calculator.lock_timeout
22
- item[LOCK_EXPIRATION_KEY] = calculator.lock_expiration
20
+ calculator = SidekiqUniqueJobs::TimeCalculator.new(item)
21
+ item[LOCK_TIMEOUT] = calculator.lock_timeout
22
+ item[LOCK_TTL] = calculator.lock_ttl
23
23
  end
24
24
 
25
25
  def add_unique_args_and_digest(item)
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SidekiqUniqueJobs
4
+ # Handles loading and dumping of json
5
+ #
6
+ # @author Mikael Henriksson <mikael@zoolutions.se>
7
+ module JSON
8
+ module_function
9
+
10
+ #
11
+ # Parses a JSON string into an object
12
+ #
13
+ # @param [String] string the object to parse
14
+ #
15
+ # @return [Object]
16
+ #
17
+ def load_json(string)
18
+ return unless string && !string.empty?
19
+
20
+ ::JSON.parse(string)
21
+ end
22
+
23
+ #
24
+ # Dumps an object into a JSON string
25
+ #
26
+ # @param [Object] object a JSON convertible object
27
+ #
28
+ # @return [String] a JSON string
29
+ #
30
+ def dump_json(object)
31
+ ::JSON.generate(object)
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,93 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SidekiqUniqueJobs
4
+ # Key class wraps logic dealing with various lock keys
5
+ #
6
+ # @author Mikael Henriksson <mikael@zoolutions.se>
7
+ class Key
8
+ #
9
+ # @!attribute [r] digest
10
+ # @return [String] the digest key for which keys are created
11
+ attr_reader :digest
12
+ #
13
+ # @!attribute [r] queued
14
+ # @return [String] the list key with queued job_id's
15
+ attr_reader :queued
16
+ #
17
+ # @!attribute [r] primed
18
+ # @return [String] the list key with primed job_id's
19
+ attr_reader :primed
20
+ #
21
+ # @!attribute [r] locked
22
+ # @return [String] the hash key with locked job_id's
23
+ attr_reader :locked
24
+ #
25
+ # @!attribute [r] info
26
+ # @return [String] information about the lock
27
+ attr_reader :info
28
+ #
29
+ # @!attribute [r] changelog
30
+ # @return [String] the zset with changelog entries
31
+ attr_reader :changelog
32
+ #
33
+ # @!attribute [r] digests
34
+ # @return [String] the zset with locked digests
35
+ attr_reader :digests
36
+
37
+ #
38
+ # Initialize a new Key
39
+ #
40
+ # @param [String] digest the digest to use as key
41
+ #
42
+ def initialize(digest)
43
+ @digest = digest
44
+ @queued = suffixed_key("QUEUED")
45
+ @primed = suffixed_key("PRIMED")
46
+ @locked = suffixed_key("LOCKED")
47
+ @info = suffixed_key("INFO")
48
+ @changelog = CHANGELOGS
49
+ @digests = DIGESTS
50
+ end
51
+
52
+ #
53
+ # Provides the only important information about this keys
54
+ #
55
+ #
56
+ # @return [String]
57
+ #
58
+ def to_s
59
+ digest
60
+ end
61
+
62
+ # @see to_s
63
+ def inspect
64
+ digest
65
+ end
66
+
67
+ #
68
+ # Compares keys by digest
69
+ #
70
+ # @param [Key] other the key to compare with
71
+ #
72
+ # @return [true, false]
73
+ #
74
+ def ==(other)
75
+ digest == other.digest
76
+ end
77
+
78
+ #
79
+ # Returns all keys as an ordered array
80
+ #
81
+ # @return [Array] an ordered array with all keys
82
+ #
83
+ def to_a
84
+ [digest, queued, primed, locked, info, changelog, digests]
85
+ end
86
+
87
+ private
88
+
89
+ def suffixed_key(variable)
90
+ "#{digest}:#{variable}"
91
+ end
92
+ end
93
+ end
@@ -0,0 +1,295 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SidekiqUniqueJobs
4
+ #
5
+ # Class Lock provides access to information about a lock
6
+ #
7
+ # @author Mikael Henriksson <mikael@zoolutions.se>
8
+ #
9
+ class Lock # rubocop:disable Metrics/ClassLength
10
+ # includes "SidekiqUniqueJobs::Connection"
11
+ # @!parse include SidekiqUniqueJobs::Connection
12
+ include SidekiqUniqueJobs::Connection
13
+
14
+ # includes "SidekiqUniqueJobs::Timing"
15
+ # @!parse include SidekiqUniqueJobs::Timing
16
+ include SidekiqUniqueJobs::Timing
17
+
18
+ # includes "SidekiqUniqueJobs::JSON"
19
+ # @!parse include SidekiqUniqueJobs::JSON
20
+ include SidekiqUniqueJobs::JSON
21
+
22
+ #
23
+ # @!attribute [r] key
24
+ # @return [String] the entity redis key
25
+ attr_reader :key
26
+
27
+ #
28
+ # Initialize a locked lock
29
+ #
30
+ # @param [String] digest a unique digest
31
+ # @param [String] job_id a sidekiq JID
32
+ # @param [Hash] lock_info information about the lock
33
+ #
34
+ # @return [Lock] a newly lock that has been locked
35
+ #
36
+ def self.create(digest, job_id, lock_info = {})
37
+ lock = new(digest, time: Timing.now_f)
38
+ lock.lock(job_id, lock_info)
39
+ lock
40
+ end
41
+
42
+ #
43
+ # Initialize a new lock
44
+ #
45
+ # @param [String, Key] key either a digest or an instance of a {Key}
46
+ # @param [Timstamp, Float] time nil optional timestamp to initiate this lock with
47
+ #
48
+ def initialize(key, time: nil)
49
+ @key = get_key(key)
50
+ @created_at = time.is_a?(Float) ? time : time.to_f
51
+ end
52
+
53
+ #
54
+ # Locks a job_id
55
+ #
56
+ # @note intended only for testing purposez
57
+ #
58
+ # @param [String] job_id a sidekiq JID
59
+ # @param [Hash] lock_info information about the lock
60
+ #
61
+ # @return [void]
62
+ #
63
+ def lock(job_id, lock_info = {})
64
+ redis do |conn|
65
+ conn.multi do
66
+ conn.set(key.digest, job_id)
67
+ conn.hset(key.locked, job_id, now_f)
68
+ info.set(lock_info)
69
+ conn.zadd(key.digests, now_f, key.digest)
70
+ conn.zadd(key.changelog, now_f, changelog_json(job_id, "queue.lua", "Queued"))
71
+ conn.zadd(key.changelog, now_f, changelog_json(job_id, "lock.lua", "Locked"))
72
+ end
73
+ end
74
+ end
75
+
76
+ #
77
+ # Unlock a specific job_id
78
+ #
79
+ # @param [String] job_id a sidekiq JID
80
+ #
81
+ # @return [true] when job_id was removed
82
+ # @return [false] when job_id wasn't locked
83
+ #
84
+ def unlock(job_id)
85
+ locked.del(job_id)
86
+ end
87
+
88
+ #
89
+ # Deletes all the redis keys for this lock
90
+ #
91
+ #
92
+ # @return [Integer] the number of keys deleted in redis
93
+ #
94
+ def del
95
+ redis do |conn|
96
+ conn.multi do
97
+ conn.zrem(DIGESTS, key.digest)
98
+ conn.del(key.digest, key.queued, key.primed, key.locked, key.info)
99
+ end
100
+ end
101
+ end
102
+
103
+ #
104
+ # Returns either the time the lock was initialized with or
105
+ # the first changelog entry's timestamp
106
+ #
107
+ #
108
+ # @return [Float] a floaty timestamp represantation
109
+ #
110
+ def created_at
111
+ @created_at ||= changelogs.first&.[]("time")
112
+ end
113
+
114
+ #
115
+ # Returns all job_id's for this lock
116
+ #
117
+ # @note a JID can be present in 3 different places
118
+ #
119
+ #
120
+ # @return [Array<String>] an array with JIDs
121
+ #
122
+ def all_jids
123
+ (queued_jids + primed_jids + locked_jids).uniq
124
+ end
125
+
126
+ #
127
+ # Returns a collection of locked job_id's
128
+ #
129
+ # @param [true, false] with_values false provide the timestamp for the lock
130
+ #
131
+ # @return [Hash<String, Float>] when given `with_values: true`
132
+ # @return [Array<String>] when given `with_values: false`
133
+ #
134
+ def locked_jids(with_values: false)
135
+ locked.entries(with_values: with_values)
136
+ end
137
+
138
+ #
139
+ # Returns the queued JIDs
140
+ #
141
+ #
142
+ # @return [Array<String>] an array with queued job_ids
143
+ #
144
+ def queued_jids
145
+ queued.entries
146
+ end
147
+
148
+ #
149
+ # Returns the primed JIDs
150
+ #
151
+ #
152
+ # @return [Array<String>] an array with primed job_ids
153
+ #
154
+ def primed_jids
155
+ primed.entries
156
+ end
157
+
158
+ #
159
+ # Returns all matching changelog entries for this lock
160
+ #
161
+ #
162
+ # @return [Array<Hash>] an array with changelogs
163
+ #
164
+ def changelogs
165
+ changelog.entries(pattern: "*#{key.digest}*")
166
+ end
167
+
168
+ #
169
+ # The digest key
170
+ #
171
+ # @note Used for exists checks to avoid enqueuing
172
+ # the same lock twice
173
+ #
174
+ #
175
+ # @return [Redis::String] a string representation of the key
176
+ #
177
+ def digest
178
+ @digest ||= Redis::String.new(key.digest)
179
+ end
180
+
181
+ #
182
+ # The queued list
183
+ #
184
+ #
185
+ # @return [Redis::List] for queued JIDs
186
+ #
187
+ def queued
188
+ @queued ||= Redis::List.new(key.queued)
189
+ end
190
+
191
+ #
192
+ # The primed list
193
+ #
194
+ #
195
+ # @return [Redis::List] for primed JIDs
196
+ #
197
+ def primed
198
+ @primed ||= Redis::List.new(key.primed)
199
+ end
200
+
201
+ #
202
+ # The locked hash
203
+ #
204
+ #
205
+ # @return [Redis::Hash] for locked JIDs
206
+ #
207
+ def locked
208
+ @locked ||= Redis::Hash.new(key.locked)
209
+ end
210
+
211
+ #
212
+ # Information about the lock
213
+ #
214
+ #
215
+ # @return [Redis::Hash] with lock information
216
+ #
217
+ def info
218
+ @info ||= LockInfo.new(key.info)
219
+ end
220
+
221
+ #
222
+ # A sorted set with changelog entries
223
+ #
224
+ # @see Changelog for more information
225
+ #
226
+ #
227
+ # @return [Changelog]
228
+ #
229
+ def changelog
230
+ @changelog ||= Changelog.new
231
+ end
232
+
233
+ #
234
+ # A nicely formatted string with information about this lock
235
+ #
236
+ #
237
+ # @return [String]
238
+ #
239
+ def to_s
240
+ <<~MESSAGE
241
+ Lock status for #{key}
242
+
243
+ value: #{digest.value}
244
+ info: #{info.value}
245
+ queued_jids: #{queued_jids}
246
+ primed_jids: #{primed_jids}
247
+ locked_jids: #{locked_jids}
248
+ changelogs: #{changelogs}
249
+ MESSAGE
250
+ end
251
+
252
+ #
253
+ # @see to_s
254
+ #
255
+ def inspect
256
+ to_s
257
+ end
258
+
259
+ private
260
+
261
+ #
262
+ # Ensure the key is a {Key}
263
+ #
264
+ # @param [String, Key] key
265
+ #
266
+ # @return [Key]
267
+ #
268
+ def get_key(key)
269
+ if key.is_a?(SidekiqUniqueJobs::Key)
270
+ key
271
+ else
272
+ SidekiqUniqueJobs::Key.new(key)
273
+ end
274
+ end
275
+
276
+ #
277
+ # Generate a changelog entry for the given arguments
278
+ #
279
+ # @param [String] job_id a sidekiq JID
280
+ # @param [String] script the name of the script generating this entry
281
+ # @param [String] message a descriptive message for later review
282
+ #
283
+ # @return [String] a JSON string matching the Lua script structure
284
+ #
285
+ def changelog_json(job_id, script, message)
286
+ dump_json(
287
+ digest: key.digest,
288
+ job_id: job_id,
289
+ script: script,
290
+ message: message,
291
+ time: now_f,
292
+ )
293
+ end
294
+ end
295
+ end
@@ -9,6 +9,17 @@ module SidekiqUniqueJobs
9
9
  class BaseLock
10
10
  include SidekiqUniqueJobs::Logging
11
11
 
12
+ #
13
+ # Validates that the sidekiq_options for the worker is valid
14
+ #
15
+ # @param [Hash] options the sidekiq_options given to the worker
16
+ #
17
+ # @return [void]
18
+ #
19
+ def self.validate_options(options = {})
20
+ Validator.validate(options)
21
+ end
22
+
12
23
  # @param [Hash] item the Sidekiq job hash
13
24
  # @param [Proc] callback the callback to use after unlock
14
25
  # @param [Sidekiq::RedisConnection, ConnectionPool] redis_pool the redis connection
@@ -16,21 +27,24 @@ module SidekiqUniqueJobs
16
27
  @item = item
17
28
  @callback = callback
18
29
  @redis_pool = redis_pool
30
+ @attempt = 0
19
31
  add_uniqueness_when_missing # Used to ease testing
20
32
  end
21
33
 
22
- # Handles locking of sidekiq jobs.
23
- # Will call a conflict strategy if lock can't be achieved.
24
- # @return [String] the sidekiq job id
25
- def lock
26
- @attempt = 0
27
- return item[JID_KEY] if locked?
28
-
29
- if (token = locksmith.lock(item[LOCK_TIMEOUT_KEY]))
30
- token
31
- else
32
- call_strategy
33
- end
34
+ #
35
+ # Locks a sidekiq job
36
+ #
37
+ # @note Will call a conflict strategy if lock can't be achieved.
38
+ #
39
+ # @return [String, nil] the locked jid when properly locked, else nil.
40
+ #
41
+ # @yield to the caller when given a block
42
+ #
43
+ def lock(&block)
44
+ # TODO: only use replace strategy when server is executing the lock
45
+ return call_strategy unless (locked_token = locksmith.lock(&block))
46
+
47
+ locked_token
34
48
  end
35
49
 
36
50
  # Execute the job in the Sidekiq server processor
@@ -43,7 +57,7 @@ module SidekiqUniqueJobs
43
57
  # @return [String] sidekiq job id when successful
44
58
  # @return [false] when unsuccessful
45
59
  def unlock
46
- locksmith.unlock(item[JID_KEY]) # Only signal to release the lock
60
+ locksmith.unlock # Only signal to release the lock
47
61
  end
48
62
 
49
63
  # Deletes the job from redis if it is locked.
@@ -61,13 +75,23 @@ module SidekiqUniqueJobs
61
75
  # @return [true] when this jid has locked the job
62
76
  # @return [false] when this jid has not locked the job
63
77
  def locked?
64
- locksmith.locked?(item[JID_KEY])
78
+ locksmith.locked?
79
+ end
80
+
81
+ #
82
+ # The lock manager/client
83
+ #
84
+ # @api private
85
+ # @return [SidekiqUniqueJobs::Locksmith] the locksmith for this sidekiq job
86
+ #
87
+ def locksmith
88
+ @locksmith ||= SidekiqUniqueJobs::Locksmith.new(item, redis_pool)
65
89
  end
66
90
 
67
91
  private
68
92
 
69
93
  def add_uniqueness_when_missing
70
- return if item.key?(UNIQUE_DIGEST_KEY)
94
+ return if item.key?(UNIQUE_DIGEST)
71
95
 
72
96
  # The below should only be done to ease testing
73
97
  # in production this will be done by the middleware
@@ -83,53 +107,35 @@ module SidekiqUniqueJobs
83
107
  strategy.replace? && attempt < 2
84
108
  end
85
109
 
86
- # The sidekiq job hash
87
- # @return [Hash] the Sidekiq job hash
110
+ # @!attribute [r] item
111
+ # @return [Hash<String, Object>] the Sidekiq job hash
88
112
  attr_reader :item
89
-
90
- # The sidekiq redis pool
91
- # @return [Sidekiq::RedisConnection, ConnectionPool, NilClass] the redis connection
113
+ # @!attribute [r] redis_pool
114
+ # @return [Sidekiq::RedisConnection, ConnectionPool, NilClass] the redis connection
92
115
  attr_reader :redis_pool
93
-
94
- # The sidekiq job hash
95
- # @return [Proc] the callback to use after unlock
116
+ # @!attribute [r] callback
117
+ # @return [Proc] the block to call after unlock
96
118
  attr_reader :callback
97
-
98
- # The current attempt to lock the job
99
- # @return [Integer] the numerical value of the attempt
119
+ # @!attribute [r] attempt
120
+ # @return [Integer] the current locking attempt
100
121
  attr_reader :attempt
101
122
 
102
- # The interface to the locking mechanism
103
- # @return [SidekiqUniqueJobs::Locksmith]
104
- def locksmith
105
- @locksmith ||= SidekiqUniqueJobs::Locksmith.new(item, redis_pool)
106
- end
107
-
108
- def with_cleanup
109
- yield
110
- rescue Sidekiq::Shutdown
111
- log_info("Sidekiq is shutting down, the job `should` be put back on the queue. Keeping the lock!")
112
- raise
113
- else
114
- unlock_with_callback
115
- end
116
-
117
123
  def unlock_with_callback
118
124
  return log_warn("might need to be unlocked manually") unless unlock
119
125
 
120
126
  callback_safely
127
+ item[JID]
121
128
  end
122
129
 
123
130
  def callback_safely
124
131
  callback&.call
125
- item[JID_KEY]
126
132
  rescue StandardError
127
133
  log_warn("unlocked successfully but the #after_unlock callback failed!")
128
134
  raise
129
135
  end
130
136
 
131
137
  def strategy
132
- @strategy ||= OnConflict.find_strategy(item[ON_CONFLICT_KEY]).new(item)
138
+ @strategy ||= OnConflict.find_strategy(item[ON_CONFLICT]).new(item, redis_pool)
133
139
  end
134
140
  end
135
141
  end