sidekiq-unique-jobs 6.0.25 → 7.1.29

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 (129) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +1157 -41
  3. data/README.md +825 -291
  4. data/lib/sidekiq_unique_jobs/batch_delete.rb +124 -0
  5. data/lib/sidekiq_unique_jobs/changelog.rb +78 -0
  6. data/lib/sidekiq_unique_jobs/cli.rb +57 -29
  7. data/lib/sidekiq_unique_jobs/config.rb +319 -0
  8. data/lib/sidekiq_unique_jobs/connection.rb +6 -5
  9. data/lib/sidekiq_unique_jobs/constants.rb +46 -25
  10. data/lib/sidekiq_unique_jobs/core_ext.rb +80 -0
  11. data/lib/sidekiq_unique_jobs/deprecation.rb +65 -0
  12. data/lib/sidekiq_unique_jobs/digests.rb +70 -102
  13. data/lib/sidekiq_unique_jobs/exceptions.rb +88 -12
  14. data/lib/sidekiq_unique_jobs/expiring_digests.rb +14 -0
  15. data/lib/sidekiq_unique_jobs/job.rb +41 -12
  16. data/lib/sidekiq_unique_jobs/json.rb +47 -0
  17. data/lib/sidekiq_unique_jobs/key.rb +98 -0
  18. data/lib/sidekiq_unique_jobs/lock/base_lock.rb +111 -82
  19. data/lib/sidekiq_unique_jobs/lock/client_validator.rb +28 -0
  20. data/lib/sidekiq_unique_jobs/lock/server_validator.rb +27 -0
  21. data/lib/sidekiq_unique_jobs/lock/until_and_while_executing.rb +40 -15
  22. data/lib/sidekiq_unique_jobs/lock/until_executed.rb +29 -7
  23. data/lib/sidekiq_unique_jobs/lock/until_executing.rb +26 -2
  24. data/lib/sidekiq_unique_jobs/lock/until_expired.rb +27 -15
  25. data/lib/sidekiq_unique_jobs/lock/validator.rb +96 -0
  26. data/lib/sidekiq_unique_jobs/lock/while_executing.rb +26 -12
  27. data/lib/sidekiq_unique_jobs/lock/while_executing_reject.rb +3 -3
  28. data/lib/sidekiq_unique_jobs/lock.rb +342 -0
  29. data/lib/sidekiq_unique_jobs/lock_args.rb +127 -0
  30. data/lib/sidekiq_unique_jobs/lock_config.rb +126 -0
  31. data/lib/sidekiq_unique_jobs/lock_digest.rb +79 -0
  32. data/lib/sidekiq_unique_jobs/lock_info.rb +68 -0
  33. data/lib/sidekiq_unique_jobs/lock_timeout.rb +62 -0
  34. data/lib/sidekiq_unique_jobs/lock_ttl.rb +77 -0
  35. data/lib/sidekiq_unique_jobs/locksmith.rb +295 -101
  36. data/lib/sidekiq_unique_jobs/logging/middleware_context.rb +44 -0
  37. data/lib/sidekiq_unique_jobs/logging.rb +202 -33
  38. data/lib/sidekiq_unique_jobs/lua/delete.lua +51 -0
  39. data/lib/sidekiq_unique_jobs/lua/delete_by_digest.lua +42 -0
  40. data/lib/sidekiq_unique_jobs/lua/delete_job_by_digest.lua +38 -0
  41. data/lib/sidekiq_unique_jobs/lua/find_digest_in_queues.lua +26 -0
  42. data/lib/sidekiq_unique_jobs/lua/lock.lua +99 -0
  43. data/lib/sidekiq_unique_jobs/lua/lock_until_expired.lua +92 -0
  44. data/lib/sidekiq_unique_jobs/lua/locked.lua +35 -0
  45. data/lib/sidekiq_unique_jobs/lua/queue.lua +87 -0
  46. data/lib/sidekiq_unique_jobs/lua/reap_orphans.lua +122 -0
  47. data/lib/sidekiq_unique_jobs/lua/shared/_common.lua +40 -0
  48. data/lib/sidekiq_unique_jobs/lua/shared/_current_time.lua +8 -0
  49. data/lib/sidekiq_unique_jobs/lua/shared/_delete_from_queue.lua +22 -0
  50. data/lib/sidekiq_unique_jobs/lua/shared/_delete_from_sorted_set.lua +18 -0
  51. data/lib/sidekiq_unique_jobs/lua/shared/_find_digest_in_process_set.lua +53 -0
  52. data/lib/sidekiq_unique_jobs/lua/shared/_find_digest_in_queues.lua +43 -0
  53. data/lib/sidekiq_unique_jobs/lua/shared/_find_digest_in_sorted_set.lua +24 -0
  54. data/lib/sidekiq_unique_jobs/lua/shared/_hgetall.lua +13 -0
  55. data/lib/sidekiq_unique_jobs/lua/shared/_upgrades.lua +3 -0
  56. data/lib/sidekiq_unique_jobs/lua/unlock.lua +107 -0
  57. data/lib/sidekiq_unique_jobs/lua/update_version.lua +40 -0
  58. data/lib/sidekiq_unique_jobs/lua/upgrade.lua +68 -0
  59. data/lib/sidekiq_unique_jobs/middleware/client.rb +42 -0
  60. data/lib/sidekiq_unique_jobs/middleware/server.rb +31 -0
  61. data/lib/sidekiq_unique_jobs/middleware.rb +29 -43
  62. data/lib/sidekiq_unique_jobs/normalizer.rb +4 -4
  63. data/lib/sidekiq_unique_jobs/on_conflict/log.rb +9 -5
  64. data/lib/sidekiq_unique_jobs/on_conflict/null_strategy.rb +1 -1
  65. data/lib/sidekiq_unique_jobs/on_conflict/raise.rb +1 -1
  66. data/lib/sidekiq_unique_jobs/on_conflict/reject.rb +63 -17
  67. data/lib/sidekiq_unique_jobs/on_conflict/replace.rb +54 -14
  68. data/lib/sidekiq_unique_jobs/on_conflict/reschedule.rb +16 -5
  69. data/lib/sidekiq_unique_jobs/on_conflict/strategy.rb +25 -6
  70. data/lib/sidekiq_unique_jobs/on_conflict.rb +23 -10
  71. data/lib/sidekiq_unique_jobs/options_with_fallback.rb +38 -35
  72. data/lib/sidekiq_unique_jobs/orphans/lua_reaper.rb +29 -0
  73. data/lib/sidekiq_unique_jobs/orphans/manager.rb +241 -0
  74. data/lib/sidekiq_unique_jobs/orphans/null_reaper.rb +24 -0
  75. data/lib/sidekiq_unique_jobs/orphans/observer.rb +42 -0
  76. data/lib/sidekiq_unique_jobs/orphans/reaper.rb +114 -0
  77. data/lib/sidekiq_unique_jobs/orphans/reaper_resurrector.rb +170 -0
  78. data/lib/sidekiq_unique_jobs/orphans/ruby_reaper.rb +298 -0
  79. data/lib/sidekiq_unique_jobs/redis/entity.rb +112 -0
  80. data/lib/sidekiq_unique_jobs/redis/hash.rb +56 -0
  81. data/lib/sidekiq_unique_jobs/redis/list.rb +32 -0
  82. data/lib/sidekiq_unique_jobs/redis/set.rb +32 -0
  83. data/lib/sidekiq_unique_jobs/redis/sorted_set.rb +86 -0
  84. data/lib/sidekiq_unique_jobs/redis/string.rb +51 -0
  85. data/lib/sidekiq_unique_jobs/redis.rb +11 -0
  86. data/lib/sidekiq_unique_jobs/reflectable.rb +26 -0
  87. data/lib/sidekiq_unique_jobs/reflections.rb +79 -0
  88. data/lib/sidekiq_unique_jobs/rspec/matchers/have_valid_sidekiq_options.rb +51 -0
  89. data/lib/sidekiq_unique_jobs/rspec/matchers.rb +26 -0
  90. data/lib/sidekiq_unique_jobs/script/caller.rb +127 -0
  91. data/lib/sidekiq_unique_jobs/script.rb +15 -0
  92. data/lib/sidekiq_unique_jobs/server.rb +61 -0
  93. data/lib/sidekiq_unique_jobs/sidekiq_unique_ext.rb +114 -65
  94. data/lib/sidekiq_unique_jobs/sidekiq_unique_jobs.rb +251 -35
  95. data/lib/sidekiq_unique_jobs/sidekiq_worker_methods.rb +47 -32
  96. data/lib/sidekiq_unique_jobs/testing.rb +102 -29
  97. data/lib/sidekiq_unique_jobs/timer_task.rb +299 -0
  98. data/lib/sidekiq_unique_jobs/timing.rb +58 -0
  99. data/lib/sidekiq_unique_jobs/unlockable.rb +20 -4
  100. data/lib/sidekiq_unique_jobs/update_version.rb +25 -0
  101. data/lib/sidekiq_unique_jobs/upgrade_locks.rb +155 -0
  102. data/lib/sidekiq_unique_jobs/version.rb +3 -1
  103. data/lib/sidekiq_unique_jobs/version_check.rb +23 -4
  104. data/lib/sidekiq_unique_jobs/web/helpers.rb +138 -13
  105. data/lib/sidekiq_unique_jobs/web/views/_paging.erb +4 -4
  106. data/lib/sidekiq_unique_jobs/web/views/changelogs.erb +54 -0
  107. data/lib/sidekiq_unique_jobs/web/views/lock.erb +108 -0
  108. data/lib/sidekiq_unique_jobs/web/views/locks.erb +54 -0
  109. data/lib/sidekiq_unique_jobs/web.rb +76 -27
  110. data/lib/sidekiq_unique_jobs.rb +53 -7
  111. data/lib/tasks/changelog.rake +16 -16
  112. metadata +133 -177
  113. data/lib/sidekiq_unique_jobs/client/middleware.rb +0 -56
  114. data/lib/sidekiq_unique_jobs/scripts.rb +0 -118
  115. data/lib/sidekiq_unique_jobs/server/middleware.rb +0 -46
  116. data/lib/sidekiq_unique_jobs/timeout/calculator.rb +0 -63
  117. data/lib/sidekiq_unique_jobs/timeout.rb +0 -8
  118. data/lib/sidekiq_unique_jobs/unique_args.rb +0 -150
  119. data/lib/sidekiq_unique_jobs/util.rb +0 -103
  120. data/lib/sidekiq_unique_jobs/web/views/unique_digest.erb +0 -28
  121. data/lib/sidekiq_unique_jobs/web/views/unique_digests.erb +0 -46
  122. data/redis/acquire_lock.lua +0 -21
  123. data/redis/convert_legacy_lock.lua +0 -13
  124. data/redis/delete.lua +0 -14
  125. data/redis/delete_by_digest.lua +0 -23
  126. data/redis/delete_job_by_digest.lua +0 -60
  127. data/redis/lock.lua +0 -62
  128. data/redis/release_stale_locks.lua +0 -90
  129. data/redis/unlock.lua +0 -35
