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
@@ -0,0 +1,52 @@
1
+ <header class="row">
2
+ <div class="col-sm-5">
3
+ <h3>
4
+ <%= t('Locks') %>
5
+ </h3>
6
+ </div>
7
+ <form action="<%= root_path %>locks" class="form form-inline" method="get">
8
+ <%= csrf_tag %>
9
+ <input name="filter" class="form-control" type="text" value="<%= @filter %>" />
10
+ <button class="btn btn-default" type="submit">
11
+ <%= t('Filter') %>
12
+ </button>
13
+ </form>
14
+ <% if @locks.any? && @total_size > @count.to_i %>
15
+ <div class="col-sm-4">
16
+ <%= erb unique_template(:_paging), locals: { url: "#{root_path}locks" } %>
17
+ </div>
18
+ <% end %>
19
+ </header>
20
+ <% if @locks.any? %>
21
+ <div class="table_container">
22
+ <table class="table table-striped table-bordered table-hover">
23
+ <thead>
24
+ <tr>
25
+ <th><%= t('Delete') %></th>
26
+ <th><%= t('Digest') %></th>
27
+ <th><%= t('Lock') %></th>
28
+ <th><%= t('Locks') %></th>
29
+ <th><%= t('Since') %></th>
30
+ </tr>
31
+ </thead>
32
+ <% @locks.each do |lock| %>
33
+ <tr>
34
+ <td>
35
+ <form action="<%= root_path %>locks/<%= lock.key %>" method="get">
36
+ <%= csrf_tag %>
37
+ <input name="lock" value="<%= h lock.key %>" type="hidden" />
38
+ <input class="btn btn-danger btn-xs" type="submit" name="delete" value="<%= t('Delete') %>" data-confirm="<%= t('AreYouSure') %>" />
39
+ </form>
40
+ </td>
41
+ <td><a href="<%= root_path %>locks/<%= lock.key %>"><%= lock.key %></a></td>
42
+ <td><%= lock.info["lock"] %></th>
43
+ <td><%= lock.locked.count %></td>
44
+ <td><%= safe_relative_time(lock.created_at) %></td>
45
+ </tr>
46
+ <% end %>
47
+ </table>
48
+ <form action="<%= root_path %>locks/delete_all" method="get">
49
+ <input class="btn btn-danger btn-xs" type="submit" name="delete_all" value="<%= t('DeleteAll') %>" data-confirm="<%= t('AreYouSure') %>" />
50
+ </form>
51
+ </div>
52
+ <% end %>
@@ -5,11 +5,12 @@ task :changelog do
5
5
  # rubocop:disable Style/MutableConstant
6
6
  CHANGELOG_CMD ||= %w[
7
7
  github_changelog_generator
8
- -u
8
+ --no-verbose
9
+ --no-http-cache
10
+ --user
9
11
  mhenrixon
10
- -p
12
+ --project
11
13
  sidekiq-unique-jobs
12
- --no-verbose
13
14
  --token
14
15
  ]
15
16
  ADD_CHANGELOG_CMD ||= "git add --all"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sidekiq-unique-jobs
3
3
  version: !ruby/object:Gem::Version
4
- version: 6.0.25
4
+ version: 7.0.0.beta2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mikael Henriksson
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-10-28 00:00:00.000000000 Z
11
+ date: 2019-10-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: concurrent-ruby
@@ -54,22 +54,16 @@ dependencies:
54
54
  name: thor
55
55
  requirement: !ruby/object:Gem::Requirement
56
56
  requirements:
57
- - - ">="
58
- - !ruby/object:Gem::Version
59
- version: '0.20'
60
- - - "<"
57
+ - - "~>"
61
58
  - !ruby/object:Gem::Version
62
- version: '2.0'
59
+ version: '0'
63
60
  type: :runtime
64
61
  prerelease: false
65
62
  version_requirements: !ruby/object:Gem::Requirement
66
63
  requirements:
