sidekiq-unique-jobs 6.0.7 → 7.1.33

Sign up to get free protection for your applications and to get access to all the features.
Files changed (194) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +1880 -156
  3. data/README.md +818 -238
  4. data/bin/uniquejobs +2 -2
  5. data/lib/sidekiq-unique-jobs.rb +1 -1
  6. data/lib/sidekiq_unique_jobs/batch_delete.rb +124 -0
  7. data/lib/sidekiq_unique_jobs/changelog.rb +78 -0
  8. data/lib/sidekiq_unique_jobs/cli.rb +68 -25
  9. data/lib/sidekiq_unique_jobs/config.rb +319 -0
  10. data/lib/sidekiq_unique_jobs/connection.rb +6 -5
  11. data/lib/sidekiq_unique_jobs/constants.rb +50 -20
  12. data/lib/sidekiq_unique_jobs/core_ext.rb +80 -0
  13. data/lib/sidekiq_unique_jobs/deprecation.rb +65 -0
  14. data/lib/sidekiq_unique_jobs/digests.rb +70 -87
  15. data/lib/sidekiq_unique_jobs/exceptions.rb +88 -12
  16. data/lib/sidekiq_unique_jobs/expiring_digests.rb +14 -0
  17. data/lib/sidekiq_unique_jobs/job.rb +47 -13
  18. data/lib/sidekiq_unique_jobs/json.rb +47 -0
  19. data/lib/sidekiq_unique_jobs/key.rb +98 -0
  20. data/lib/sidekiq_unique_jobs/lock/base_lock.rb +111 -82
  21. data/lib/sidekiq_unique_jobs/lock/client_validator.rb +28 -0
  22. data/lib/sidekiq_unique_jobs/lock/server_validator.rb +27 -0
  23. data/lib/sidekiq_unique_jobs/lock/until_and_while_executing.rb +45 -5
  24. data/lib/sidekiq_unique_jobs/lock/until_executed.rb +30 -4
  25. data/lib/sidekiq_unique_jobs/lock/until_executing.rb +26 -2
  26. data/lib/sidekiq_unique_jobs/lock/until_expired.rb +27 -15
  27. data/lib/sidekiq_unique_jobs/lock/validator.rb +96 -0
  28. data/lib/sidekiq_unique_jobs/lock/while_executing.rb +26 -7
  29. data/lib/sidekiq_unique_jobs/lock/while_executing_reject.rb +3 -11
  30. data/lib/sidekiq_unique_jobs/lock.rb +342 -0
  31. data/lib/sidekiq_unique_jobs/lock_args.rb +127 -0
  32. data/lib/sidekiq_unique_jobs/lock_config.rb +126 -0
  33. data/lib/sidekiq_unique_jobs/lock_digest.rb +79 -0
  34. data/lib/sidekiq_unique_jobs/lock_info.rb +68 -0
  35. data/lib/sidekiq_unique_jobs/lock_timeout.rb +62 -0
  36. data/lib/sidekiq_unique_jobs/lock_ttl.rb +77 -0
  37. data/lib/sidekiq_unique_jobs/lock_type.rb +37 -0
  38. data/lib/sidekiq_unique_jobs/locksmith.rb +324 -95
  39. data/lib/sidekiq_unique_jobs/logging/middleware_context.rb +44 -0
  40. data/lib/sidekiq_unique_jobs/logging.rb +208 -30
  41. data/lib/sidekiq_unique_jobs/lua/delete.lua +51 -0
  42. data/lib/sidekiq_unique_jobs/lua/delete_by_digest.lua +42 -0
  43. data/lib/sidekiq_unique_jobs/lua/delete_job_by_digest.lua +38 -0
  44. data/lib/sidekiq_unique_jobs/lua/find_digest_in_queues.lua +26 -0
  45. data/lib/sidekiq_unique_jobs/lua/lock.lua +99 -0
  46. data/lib/sidekiq_unique_jobs/lua/lock_until_expired.lua +92 -0
  47. data/lib/sidekiq_unique_jobs/lua/locked.lua +35 -0
  48. data/lib/sidekiq_unique_jobs/lua/queue.lua +87 -0
  49. data/lib/sidekiq_unique_jobs/lua/reap_orphans.lua +122 -0
  50. data/lib/sidekiq_unique_jobs/lua/shared/_common.lua +40 -0
  51. data/lib/sidekiq_unique_jobs/lua/shared/_current_time.lua +8 -0
  52. data/lib/sidekiq_unique_jobs/lua/shared/_delete_from_queue.lua +22 -0
  53. data/lib/sidekiq_unique_jobs/lua/shared/_delete_from_sorted_set.lua +18 -0
  54. data/lib/sidekiq_unique_jobs/lua/shared/_find_digest_in_process_set.lua +53 -0
  55. data/lib/sidekiq_unique_jobs/lua/shared/_find_digest_in_queues.lua +43 -0
  56. data/lib/sidekiq_unique_jobs/lua/shared/_find_digest_in_sorted_set.lua +24 -0
  57. data/lib/sidekiq_unique_jobs/lua/shared/_hgetall.lua +13 -0
  58. data/lib/sidekiq_unique_jobs/lua/shared/_upgrades.lua +3 -0
  59. data/lib/sidekiq_unique_jobs/lua/unlock.lua +107 -0
  60. data/lib/sidekiq_unique_jobs/lua/update_version.lua +40 -0
  61. data/lib/sidekiq_unique_jobs/lua/upgrade.lua +68 -0
  62. data/lib/sidekiq_unique_jobs/middleware/client.rb +42 -0
  63. data/lib/sidekiq_unique_jobs/middleware/server.rb +31 -0
  64. data/lib/sidekiq_unique_jobs/middleware.rb +33 -30
  65. data/lib/sidekiq_unique_jobs/normalizer.rb +4 -4
  66. data/lib/sidekiq_unique_jobs/on_conflict/log.rb +9 -5
  67. data/lib/sidekiq_unique_jobs/on_conflict/null_strategy.rb +1 -1
  68. data/lib/sidekiq_unique_jobs/on_conflict/raise.rb +2 -2
  69. data/lib/sidekiq_unique_jobs/on_conflict/reject.rb +63 -17
  70. data/lib/sidekiq_unique_jobs/on_conflict/replace.rb +54 -14
  71. data/lib/sidekiq_unique_jobs/on_conflict/reschedule.rb +16 -5
  72. data/lib/sidekiq_unique_jobs/on_conflict/strategy.rb +26 -7
  73. data/lib/sidekiq_unique_jobs/on_conflict.rb +34 -16
  74. data/lib/sidekiq_unique_jobs/options_with_fallback.rb +39 -36
  75. data/lib/sidekiq_unique_jobs/orphans/lua_reaper.rb +29 -0
  76. data/lib/sidekiq_unique_jobs/orphans/manager.rb +241 -0
  77. data/lib/sidekiq_unique_jobs/orphans/null_reaper.rb +24 -0
  78. data/lib/sidekiq_unique_jobs/orphans/observer.rb +42 -0
  79. data/lib/sidekiq_unique_jobs/orphans/reaper.rb +114 -0
  80. data/lib/sidekiq_unique_jobs/orphans/reaper_resurrector.rb +170 -0
  81. data/lib/sidekiq_unique_jobs/orphans/ruby_reaper.rb +298 -0
  82. data/lib/sidekiq_unique_jobs/redis/entity.rb +112 -0
  83. data/lib/sidekiq_unique_jobs/redis/hash.rb +56 -0
  84. data/lib/sidekiq_unique_jobs/redis/list.rb +32 -0
  85. data/lib/sidekiq_unique_jobs/redis/set.rb +32 -0
  86. data/lib/sidekiq_unique_jobs/redis/sorted_set.rb +86 -0
  87. data/lib/sidekiq_unique_jobs/redis/string.rb +51 -0
  88. data/lib/sidekiq_unique_jobs/redis.rb +11 -0
  89. data/lib/sidekiq_unique_jobs/reflectable.rb +26 -0
  90. data/lib/sidekiq_unique_jobs/reflections.rb +79 -0
  91. data/lib/sidekiq_unique_jobs/rspec/matchers/have_valid_sidekiq_options.rb +51 -0
  92. data/lib/sidekiq_unique_jobs/rspec/matchers.rb +26 -0
  93. data/lib/sidekiq_unique_jobs/script/caller.rb +127 -0
  94. data/lib/sidekiq_unique_jobs/script.rb +15 -0
  95. data/lib/sidekiq_unique_jobs/server.rb +61 -0
  96. data/lib/sidekiq_unique_jobs/sidekiq_unique_ext.rb +115 -66
  97. data/lib/sidekiq_unique_jobs/sidekiq_unique_jobs.rb +304 -0
  98. data/lib/sidekiq_unique_jobs/sidekiq_worker_methods.rb +51 -29
  99. data/lib/sidekiq_unique_jobs/testing.rb +104 -31
  100. data/lib/sidekiq_unique_jobs/timer_task.rb +299 -0
  101. data/lib/sidekiq_unique_jobs/timing.rb +58 -0
  102. data/lib/sidekiq_unique_jobs/unlockable.rb +20 -4
  103. data/lib/sidekiq_unique_jobs/update_version.rb +25 -0
  104. data/lib/sidekiq_unique_jobs/upgrade_locks.rb +155 -0
  105. data/lib/sidekiq_unique_jobs/version.rb +3 -1
  106. data/lib/sidekiq_unique_jobs/version_check.rb +114 -0
  107. data/lib/sidekiq_unique_jobs/web/helpers.rb +140 -15
  108. data/lib/sidekiq_unique_jobs/web/views/_paging.erb +4 -4
  109. data/lib/sidekiq_unique_jobs/web/views/changelogs.erb +54 -0
  110. data/lib/sidekiq_unique_jobs/web/views/lock.erb +110 -0
  111. data/lib/sidekiq_unique_jobs/web/views/locks.erb +54 -0
  112. data/lib/sidekiq_unique_jobs/web.rb +86 -29
  113. data/lib/sidekiq_unique_jobs.rb +78 -105
  114. data/lib/tasks/changelog.rake +23 -0
  115. metadata +154 -218
  116. data/.codeclimate.yml +0 -35
  117. data/.csslintrc +0 -2
  118. data/.dockerignore +0 -4
  119. data/.editorconfig +0 -14
  120. data/.eslintignore +0 -1
  121. data/.eslintrc +0 -213
  122. data/.fasterer.yml +0 -23
  123. data/.github/ISSUE_TEMPLATE/bug_report.md +0 -31
  124. data/.github/ISSUE_TEMPLATE/feature_request.md +0 -17
  125. data/.gitignore +0 -28
  126. data/.reek.yml +0 -87
  127. data/.rspec +0 -2
  128. data/.rubocop.yml +0 -127
  129. data/.simplecov +0 -20
  130. data/.travis.yml +0 -47
  131. data/.yardopts +0 -7
  132. data/Appraisals +0 -29
  133. data/CODE_OF_CONDUCT.md +0 -74
  134. data/Gemfile +0 -24
  135. data/Guardfile +0 -55
  136. data/Rakefile +0 -12
  137. data/_config.yml +0 -1
  138. data/assets/unique_digests_1.png +0 -0
  139. data/assets/unique_digests_2.png +0 -0
  140. data/bin/bench +0 -20
  141. data/examples/another_unique_job.rb +0 -15
  142. data/examples/custom_queue_job.rb +0 -12
  143. data/examples/custom_queue_job_with_filter_method.rb +0 -13
  144. data/examples/custom_queue_job_with_filter_proc.rb +0 -16
  145. data/examples/expiring_job.rb +0 -12
  146. data/examples/inline_worker.rb +0 -12
  147. data/examples/just_a_worker.rb +0 -13
  148. data/examples/long_running_job.rb +0 -14
  149. data/examples/main_job.rb +0 -14
  150. data/examples/my_job.rb +0 -12
  151. data/examples/my_unique_job.rb +0 -15
  152. data/examples/my_unique_job_with_filter_method.rb +0 -21
  153. data/examples/my_unique_job_with_filter_proc.rb +0 -19
  154. data/examples/notify_worker.rb +0 -14
  155. data/examples/plain_class.rb +0 -13
  156. data/examples/simple_worker.rb +0 -15
  157. data/examples/spawn_simple_worker.rb +0 -12
  158. data/examples/test_class.rb +0 -9
  159. data/examples/unique_across_workers_job.rb +0 -20
  160. data/examples/unique_job_on_conflict_raise.rb +0 -14
  161. data/examples/unique_job_on_conflict_reject.rb +0 -14
  162. data/examples/unique_job_on_conflict_reschedule.rb +0 -14
  163. data/examples/unique_job_with_conditional_parameter.rb +0 -18
  164. data/examples/unique_job_with_filter_method.rb +0 -21
  165. data/examples/unique_job_with_nil_unique_args.rb +0 -20
  166. data/examples/unique_job_with_no_unique_args_method.rb +0 -16
  167. data/examples/unique_job_withthout_unique_args_parameter.rb +0 -18
  168. data/examples/unique_on_all_queues_job.rb +0 -16
  169. data/examples/until_and_while_executing_job.rb +0 -17
  170. data/examples/until_executed_2_job.rb +0 -24
  171. data/examples/until_executed_job.rb +0 -25
  172. data/examples/until_executing_job.rb +0 -11
  173. data/examples/until_expired_job.rb +0 -12
  174. data/examples/until_global_expired_job.rb +0 -12
  175. data/examples/while_executing_job.rb +0 -15
  176. data/examples/while_executing_reject_job.rb +0 -14
  177. data/examples/without_argument_job.rb +0 -13
  178. data/lib/sidekiq_unique_jobs/client/middleware.rb +0 -56
  179. data/lib/sidekiq_unique_jobs/scripts.rb +0 -89
  180. data/lib/sidekiq_unique_jobs/server/middleware.rb +0 -40
  181. data/lib/sidekiq_unique_jobs/timeout/calculator.rb +0 -63
  182. data/lib/sidekiq_unique_jobs/timeout.rb +0 -8
  183. data/lib/sidekiq_unique_jobs/unique_args.rb +0 -149
  184. data/lib/sidekiq_unique_jobs/util.rb +0 -103
  185. data/lib/sidekiq_unique_jobs/web/views/unique_digest.erb +0 -28
  186. data/lib/sidekiq_unique_jobs/web/views/unique_digests.erb +0 -42
  187. data/redis/acquire_lock.lua +0 -21
  188. data/redis/delete.lua +0 -14
  189. data/redis/delete_by_digest.lua +0 -24
  190. data/redis/delete_job_by_digest.lua +0 -60
  191. data/redis/lock.lua +0 -58
  192. data/redis/release_stale_locks.lua +0 -90
  193. data/redis/unlock.lua +0 -32
  194. data/sidekiq-unique-jobs.gemspec +0 -42
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SidekiqUniqueJobs
4
+ module Middleware
5
+ # The unique sidekiq middleware for the client push
6
+ #
7
+ # @author Mikael Henriksson <mikael@mhenrixon.com>
8
+ class Client
9
+ include Sidekiq::ClientMiddleware if defined?(Sidekiq::ClientMiddleware)
10
+
11
+ # prepend "SidekiqUniqueJobs::Middleware"
12
+ # @!parse prepends SidekiqUniqueJobs::Middleware
13
+ prepend SidekiqUniqueJobs::Middleware
14
+ # includes "SidekiqUniqueJobs::Reflectable"
15
+ # @!parse include SidekiqUniqueJobs::Reflectable
16
+ include SidekiqUniqueJobs::Reflectable
17
+
18
+ # Calls this client middleware
19
+ # Used from Sidekiq.process_single
20
+ #
21
+ # @see SidekiqUniqueJobs::Middleware#call
22
+ #
23
+ # @see https://github.com/mperham/sidekiq/wiki/Job-Format
24
+ # @see https://github.com/mperham/sidekiq/wiki/Middleware
25
+ #
26
+ # @yield when uniqueness is disable
27
+ # @yield when the lock is successful
28
+ def call(*, &block)
29
+ lock(&block)
30
+ end
31
+
32
+ private
33
+
34
+ def lock
35
+ lock_instance.lock do
36
+ reflect(:locked, item)
37
+ return yield
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SidekiqUniqueJobs
4
+ module Middleware
5
+ # The unique sidekiq middleware for the server processor
6
+ #
7
+ # @author Mikael Henriksson <mikael@mhenrixon.com>
8
+ class Server
9
+ include Sidekiq::ServerMiddleware if defined?(Sidekiq::ServerMiddleware)
10
+
11
+ # prepend "SidekiqUniqueJobs::Middleware"
12
+ # @!parse prepends SidekiqUniqueJobs::Middleware
13
+ prepend SidekiqUniqueJobs::Middleware
14
+
15
+ #
16
+ #
17
+ # Runs the server middleware (used from Sidekiq::Processor#process)
18
+ #
19
+ # @see SidekiqUniqueJobs::Middleware#call
20
+ #
21
+ # @see https://github.com/mperham/sidekiq/wiki/Job-Format
22
+ # @see https://github.com/mperham/sidekiq/wiki/Middleware
23
+ #
24
+ # @yield when uniqueness is disabled
25
+ # @yield when owning the lock
26
+ def call(*, &block)
27
+ lock_instance.execute(&block)
28
+ end
29
+ end
30
+ end
31
+ end
@@ -1,42 +1,45 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'sidekiq'
4
-
5
3
  module SidekiqUniqueJobs