@@ -3,64 +3,109 @@
3
3
  module SidekiqUniqueJobs
4
4
  # Lock manager class that handles all the various locks
5
5
  #
6
- # @author Mikael Henriksson <mikael@zoolutions.se>
7
- # rubocop:disable Metrics/ClassLength
8
- class Locksmith
6
+ # @author Mikael Henriksson <mikael@mhenrixon.com>
7
+ class Locksmith # rubocop:disable Metrics/ClassLength
8
+ # includes "SidekiqUniqueJobs::Connection"
9
+ # @!parse include SidekiqUniqueJobs::Connection
9
10
  include SidekiqUniqueJobs::Connection
10
11
 
12
+ # includes "SidekiqUniqueJobs::Logging"
13
+ # @!parse include SidekiqUniqueJobs::Logging
14
+ include SidekiqUniqueJobs::Logging
15
+
16
+ # includes "SidekiqUniqueJobs::Reflectable"
17
+ # @!parse include SidekiqUniqueJobs::Reflectable
18
+ include SidekiqUniqueJobs::Reflectable
19
+
20
+ # includes "SidekiqUniqueJobs::Timing"
21
+ # @!parse include SidekiqUniqueJobs::Timing
22
+ include SidekiqUniqueJobs::Timing
23
+
24
+ # includes "SidekiqUniqueJobs::Script::Caller"
25
+ # @!parse include SidekiqUniqueJobs::Script::Caller
26
+ include SidekiqUniqueJobs::Script::Caller
27
+
28
+ # includes "SidekiqUniqueJobs::JSON"
29
+ # @!parse include SidekiqUniqueJobs::JSON
30
+ include SidekiqUniqueJobs::JSON
31
+
32
+ #
33
+ # @return [Float] used to take into consideration the inaccuracy of redis timestamps
34
+ CLOCK_DRIFT_FACTOR = 0.01
35
+ NETWORK_FACTOR = 0.04
36
+
37
+ #
38
+ # @!attribute [r] key
39
+ # @return [Key] the key used for locking
40
+ attr_reader :key
41
+ #
42
+ # @!attribute [r] job_id
43
+ # @return [String] a sidekiq JID
44
+ attr_reader :job_id
45
+ #
46
+ # @!attribute [r] config
47
+ # @return [LockConfig] the configuration for this lock
48
+ attr_reader :config
49
+ #
50
+ # @!attribute [r] item
51
+ # @return [Hash] a sidekiq job hash
52
+ attr_reader :item
53
+
54
+ #
55
+ # Initialize a new Locksmith instance
56
+ #
11
57
  # @param [Hash] item a Sidekiq job hash