67
- - - ">="
68
- - !ruby/object:Gem::Version
69
- version: '0.20'
70
- - - "<"
64
+ - - "~>"
71
65
  - !ruby/object:Gem::Version
72
- version: '2.0'
66
+ version: '0'
73
67
  - !ruby/object:Gem::Dependency
74
68
  name: bundler
75
69
  requirement: !ruby/object:Gem::Requirement
@@ -226,7 +220,7 @@ description: |
226
220
  Prevents simultaneous Sidekiq jobs with the same unique arguments to run.
227
221
  Highly configurable to suite your specific needs.
228
222
  email:
229
- - mikael@mhenrixon.com
223
+ - mikael@zoolutions.se
230
224
  executables:
231
225
  - uniquejobs
232
226
  extensions: []
@@ -238,24 +232,58 @@ files:
238
232
  - bin/uniquejobs
239
233
  - lib/sidekiq-unique-jobs.rb
240
234
  - lib/sidekiq_unique_jobs.rb
235
+ - lib/sidekiq_unique_jobs/batch_delete.rb
236
+ - lib/sidekiq_unique_jobs/changelog.rb
241
237
  - lib/sidekiq_unique_jobs/cli.rb
242
- - lib/sidekiq_unique_jobs/client/middleware.rb
238
+ - lib/sidekiq_unique_jobs/config.rb
243
239
  - lib/sidekiq_unique_jobs/connection.rb
244
240
  - lib/sidekiq_unique_jobs/constants.rb
245
241
  - lib/sidekiq_unique_jobs/core_ext.rb
246
242
  - lib/sidekiq_unique_jobs/digests.rb
247
243
  - lib/sidekiq_unique_jobs/exceptions.rb
248
244
  - lib/sidekiq_unique_jobs/job.rb
245
+ - lib/sidekiq_unique_jobs/json.rb
246
+ - lib/sidekiq_unique_jobs/key.rb
247
+ - lib/sidekiq_unique_jobs/lock.rb
249
248
  - lib/sidekiq_unique_jobs/lock/base_lock.rb
249
+ - lib/sidekiq_unique_jobs/lock/client_validator.rb
250
+ - lib/sidekiq_unique_jobs/lock/server_validator.rb
250
251
  - lib/sidekiq_unique_jobs/lock/until_and_while_executing.rb
251
252
  - lib/sidekiq_unique_jobs/lock/until_executed.rb
252
253
  - lib/sidekiq_unique_jobs/lock/until_executing.rb
253
254
  - lib/sidekiq_unique_jobs/lock/until_expired.rb
255
+ - lib/sidekiq_unique_jobs/lock/validator.rb
254
256
  - lib/sidekiq_unique_jobs/lock/while_executing.rb
255
257
  - lib/sidekiq_unique_jobs/lock/while_executing_reject.rb
258
+ - lib/sidekiq_unique_jobs/lock_config.rb
259
+ - lib/sidekiq_unique_jobs/lock_info.rb
256
260
  - lib/sidekiq_unique_jobs/locksmith.rb
257
261
  - lib/sidekiq_unique_jobs/logging.rb