4
+ #
5
+ # Provides the sidekiq middleware that makes the gem work
6
+ #
7
+ # @author Mikael Henriksson <mikael@mhenrixon.com>
8
+ #
6
9
  module Middleware
7
- def self.extended(base)
8
- base.class_eval do
9
- configure_middleware
10
- end
11
- end
10
+ include SidekiqUniqueJobs::Logging::Middleware
11
+ include SidekiqUniqueJobs::OptionsWithFallback
12
+ include SidekiqUniqueJobs::JSON
12
13
 
13
- def configure_middleware
14
- configure_server_middleware
15
- configure_client_middleware
16
- end
14
+ # The sidekiq job hash
15
+ # @return [Hash] the Sidekiq job hash
16
+ attr_reader :item
17
17
 
18
- def configure_server_middleware
19
- Sidekiq.configure_server do |config|
20
- config.client_middleware do |chain|
21
- require 'sidekiq_unique_jobs/client/middleware'
22
- chain.add SidekiqUniqueJobs::Client::Middleware
23
- end
18
+ #
19
+ # This method runs before (prepended) the actual middleware implementation.
20
+ # This is done to reduce duplication
21
+ #
22
+ # @param [Sidekiq::Worker] worker_class
23
+ # @param [Hash] item a sidekiq job hash
24
+ # @param [String] queue name of the queue
25
+ # @param [ConnectionPool] redis_pool only used for compatility reasons
26
+ #
27
+ # @return [yield<super>] <description>
28
+ #
29
+ # @yieldparam [<type>] if <description>
30
+ # @yieldreturn [<type>] <describe what yield should return>
31
+ def call(worker_class, item, queue, redis_pool = nil)
32
+ @item = item
33
+ @queue = queue
34
+ @redis_pool = redis_pool
35
+ self.job_class = worker_class
36
+ return yield if unique_disabled?
24
37
 