12
- # @option item [Integer] :lock_expiration the configured expiration
58
+ # @option item [Integer] :lock_ttl the configured expiration
13
59
  # @option item [String] :jid the sidekiq job id
14
- # @option item [String] :unique_digest the unique digest (See: {UniqueArgs#unique_digest})
60
+ # @option item [String] :unique_digest the unique digest (See: {LockDigest#lock_digest})
15
61
  # @param [Sidekiq::RedisConnection, ConnectionPool] redis_pool the redis connection
62
+ #
16
63
  def initialize(item, redis_pool = nil)
17
- # @concurrency = 1 # removed in a0cff5bc42edbe7190d6ede7e7f845074d2d7af6
18
- @ttl = item[LOCK_EXPIRATION_KEY] || item[LOCK_TTL_KEY]
19
- @jid = item[JID_KEY]
20
- @unique_digest = item[UNIQUE_DIGEST_KEY] || item[LOCK_DIGEST_KEY]
21
- @lock_type = item[LOCK_KEY] || item[UNIQUE_KEY]
22
- @lock_type &&= @lock_type.to_sym
23
- @redis_pool = redis_pool
64
+ @item = item
65
+ @key = Key.new(item[LOCK_DIGEST] || item[UNIQUE_DIGEST]) # fallback until can be removed
66
+ @job_id = item[JID]
67
+ @config = LockConfig.new(item)
68
+ @redis_pool = redis_pool
24
69
  end