262
+ - lib/sidekiq_unique_jobs/logging/middleware_context.rb
263
+ - lib/sidekiq_unique_jobs/lua/delete.lua
264
+ - lib/sidekiq_unique_jobs/lua/delete_by_digest.lua
265
+ - lib/sidekiq_unique_jobs/lua/delete_job_by_digest.lua
266
+ - lib/sidekiq_unique_jobs/lua/find_digest_in_queues.lua
267
+ - lib/sidekiq_unique_jobs/lua/find_digest_in_sorted_set.lua
268
+ - lib/sidekiq_unique_jobs/lua/lock.lua
269
+ - lib/sidekiq_unique_jobs/lua/locked.lua
270
+ - lib/sidekiq_unique_jobs/lua/queue.lua
271
+ - lib/sidekiq_unique_jobs/lua/reap_orphans.lua
272
+ - lib/sidekiq_unique_jobs/lua/shared/_common.lua
273
+ - lib/sidekiq_unique_jobs/lua/shared/_current_time.lua
274
+ - lib/sidekiq_unique_jobs/lua/shared/_delete_from_queue.lua
275
+ - lib/sidekiq_unique_jobs/lua/shared/_delete_from_sorted_set.lua
276
+ - lib/sidekiq_unique_jobs/lua/shared/_find_digest_in_queues.lua
277
+ - lib/sidekiq_unique_jobs/lua/shared/_find_digest_in_sorted_set.lua
278
+ - lib/sidekiq_unique_jobs/lua/shared/_hgetall.lua
279
+ - lib/sidekiq_unique_jobs/lua/shared/_upgrades.lua
280
+ - lib/sidekiq_unique_jobs/lua/shared/find_digest_in_sorted_set.lua
281
+ - lib/sidekiq_unique_jobs/lua/unlock.lua
282
+ - lib/sidekiq_unique_jobs/lua/update_version.lua
283
+ - lib/sidekiq_unique_jobs/lua/upgrade.lua
258
284
  - lib/sidekiq_unique_jobs/middleware.rb
285
+ - lib/sidekiq_unique_jobs/middleware/client.rb
286
+ - lib/sidekiq_unique_jobs/middleware/server.rb
259
287
  - lib/sidekiq_unique_jobs/normalizer.rb
260
288
  - lib/sidekiq_unique_jobs/on_conflict.rb
261
289
  - lib/sidekiq_unique_jobs/on_conflict/log.rb
@@ -266,33 +294,40 @@ files:
266
294
  - lib/sidekiq_unique_jobs/on_conflict/reschedule.rb
267
295
  - lib/sidekiq_unique_jobs/on_conflict/strategy.rb
268
296
  - lib/sidekiq_unique_jobs/options_with_fallback.rb
269
- - lib/sidekiq_unique_jobs/scripts.rb
270
- - lib/sidekiq_unique_jobs/server/middleware.rb
297
+ - lib/sidekiq_unique_jobs/orphans/manager.rb
298
+ - lib/sidekiq_unique_jobs/orphans/observer.rb
299
+ - lib/sidekiq_unique_jobs/orphans/reaper.rb
300
+ - lib/sidekiq_unique_jobs/profiler.rb
301
+ - lib/sidekiq_unique_jobs/redis.rb
302
+ - lib/sidekiq_unique_jobs/redis/entity.rb
303
+ - lib/sidekiq_unique_jobs/redis/hash.rb
304
+ - lib/sidekiq_unique_jobs/redis/list.rb
305
+ - lib/sidekiq_unique_jobs/redis/set.rb
306
+ - lib/sidekiq_unique_jobs/redis/sorted_set.rb
307
+ - lib/sidekiq_unique_jobs/redis/string.rb
308
+ - lib/sidekiq_unique_jobs/rspec/matchers.rb
309
+ - lib/sidekiq_unique_jobs/rspec/matchers/have_valid_sidekiq_options.rb
310
+ - lib/sidekiq_unique_jobs/script.rb
311
+ - lib/sidekiq_unique_jobs/script/caller.rb
312
+ - lib/sidekiq_unique_jobs/script/template.rb
271
313
  - lib/sidekiq_unique_jobs/sidekiq_unique_ext.rb
272
314
  - lib/sidekiq_unique_jobs/sidekiq_unique_jobs.rb
273
315
  - lib/sidekiq_unique_jobs/sidekiq_worker_methods.rb
274
316
  - lib/sidekiq_unique_jobs/testing.rb
275
- - lib/sidekiq_unique_jobs/timeout.rb
276
- - lib/sidekiq_unique_jobs/timeout/calculator.rb
317
+ - lib/sidekiq_unique_jobs/time_calculator.rb
318
+ - lib/sidekiq_unique_jobs/timing.rb
277
319
  - lib/sidekiq_unique_jobs/unique_args.rb