25
- config.server_middleware do |chain|
26
- require 'sidekiq_unique_jobs/server/middleware'
27
- chain.add SidekiqUniqueJobs::Server::Middleware
28
- end
29
- end
30
- end
38
+ SidekiqUniqueJobs::Job.prepare(item) unless item[LOCK_DIGEST]
31
39
 
32
- def configure_client_middleware
33
- Sidekiq.configure_client do |config|
34
- config.client_middleware do |chain|
35
- require 'sidekiq_unique_jobs/client/middleware'
36
- chain.add SidekiqUniqueJobs::Client::Middleware
37
- end
40
+ with_logging_context do
41
+ super
38
42
  end
39
43
  end
40
44
  end
41
45
  end
42
- SidekiqUniqueJobs.send(:extend, SidekiqUniqueJobs::Middleware)
@@ -1,17 +1,17 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'json'
4
-
5
3
  module SidekiqUniqueJobs
6
4
  # Normalizes hashes by dumping them to json and loading them from json
7
5
  #
8
- # @author Mikael Henriksson <mikael@zoolutions.se>
6
+ # @author Mikael Henriksson <mikael@mhenrixon.com>
9
7
  module Normalizer
8
+ extend SidekiqUniqueJobs::JSON
9
+
10
10
  # Changes hash to a json compatible hash