25
70
 
26
71
  #
27
- # Deletes the lock unless it has a ttl set
72
+ # Deletes the lock unless it has a pttl set
28
73
  #
29
74
  #
30
75
  def delete
31
- return if ttl
76
+ return if config.pttl.positive?
32
77
 
33
78
  delete!
34
79
  end
35
80
 
36
- # Deletes the lock regardless of if it has a ttl set
81
+ #
82
+ # Deletes the lock regardless of if it has a pttl set
83
+ #
37
84
  def delete!
38
- Scripts.call(
39
- :delete,
40
- redis_pool,
41
- keys: [exists_key, grabbed_key, available_key, version_key, UNIQUE_SET, unique_digest],
42
- )
85
+ call_script(:delete, key.to_a, [job_id, config.pttl, config.type, config.limit]).to_i.positive?
43
86
  end
44
87
 
45
88
  #
46
- # Create a lock for the item
89
+ # Create a lock for the Sidekiq job
47
90
  #
48
- # @param [Integer] timeout the number of seconds to wait for a lock.
91
+ # @return [String] the Sidekiq job_id that was locked/queued
49
92
  #
50
- # @return [String] the Sidekiq job_id (jid)
51
- #
52
- #
53
- def lock(timeout = nil, &block)
54
- Scripts.call(:lock, redis_pool,
55
- keys: [exists_key, grabbed_key, available_key, UNIQUE_SET, unique_digest],
56
- argv: [jid, ttl, lock_type])
93
+ def lock(wait: nil)
94
+ method_name = wait ? :primed_async : :primed_sync
95
+ redis(redis_pool) do |conn|
96
+ lock!(conn, method(method_name), wait) do
97
+ return job_id
98
+ end
99
+ end
100
+ end
101
+
102
+ def execute(&block)
103
+ raise SidekiqUniqueJobs::InvalidArgument, "#execute needs a block" unless block
57
104
 