278
320
  - lib/sidekiq_unique_jobs/unlockable.rb
279
- - lib/sidekiq_unique_jobs/util.rb
321
+ - lib/sidekiq_unique_jobs/update_version.rb
322
+ - lib/sidekiq_unique_jobs/upgrade_locks.rb
280
323
  - lib/sidekiq_unique_jobs/version.rb
281
324
  - lib/sidekiq_unique_jobs/version_check.rb
282
325
  - lib/sidekiq_unique_jobs/web.rb
283
326
  - lib/sidekiq_unique_jobs/web/helpers.rb
284
327
  - lib/sidekiq_unique_jobs/web/views/_paging.erb
285
- - lib/sidekiq_unique_jobs/web/views/unique_digest.erb
286
- - lib/sidekiq_unique_jobs/web/views/unique_digests.erb
328
+ - lib/sidekiq_unique_jobs/web/views/lock.erb
329
+ - lib/sidekiq_unique_jobs/web/views/locks.erb
287
330
  - lib/tasks/changelog.rake
288
- - redis/acquire_lock.lua
289
- - redis/convert_legacy_lock.lua
290
- - redis/delete.lua
291
- - redis/delete_by_digest.lua
292
- - redis/delete_job_by_digest.lua
293
- - redis/lock.lua
294
- - redis/release_stale_locks.lua
295
- - redis/unlock.lua
296
331
  homepage: https://mhenrixon.github.io/sidekiq-unique-jobs
297
332
  licenses:
298
333
  - MIT
@@ -302,7 +337,7 @@ metadata:
302
337
  documentation_uri: https://mhenrixon.github.io/sidekiq-unique-jobs
303
338
  source_code_uri: https://github.com/mhenrixon/sidekiq-unique-jobs
304
339
  changelog_uri: https://github.com/mhenrixon/sidekiq-unique-jobs/CHANGELOG.md
305
- post_install_message:
340
+ post_install_message:
306
341
  rdoc_options: []
307
342
  require_paths:
308
343
  - lib
@@ -313,12 +348,12 @@ required_ruby_version: !ruby/object:Gem::Requirement
313
348
  version: '0'
314
349
  required_rubygems_version: !ruby/object:Gem::Requirement
315
350
  requirements:
316
- - - ">="
351
+ - - ">"
317
352
  - !ruby/object:Gem::Version
318
- version: '0'
353
+ version: 1.3.1
319
354
  requirements: []
320
- rubygems_version: 3.1.2
321
- signing_key:
355
+ rubygems_version: 3.0.6
356
+ signing_key:
322
357
  specification_version: 4
323
358
  summary: Sidekiq middleware that prevents duplicates jobs
324
359
  test_files: []