11
11
  # @param [Hash] args
12
12
  # @return [Hash] a json compatible hash
13
13
  def self.jsonify(args)
14
- Sidekiq.load_json(Sidekiq.dump_json(args))
14
+ load_json(dump_json(args))
15
15
  end
16
16
  end
17
17
  end
@@ -4,16 +4,20 @@ module SidekiqUniqueJobs
4
4
  module OnConflict
5
5
  # Strategy to log information about conflict
6
6
  #
7
- # @author Mikael Henriksson <mikael@zoolutions.se>
7
+ # @author Mikael Henriksson <mikael@mhenrixon.com>
8
8
  class Log < OnConflict::Strategy
9
9
  include SidekiqUniqueJobs::Logging
10
10
 
11
+ #
11
12
  # Logs an informational message about that the job was not unique
13
+ #
14
+ #
15
+ # @return [void]
16
+ #
12
17
  def call
13
- log_info(
14
- "skipping job with id (#{item[JID_KEY]}) " \
15
- "because unique_digest: (#{item[UNIQUE_DIGEST_KEY]}) already exists",
16
- )
18
+ log_info(<<~MESSAGE.chomp)
19
+ Skipping job with id (#{item[JID]}) because lock_digest: (#{item[LOCK_DIGEST]}) already exists
20
+ MESSAGE
17
21
  end
18
22
  end
19
23
  end
@@ -4,7 +4,7 @@ module SidekiqUniqueJobs
4
4
  module OnConflict
5
5
  # Default conflict strategy class that does nothing
6
6
  #
7
- # @author Mikael Henriksson <mikael@zoolutions.se>
7
+ # @author Mikael Henriksson <mikael@mhenrixon.com>
8
8
  class NullStrategy < OnConflict::Strategy
9
9
  # Do nothing on conflict
10
10
  # @return [nil]
@@ -4,13 +4,13 @@ module SidekiqUniqueJobs
4
4
  module OnConflict
5
5
  # Strategy to raise an error on conflict
6
6
  #
7
- # @author Mikael Henriksson <mikael@zoolutions.se>
7
+ # @author Mikael Henriksson <mikael@mhenrixon.com>
8
8
  class Raise < OnConflict::Strategy
9
9
  # Raise an error on conflict.
10
10
  # This will cause Sidekiq to retry the job
11
11
  # @raise [SidekiqUniqueJobs::Conflict]
12
12
  def call
13
- fail SidekiqUniqueJobs::Conflict, item
13
+ raise SidekiqUniqueJobs::Conflict, item
14
14
  end
15
15
  end
16
16
  end
@@ -4,16 +4,13 @@ module SidekiqUniqueJobs
4
4
  module OnConflict
5
5
  # Strategy to send jobs to dead queue
6
6
  #
7
- # @author Mikael Henriksson <mikael@zoolutions.se>
7
+ # @author Mikael Henriksson <mikael@mhenrixon.com>
8
8
  class Reject < OnConflict::Strategy
9
+ include SidekiqUniqueJobs::Timing
10
+
9
11
  # Send jobs to dead queue
10
12
  def call
11
- log_debug { "Rejecting job with jid: #{item[JID_KEY]}" }
12
- send_to_deadset
13
- end
14
-
15
- def send_to_deadset
16
- log_info { "Adding dead #{item[CLASS_KEY]} job #{item[JID_KEY]}" }
13
+ log_info { "Adding dead #{item[CLASS]} job #{item[JID]}" }
17
14
 
18
15
  if deadset_kill?
19
16
  deadset_kill
@@ -22,10 +19,25 @@ module SidekiqUniqueJobs
22
19
  end
23
20
  end
24
21
 
22
+ #
23
+ # Sidekiq version compatibility check
24
+ # @api private
25
+ #
26
+ #
27
+ # @return [true, false] depending on if Sidekiq::Deadset responds to kill
28
+ #
25
29
  def deadset_kill?
26
30
  deadset.respond_to?(:kill)
27
31
  end
28
32
 
33
+ #
34
+ # Use Sidekiqs built in Sidekiq::DeadSet#kill
35
+ # to get rid of the job
36
+ # @api private
37
+ #
38
+ #
39
+ # @return [void]
40
+ #
29
41
  def deadset_kill
30
42
  if kill_with_options?
31
43
  kill_job_with_options
@@ -34,38 +46,72 @@ module SidekiqUniqueJobs
34
46
  end
35
47
  end
36
48
 
49
+ #
50
+ # Sidekiq version compatibility check
51
+ # @api private
52
+ #
53
+ #
54
+ # @return [true] when Sidekiq::Deadset#kill takes more than 1 argument
55
+ # @return [false] when Sidekiq::Deadset#kill does not take multiple arguments
56
+ #
37
57
  def kill_with_options?
38
58
  Sidekiq::DeadSet.instance_method(:kill).arity > 1
39
59
  end
40
60
 
61
+ #
62
+ # Executes the kill instructions without arguments
63
+ # @api private
64
+ #
65
+ # @return [void]
66
+ #
41
67
  def kill_job_without_options
42
68
  deadset.kill(payload)
43
69
  end
44
70
 
71
+ #
72
+ # Executes the kill instructions with arguments
73
+ # @api private
74
+ #
75
+ # @return [void]
76
+ #
45
77
  def kill_job_with_options
46
78
  deadset.kill(payload, notify_failure: false)
47
79
  end
48
80
 
81
+ #
82
+ # An instance of Sidekiq::Deadset
83
+ # @api private
84
+ #
85
+ # @return [Sidekiq::Deadset]>
86
+ #
49
87
  def deadset
50
88
  @deadset ||= Sidekiq::DeadSet.new
51
89
  end
52
90
 
91
+ #
92
+ # Used for compatibility with older Sidekiq versions
93
+ #
94
+ #
95
+ # @return [void]
96
+ #
53
97
  def push_to_deadset
54
- Sidekiq.redis do |conn|
55
- conn.multi do
56
- conn.zadd('dead', current_time, payload)
57
- conn.zremrangebyscore('dead', '-inf', current_time - Sidekiq::DeadSet.timeout)
58
- conn.zremrangebyrank('dead', 0, -Sidekiq::DeadSet.max_jobs)
98
+ redis do |conn|
99
+ conn.multi do |pipeline|
100
+ pipeline.zadd("dead", now_f, payload)
101
+ pipeline.zremrangebyscore("dead", "-inf", now_f - Sidekiq::DeadSet.timeout)
102
+ pipeline.zremrangebyrank("dead", 0, -Sidekiq::DeadSet.max_jobs)
59
103
  end
60
104
  end
61
105
  end
62
106
 
63
- def current_time
64
- @current_time ||= Time.now.to_f
65
- end
66
-
107
+ #
108
+ # The Sidekiq job hash as JSON
109
+ #
110
+ #
111
+ # @return [String] a JSON formatted string
112
+ #
67
113
  def payload
68
- @payload ||= Sidekiq.dump_json(item)
114
+ @payload ||= dump_json(item)
69
115
  end
70
116
  end
71
117
  end
@@ -4,38 +4,78 @@ module SidekiqUniqueJobs
4
4
  module OnConflict
5
5
  # Strategy to replace the job on conflict
6
6
  #
7
- # @author Mikael Henriksson <mikael@zoolutions.se>
7
+ # @author Mikael Henriksson <mikael@mhenrixon.com>
8
8
  class Replace < OnConflict::Strategy
9
- attr_reader :queue, :unique_digest
9
+ #
10
+ # @!attribute [r] queue
11
+ # @return [String] rthe sidekiq queue this job belongs to
12
+ attr_reader :queue
13
+ #
14
+ # @!attribute [r] lock_digest
15
+ # @return [String] the unique digest to use for locking
16
+ attr_reader :lock_digest
10
17
 
18
+ #
19
+ # Initialize a new Replace strategy
20
+ #
11
21
  # @param [Hash] item sidekiq job hash
12
- def initialize(item)
13
- super
14
- @queue = item[QUEUE_KEY]
15
- @unique_digest = item[UNIQUE_DIGEST_KEY]
22
+ #
23
+ def initialize(item, redis_pool = nil)
24
+ super(item, redis_pool)
25
+ @queue = item[QUEUE]
26
+ @lock_digest = item[LOCK_DIGEST]
16
27
  end
17
28
 
29
+ #
18
30
  # Replace the old job in the queue
31
+ #
32
+ #
33
+ # @return [void] <description>
34
+ #
19
35
  # @yield to retry the lock after deleting the old one
36
+ #
20
37
  def call(&block)
21
- return unless delete_job_by_digest
38
+ return unless (deleted_job = delete_job_by_digest)
39
+
40
+ log_info("Deleted job: #{deleted_job}")
41
+ if (del_count = delete_lock)
42
+ log_info("Deleted `#{del_count}` keys for #{lock_digest}")
43
+ end
22
44
 
23
- delete_lock
24
45
  block&.call
25
46
  end
26
47
 
48
+ #
27
49
  # Delete the job from either schedule, retry or the queue
50
+ #
51
+ #
52
+ # @return [String] the deleted job hash
53
+ # @return [nil] when deleting nothing
54
+ #
28
55
  def delete_job_by_digest
29
- Scripts.call(
30
- :delete_job_by_digest,
31
- nil,
32
- keys: ["#{QUEUE_KEY}:#{queue}", SCHEDULE_SET, RETRY_SET], argv: [unique_digest],
33
- )
56
+ call_script(:delete_job_by_digest,
57
+ keys: ["#{QUEUE}:#{queue}", SCHEDULE, RETRY],
58
+ argv: [lock_digest])
34
59
  end
35
60
 
61
+ #
36
62
  # Delete the keys belonging to the job
63
+ #
64
+ #
65
+ # @return [Integer] the number of keys deleted
66
+ #
37
67
  def delete_lock
38
- Scripts.call(:delete_by_digest, nil, keys: [UNIQUE_SET, unique_digest])
68
+ digests.delete_by_digest(lock_digest)
69
+ end
70
+
71
+ #
72
+ # Access to the {Digests}
73
+ #
74
+ #
75
+ # @return [Digests] and instance with digests
76
+ #
77
+ def digests
78
+ @digests ||= SidekiqUniqueJobs::Digests.new
39
79
  end
40
80
  end
41
81
  end
@@ -4,20 +4,31 @@ module SidekiqUniqueJobs
4
4
  module OnConflict
5
5
  # Strategy to reschedule job on conflict
6
6
  #
7
- # @author Mikael Henriksson <mikael@zoolutions.se>
7
+ # @author Mikael Henriksson <mikael@mhenrixon.com>
8
8
  class Reschedule < OnConflict::Strategy
9
9
  include SidekiqUniqueJobs::SidekiqWorkerMethods
10
+ include SidekiqUniqueJobs::Logging
11
+ include SidekiqUniqueJobs::JSON
12
+ include SidekiqUniqueJobs::Reflectable
10
13
 
11
14
  # @param [Hash] item sidekiq job hash
12
- def initialize(item)
13
- super
14
- @worker_class = item[CLASS_KEY]
15
+ def initialize(item, redis_pool = nil)
16
+ super(item, redis_pool)
17
+ self.job_class = item[CLASS]
15
18
  end
16
19
 
17
20
  # Create a new job from the current one.
18
21
  # This will mess up sidekiq stats because a new job is created
19
22
  def call
20
- worker_class&.perform_in(5, *item[ARGS_KEY]) if sidekiq_worker_class?
23
+ if sidekiq_job_class?
24
+ if job_class.set(queue: item["queue"].to_sym).perform_in(5, *item[ARGS])
25
+ reflect(:rescheduled, item)
26
+ else
27
+ reflect(:reschedule_failed, item)
28
+ end
29
+ else
30
+ reflect(:unknown_sidekiq_worker, item)
31
+ end
21
32
  end
22
33
  end
23
34
  end
@@ -5,25 +5,44 @@ module SidekiqUniqueJobs
5
5
  # Abstract conflict strategy class
6
6
  #
7
7
  # @abstract
8
- # @author Mikael Henriksson <mikael@zoolutions.se>
8
+ # @author Mikael Henriksson <mikael@mhenrixon.com>
9
9
  class Strategy
10
+ include SidekiqUniqueJobs::JSON
10
11
  include SidekiqUniqueJobs::Logging
12
+ include SidekiqUniqueJobs::Script::Caller
13
+ include SidekiqUniqueJobs::Timing
11
14
 
12
- # The sidekiq job hash
13
- # @return [Hash] the Sidekiq job hash
15
+ # @!attribute [r] item
16
+ # @return [Hash] sidekiq job hash
14
17
  attr_reader :item
18
+ # @!attribute [r] redis_pool
19
+ # @return [Sidekiq::RedisConnection, ConnectionPool, NilClass] the redis connection
20
+ attr_reader :redis_pool
15
21
 
16
- # @param [Hash] item the Sidekiq job hash
17
- def initialize(item)
18
- @item = item
22
+ #
23
+ # Initialize a new Strategy
24
+ #
25
+ # @param [Hash] item sidekiq job hash
26
+ # @param [ConnectionPool] redis_pool the connection pool instance
27
+ #
28
+ def initialize(item, redis_pool = nil)
29
+ @item = item
30
+ @redis_pool = redis_pool
19
31
  end
20
32
 
21
33
  # Use strategy on conflict
22
34
  # @raise [NotImplementedError] needs to be implemented in child class
23
35
  def call
24
- fail NotImplementedError, 'needs to be implemented in child class'
36
+ raise NotImplementedError, "needs to be implemented in child class"
25
37
  end
26
38
 
39
+ #
40
+ # Check if the strategy is kind of {Replace}
41
+ #
42
+ #
43
+ # @return [true] when the strategy is a {Replace}
44
+ # @return [false] when the strategy is not a {Replace}
45
+ #
27
46
  def replace?
28
47
  is_a?(Replace)
29
48
  end
@@ -1,26 +1,44 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative 'on_conflict/strategy'
4
- require_relative 'on_conflict/null_strategy'
5
- require_relative 'on_conflict/log'
6
- require_relative 'on_conflict/raise'
7
- require_relative 'on_conflict/reject'
8
- require_relative 'on_conflict/replace'
9
- require_relative 'on_conflict/reschedule'
3
+ require_relative "on_conflict/strategy"
4
+ require_relative "on_conflict/null_strategy"
5
+ require_relative "on_conflict/log"
6
+ require_relative "on_conflict/raise"
7
+ require_relative "on_conflict/reject"
8
+ require_relative "on_conflict/replace"
9
+ require_relative "on_conflict/reschedule"
10
10
 
11
11
  module SidekiqUniqueJobs
12
+ #
13
+ # Provides lock conflict resolutions
14
+ #
15
+ # @author Mikael Henriksson <mikael@mhenrixon.com>
16
+ #
12
17
  module OnConflict
13
- STRATEGIES = {
14
- log: OnConflict::Log,
15
- raise: OnConflict::Raise,
16
- reject: OnConflict::Reject,
17
- replace: OnConflict::Replace,
18
- reschedule: OnConflict::Reschedule,
19
- }.freeze
18
+ # A convenience method for using the configured strategies
19
+ def self.strategies
20
+ SidekiqUniqueJobs.strategies
21
+ end
20
22
 
21
- # returns OnConflict::NullStrategy when no other could be found
23
+ #
24
+ # Find a strategy to use for conflicting locks
25
+ #
26
+ # @param [Symbol] strategy the key for the strategy
27
+ #
28
+ # @return [OnConflict::Strategy] when found
29
+ # @return [OnConflict::NullStrategy] when no other could be found
30
+ #
22
31
  def self.find_strategy(strategy)
23
- STRATEGIES.fetch(strategy.to_s.to_sym) { OnConflict::NullStrategy }
32
+ return OnConflict::NullStrategy unless strategy
33
+
34
+ strategies.fetch(strategy.to_sym) do
35
+ SidekiqUniqueJobs.logger.warn(
36
+ "No matching implementation for strategy: #{strategy}, returning OnConflict::NullStrategy." \
37
+ " Available strategies are (#{strategies.inspect})",
38
+ )
39
+
40
+ OnConflict::NullStrategy
41
+ end
24
42
  end
25
43
  end
26
44
  end