58
- grab_token(timeout) do |token|
59
- touch_grabbed_token(token)
60
- return_token_or_block_value(token, &block)
105
+ redis(redis_pool) do |conn|
106
+ lock!(conn, method(:primed_async), &block)
61
107
  end
62
108
  end
63
- alias wait lock
64
109
 
65
110
  #
66
111
  # Removes the lock keys from Redis if locked by the provided jid/token
@@ -68,122 +113,271 @@ module SidekiqUniqueJobs
68
113
  # @return [false] unless locked?
69
114
  # @return [String] Sidekiq job_id (jid) if successful
70
115
  #
71
- def unlock(token = nil)
72
- token ||= jid
73
- return false unless locked?(token)
116
+ def unlock(conn = nil)
117
+ return false unless locked?(conn)
74
118
 
75
- unlock!(token)
119
+ unlock!(conn)
76
120
  end
77
121
 
78
122
  #
79
123
  # Removes the lock keys from Redis
80
124
  #
81
- # @param [String] token the token to unlock (defaults to jid)
82
- #
83
125
  # @return [false] unless locked?
84
126
  # @return [String] Sidekiq job_id (jid) if successful
85
127
  #
86
- def unlock!(token = nil)
87
- token ||= jid
128
+ def unlock!(conn = nil)
129
+ call_script(:unlock, key.to_a, argv, conn) do |unlocked_jid|
130
+ reflect(:debug, :unlocked, item, unlocked_jid) if unlocked_jid == job_id
88
131
 
89
- Scripts.call(
90
- :unlock,
91
- redis_pool,
92
- keys: [exists_key, grabbed_key, available_key, version_key, UNIQUE_SET, unique_digest],
93
- argv: [token, ttl, lock_type],
94
- )
132
+ unlocked_jid
133
+ end
95
134
  end
96
135
 
136
+ # Checks if this instance is considered locked
97
137
  #
98
- # @param [String] token the unique token to check for a lock.
99
- # nil will default to the jid provided in the initializer
100
- # @return [true, false]
138
+ # @param [Sidekiq::RedisConnection, ConnectionPool] conn the redis connection
101
139
  #
102
- # Checks if this instance is considered locked
140
+ # @return [true, false] true when the :LOCKED hash contains the job_id
141
+ #
142
+ def locked?(conn = nil)
143
+ return taken?(conn) if conn
144
+
145
+ redis { |rcon| taken?(rcon) }
146
+ end
147
+
148
+ #
149
+ # Nicely formatted string with information about self
103
150
  #