@@ -1,56 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "sidekiq_unique_jobs/server/middleware"
4
-
5
- module SidekiqUniqueJobs
6
- module Client
7
- # The unique sidekiq middleware for the client push
8
- #
9
- # @author Mikael Henriksson <mikael@zoolutions.se>
10
- class Middleware
11
- include SidekiqUniqueJobs::Logging
12
- include OptionsWithFallback
13
-
14
- # Calls this client middleware
15
- # Used from Sidekiq.process_single
16
- # @param [String] worker_class name of the sidekiq worker class
17
- # @param [Hash] item a sidekiq job hash
18
- # @param [String] queue name of the queue
19
- # @param [Sidekiq::RedisConnection, ConnectionPool] redis_pool the redis connection
20
- # @yield when uniqueness is disable or lock successful
21
- def call(worker_class, item, queue, redis_pool = nil)
22
- @worker_class = worker_class
23
- @item = item
24
- @queue = queue
25
- @redis_pool = redis_pool
26
-
27
- yield if success?
28
- end
29
-
30
- private
31
-
32
- # The sidekiq job hash
33
- # @return [Hash] the Sidekiq job hash
34
- attr_reader :item
35
-
36
- def success?
37
- unique_disabled? || locked?
38
- end
39
-
40
- def locked?
41
- SidekiqUniqueJobs::Job.add_uniqueness(item)
42
- SidekiqUniqueJobs.with_context(logging_context(self.class, item)) do
43
- locked = lock.lock
44
- warn_about_duplicate unless locked
45
- locked
46
- end
47
- end
48
-
49
- def warn_about_duplicate
50
- return unless log_duplicate_payload?
51
-
52
- log_warn "payload is not unique #{item}"
53
- end
54
- end
55
- end
56
- end
@@ -1,46 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module SidekiqUniqueJobs
4
- module Server
5
- # The unique sidekiq middleware for the server processor
6
- #
7
- # @author Mikael Henriksson <mikael@zoolutions.se>
8
- class Middleware
9
- include Logging
10
- include OptionsWithFallback
11
-
12
- #
13
- #
14
- # Runs the server middleware (used from Sidekiq::Processor#process)
15
- #
16
- # @param [Sidekiq::Worker] worker_class
17
- # @param [Hash] item a sidekiq job hash
18
- # @param [String] queue name of the queue
19
- #
20
- # @see https://github.com/mperham/sidekiq/wiki/Job-Format
21
- # @see https://github.com/mperham/sidekiq/wiki/Middleware
22
- #
23
- # @yield when uniqueness is disabled
24
- # @yield when the lock is acquired
25
- def call(worker_class, item, queue)
26
- @worker_class = worker_class
27
- @item = item
28
- @queue = queue
29
- return yield if unique_disabled?
30
-
31
- SidekiqUniqueJobs::Job.add_uniqueness(item)
32
- SidekiqUniqueJobs.with_context(logging_context(self.class, item)) do
33
- lock.execute do
34
- yield
35
- end
36
- end
37
- end
38
-
39
- private
40
-
41
- # The sidekiq job hash
42
- # @return [Hash] the Sidekiq job hash
43
- attr_reader :item
44
- end
45
- end
46
- end
@@ -1,8 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module SidekiqUniqueJobs
4
- module Timeout
5
- end
6
- end
7
-
8
- require "sidekiq_unique_jobs/timeout/calculator"
@@ -1,63 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module SidekiqUniqueJobs
4
- module Timeout
5
- # Calculates timeout and expiration
6
- #
7
- # @author Mikael Henriksson <mikael@zoolutions.se>
8
- class Calculator
9
- include SidekiqUniqueJobs::SidekiqWorkerMethods
10
-
11
- # @attr [Hash] item the Sidekiq job hash
12
- attr_reader :item
13
-
14
- # @param [Hash] item the Sidekiq job hash
15
- # @option item [Integer, nil] :lock_expiration the configured lock expiration
16
- # @option item [Integer, nil] :lock_timeout the configured lock timeout
17
- # @option item [String] :class the class of the sidekiq worker
18
- # @option item [Float] :at the unix time the job is scheduled at
19
- def initialize(item)
20
- @item = item
21
- @worker_class = item[CLASS_KEY]
22
- end
23
-
24
- # The time until a job is scheduled
25
- # @return [Integer] the number of seconds until job is scheduled
26
- def time_until_scheduled
27
- return 0 unless scheduled_at
28
-
29
- scheduled_at.to_i - Time.now.utc.to_i
30
- end
31
-
32
- # The time a job is scheduled
33
- # @return [Float] the exact unix time the job is scheduled at
34
- def scheduled_at
35
- @scheduled_at ||= item[AT_KEY]
36
- end
37
-
38
- # The configured lock_expiration
39
- def lock_expiration
40
- @lock_expiration ||= begin
41
- expiration = item[LOCK_EXPIRATION_KEY]
42
- expiration ||= worker_options[LOCK_EXPIRATION_KEY]
43
- expiration && expiration.to_i + time_until_scheduled
44
- end
45
- end
46
-
47
- # The configured lock_timeout
48
- def lock_timeout
49
- @lock_timeout = begin
50
- timeout = default_worker_options[LOCK_TIMEOUT_KEY]
51
- timeout = default_lock_timeout if default_lock_timeout
52
- timeout = worker_options[LOCK_TIMEOUT_KEY] if worker_options.key?(LOCK_TIMEOUT_KEY)
53
- timeout
54
- end
55
- end
56
-
57
- # The default lock_timeout of this gem
58
- def default_lock_timeout
59
- SidekiqUniqueJobs.config.default_lock_timeout
60
- end
61
- end
62
- end
63
- end
@@ -1,103 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module SidekiqUniqueJobs
4
- # Utility module to help manage unique keys in redis.
5
- # Useful for deleting keys that for whatever reason wasn't deleted
6
- #
7
- # @author Mikael Henriksson <mikael@zoolutions.se>
8
- module Util
9
- DEFAULT_COUNT = 1_000
10
- SCAN_PATTERN = "*"
11
-
12
- include SidekiqUniqueJobs::Logging
13
- include SidekiqUniqueJobs::Connection
14
- extend self
15
-
16
- # Find unique keys in redis
17
- #
18
- # @param [String] pattern a pattern to scan for in redis
19
- # @param [Integer] count the maximum number of keys to delete
20
- # @return [Array<String>] an array with active unique keys
21
- def keys(pattern = SCAN_PATTERN, count = DEFAULT_COUNT)
22
- return redis(&:keys) if pattern.nil?
23
-
24
- redis { |conn| conn.scan_each(match: prefix(pattern), count: count).to_a }
25
- end
26
-
27
- # Find unique keys with ttl
28
- # @param [String] pattern a pattern to scan for in redis
29
- # @param [Integer] count the maximum number of keys to delete
30
- # @return [Hash<String, Integer>] a hash with active unique keys and corresponding ttl
31
- def keys_with_ttl(pattern = SCAN_PATTERN, count = DEFAULT_COUNT)
32
- hash = {}
33
- redis do |conn|
34
- conn.scan_each(match: prefix(pattern), count: count).each do |key|
35
- hash[key] = conn.ttl(key)
36
- end
37
- end
38
- hash
39
- end
40
-
41
- # Deletes unique keys from redis
42
- #
43
- # @param [String] pattern a pattern to scan for in redis
44
- # @param [Integer] count the maximum number of keys to delete
45
- # @return [Integer] the number of keys deleted
46
- def del(pattern = SCAN_PATTERN, count = 0)
47
- raise ArgumentError, "Please provide a number of keys to delete greater than zero" if count.zero?
48
-
49
- pattern = suffix(pattern)
50
-
51
- log_debug { "Deleting keys by: #{pattern}" }
52
- keys, time = timed { keys(pattern, count) }
53
- key_size = keys.size
54
- log_debug { "#{key_size} keys found in #{time} sec." }
55
- _, time = timed { batch_delete(keys) }
56
- log_debug { "Deleted #{key_size} keys in #{time} sec." }
57
-
58
- key_size
59
- end
60
-
61
- private
62
-
63
- def batch_delete(keys)
64
- redis do |conn|
65
- keys.each_slice(500) do |chunk|
66
- conn.pipelined do
67
- chunk.each do |key|
68
- conn.del key
69
- end
70
- end
71
- end
72
- end
73
- end
74
-
75
- def timed
76
- start = current_time
77
- result = yield
78
- elapsed = (current_time - start).round(2)
79
- [result, elapsed]
80
- end
81
-
82
- def current_time
83
- Time.now
84
- end
85
-
86
- def prefix(key)
87
- return key if unique_prefix.nil?
88
- return key if key.start_with?("#{unique_prefix}:")
89
-
90
- "#{unique_prefix}:#{key}"
91
- end
92
-
93
- def suffix(key)
94
- return "#{key}*" unless key.end_with?(":*")
95
-
96
- key
97
- end
98
-
99
- def unique_prefix
100
- SidekiqUniqueJobs.config.unique_prefix
101
- end
102
- end
103
- end