104
- # @param [<type>] token <description>
105
151
  #
106
- # @return [<type>] <description>
152
+ # @return [String]
107
153
  #
108
- def locked?(token = nil)
109
- token ||= jid
154
+ def to_s
155
+ "Locksmith##{object_id}(digest=#{key} job_id=#{job_id} locked=#{locked?})"
156
+ end
157
+
158
+ #
159
+ # @see to_s
160
+ #
161
+ def inspect
162
+ to_s
163
+ end
110
164
 
111
- convert_legacy_lock(token)
112
- redis(redis_pool) { |conn| conn.hexists(grabbed_key, token) }
165
+ #
166
+ # Compare this locksmith with another
167
+ #
168
+ # @param [Locksmith] other the locksmith to compare with
169
+ #
170
+ # @return [true, false]
171
+ #
172
+ def ==(other)
173
+ key == other.key && job_id == other.job_id
113
174
  end
114
175
 
115
176
  private
116
177
 
117
- attr_reader :unique_digest, :ttl, :jid, :redis_pool, :lock_type
178
+ attr_reader :redis_pool
118
179
 
119
- def convert_legacy_lock(token)
120
- Scripts.call(
121
- :convert_legacy_lock,
122
- redis_pool,
123
- keys: [grabbed_key, unique_digest],
124
- argv: [token, current_time.to_f],
125
- )
126
- end
180
+ #
181
+ # Used to reduce some duplication from the two methods
182
+ #
183
+ # @see lock
184
+ # @see execute
185
+ #
186
+ # @param [Sidekiq::RedisConnection, ConnectionPool] conn the redis connection
187
+ # @param [Method] primed_method reference to the method to use for getting a primed token
188
+ # @param [nil, Integer, Float] time to wait before timeout
189
+ #
190
+ # @yieldparam [string] job_id the sidekiq JID
191
+ # @yieldreturn [void] whatever the calling block returns
192
+ def lock!(conn, primed_method, wait = nil)
193
+ return yield if locked?(conn)
127
194
 
128
- def grab_token(timeout = nil)
129
- redis(redis_pool) do |conn|
130
- if timeout.nil? || timeout.positive?
131
- # passing timeout 0 to blpop causes it to block
132
- _key, token = conn.blpop(available_key, timeout || 0)
133
- else
134
- token = conn.lpop(available_key)
195
+ enqueue(conn) do |queued_jid|
196
+ reflect(:debug, :queued, item, queued_jid)
197
+
198
+ primed_method.call(conn, wait) do |primed_jid|
199
+ reflect(:debug, :primed, item, primed_jid)
200
+ locked_jid = call_script(:lock, key.to_a, argv, conn)
201
+
202
+ if locked_jid
203
+ reflect(:debug, :locked, item, locked_jid)
204
+ return yield
205
+ end
135
206
  end
207
+ end
208
+ end
136
209
 
137
- return yield jid if token
210
+ #
211
+ # Prepares all the various lock data
212
+ #
213
+ # @param [Redis] conn a redis connection
214
+ #
215
+ # @return [nil] when redis was already prepared for this lock
216
+ # @return [yield<String>] when successfully enqueued
217
+ #
218
+ def enqueue(conn)
219
+ queued_jid, elapsed = timed do
220
+ call_script(:queue, key.to_a, argv, conn)
138
221
  end
222
+
223
+ return unless queued_jid
224
+ return unless [job_id, "1"].include?(queued_jid)
225
+
226
+ validity = config.pttl - elapsed - drift(config.pttl)
227
+ return unless validity >= 0 || config.pttl.zero?
228
+
229
+ write_lock_info(conn)
230
+ yield job_id
139
231
  end
140
232
 
141
- def touch_grabbed_token(token)
142
- redis(redis_pool) do |conn|
143
- conn.hset(grabbed_key, token, current_time.to_f)
144
- conn.expire(grabbed_key, ttl) if ttl && lock_type == :until_expired
233
+ #
234
+ # Pops an enqueued token
235
+ # @note Used for runtime locks to avoid problems with blocking commands
236
+ # in current thread
237
+ #
238
+ # @param [Redis] conn a redis connection
239
+ #
240
+ # @return [nil] when lock was not possible
241
+ # @return [Object] whatever the block returns when lock was acquired
242
+ #
243
+ def primed_async(conn, wait = nil, &block)
244
+ timeout = (wait || config.timeout).to_i
245
+ timeout = 1 if timeout.zero?
246
+
247
+ brpoplpush_timeout = timeout
248
+ concurrent_timeout = add_drift(timeout)
249
+
250
+ reflect(:debug, :timeouts, item,
251
+ timeouts: { brpoplpush_timeout: brpoplpush_timeout, concurrent_timeout: concurrent_timeout })
252
+
253
+ primed_jid = Concurrent::Promises
254
+ .future(conn) { |red_con| pop_queued(red_con, timeout) }
255
+ .value
256
+
257
+ handle_primed(primed_jid, &block)
258
+ end
259
+
260
+ #
261
+ # Pops an enqueued token
262
+ # @note Used for non-runtime locks
263
+ #
264
+ # @param [Redis] conn a redis connection
265
+ #
266
+ # @return [nil] when lock was not possible
267
+ # @return [Object] whatever the block returns when lock was acquired
268
+ #
269
+ def primed_sync(conn, wait = nil, &block)
270
+ primed_jid = pop_queued(conn, wait)
271
+ handle_primed(primed_jid, &block)
272
+ end
273
+
274
+ def handle_primed(primed_jid)
275
+ return yield job_id if [job_id, "1"].include?(primed_jid)
276
+
277
+ reflect(:timeout, item) unless config.wait_for_lock?
278
+ end
279
+
280
+ #
281
+ # Does the actual popping of the enqueued token
282
+ #
283
+ # @param [Redis] conn a redis connection
284
+ #
285
+ # @return [String] a previously enqueued token (now taken off the queue)
286
+ #
287
+ def pop_queued(conn, wait = 1)
288
+ wait ||= config.timeout if config.wait_for_lock?
289
+
290
+ if wait.nil?
291
+ rpoplpush(conn)
292
+ else
293
+ brpoplpush(conn, wait)
145
294
  end
146
295
  end
147
296
 
148
- def return_token_or_block_value(token)
149
- return token unless block_given?
297
+ #
298
+ # @api private
299
+ #
300
+ def brpoplpush(conn, wait)
301
+ # passing timeout 0 to brpoplpush causes it to block indefinitely
302
+ raise InvalidArgument, "wait must be an integer" unless wait.is_a?(Integer)
303
+ return conn.brpoplpush(key.queued, key.primed, wait) if conn.class.to_s == "Redis::Namespace"
150
304
 
151
- # The reason for begin is to only signal when we have a block
152
- begin
153
- yield token
154
- ensure
155
- unlock(token)
305
+ if VersionCheck.satisfied?(redis_version, ">= 6.2.0") && conn.respond_to?(:blmove)
306
+ conn.blmove(key.queued, key.primed, "RIGHT", "LEFT", timeout: wait)
307
+ else
308
+ conn.brpoplpush(key.queued, key.primed, timeout: wait)
156
309
  end
157
310
  end
158
311
 
159
- def available_key
160
- @available_key ||= namespaced_key("AVAILABLE")
312
+ #
313
+ # @api private
314
+ #
315
+ def rpoplpush(conn)
316
+ conn.rpoplpush(key.queued, key.primed)
161
317
  end
162
318
 
163
- def exists_key
164
- @exists_key ||= namespaced_key("EXISTS")
319
+ #
320
+ # Writes lock information to redis.
321
+ # The lock information contains information about worker, queue, limit etc.
322
+ #
323
+ #
324
+ # @return [void]
325
+ #
326
+ def write_lock_info(conn)
327
+ return unless config.lock_info?
328
+
329
+ conn.set(key.info, lock_info)
165
330
  end
166
331
 
167
- def grabbed_key
168
- @grabbed_key ||= namespaced_key("GRABBED")
332
+ #
333
+ # Used to combat redis imprecision with ttl/pttl
334
+ #
335
+ # @param [Integer] val the value to compute drift for
336
+ #
337
+ # @return [Integer] a computed drift value
338
+ #
339
+ def drift(val)
340
+ # Add 2 milliseconds to the drift to account for Redis expires
341
+ # precision, which is 1 millisecond, plus 1 millisecond min drift
342
+ # for small TTLs.
343
+ (val + 2).to_f * CLOCK_DRIFT_FACTOR
169
344
  end
170
345
 
171
- def version_key
172
- @version_key ||= namespaced_key("VERSION")
346
+ def add_drift(val)
347
+ val = val.to_f
348
+ val + drift(val)
173
349
  end
174
350
 
175
- def namespaced_key(variable)
176
- "#{unique_digest}:#{variable}"
351
+ #
352
+ # Checks if the lock has been taken
353
+ #
354
+ # @param [Redis] conn a redis connection
355
+ #
356
+ # @return [true, false]
357
+ #
358
+ def taken?(conn)
359
+ conn.hexists(key.locked, job_id)
177
360
  end
178
361
 
179
- def current_time
180
- seconds, microseconds_with_frac = redis_time
181
- Time.at(seconds, microseconds_with_frac)
362
+ def argv
363
+ [job_id, config.pttl, config.type, config.limit]
364
+ end
365
+
366
+ def lock_info
367
+ @lock_info ||= dump_json(
368
+ WORKER => item[CLASS],
369
+ QUEUE => item[QUEUE],
370
+ LIMIT => item[LOCK_LIMIT],
371
+ TIMEOUT => item[LOCK_TIMEOUT],
372
+ TTL => item[LOCK_TTL],
373
+ TYPE => config.type,
374
+ LOCK_ARGS => item[LOCK_ARGS],
375
+ TIME => now_f,
376
+ )
182
377
  end
183
378
 
184
- def redis_time
185
- redis(&:time)
379
+ def redis_version
380
+ @redis_version ||= SidekiqUniqueJobs.config.redis_version
186
381
  end
187
382
  end
188
- # rubocop:enable Metrics/ClassLength
189
383
  end
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SidekiqUniqueJobs
4
+ #
5
+ # Provides the sidekiq middleware that makes the gem work
6
+ #
7
+ # @author Mikael Henriksson <mikael@mhenrixon.com>
8
+ #
9
+ module Logging
10
+ #
11
+ # Context aware logging for Sidekiq Middlewares
12
+ #
13
+ # @author Mikael Henriksson <mikael@mhenrixon.com>
14
+ #
15
+ module Middleware
16
+ include Logging
17
+
18
+ def self.included(base)
19
+ base.class_eval do
20
+ extend Logging::Middleware
21
+ end
22
+ end
23
+
24
+ #
25
+ # Provides a logging context for Sidekiq Middlewares
26
+ #
27
+ #
28
+ # @return [Hash] when logger responds to `:with_context`
29
+ # @return [String] when logger does not responds to `:with_context`
30
+ #
31
+ def logging_context
32
+ middleware = is_a?(SidekiqUniqueJobs::Middleware::Client) ? :client : :server
33
+ digest = item[LOCK_DIGEST]
34
+ lock_type = item[LOCK]
35
+
36
+ if logger_context_hash?
37
+ { "uniquejobs" => middleware, lock_type => digest }
38
+ else
39
+ "uniquejobs-#{middleware} #{"DIG-#{digest}" if digest}"
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end