sidekiq-unique-jobs 6.0.25 → 7.1.33

Sign up to get free protection for your applications and to get access to all the features.
Files changed (130) 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 +46 -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 +30 -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/lock_type.rb +37 -0
  36. data/lib/sidekiq_unique_jobs/locksmith.rb +305 -101
  37. data/lib/sidekiq_unique_jobs/logging/middleware_context.rb +44 -0
  38. data/lib/sidekiq_unique_jobs/logging.rb +202 -33
  39. data/lib/sidekiq_unique_jobs/lua/delete.lua +51 -0
  40. data/lib/sidekiq_unique_jobs/lua/delete_by_digest.lua +42 -0
  41. data/lib/sidekiq_unique_jobs/lua/delete_job_by_digest.lua +38 -0
  42. data/lib/sidekiq_unique_jobs/lua/find_digest_in_queues.lua +26 -0
  43. data/lib/sidekiq_unique_jobs/lua/lock.lua +99 -0
  44. data/lib/sidekiq_unique_jobs/lua/lock_until_expired.lua +92 -0
  45. data/lib/sidekiq_unique_jobs/lua/locked.lua +35 -0
  46. data/lib/sidekiq_unique_jobs/lua/queue.lua +87 -0
  47. data/lib/sidekiq_unique_jobs/lua/reap_orphans.lua +122 -0
  48. data/lib/sidekiq_unique_jobs/lua/shared/_common.lua +40 -0
  49. data/lib/sidekiq_unique_jobs/lua/shared/_current_time.lua +8 -0
  50. data/lib/sidekiq_unique_jobs/lua/shared/_delete_from_queue.lua +22 -0
  51. data/lib/sidekiq_unique_jobs/lua/shared/_delete_from_sorted_set.lua +18 -0
  52. data/lib/sidekiq_unique_jobs/lua/shared/_find_digest_in_process_set.lua +53 -0
  53. data/lib/sidekiq_unique_jobs/lua/shared/_find_digest_in_queues.lua +43 -0
  54. data/lib/sidekiq_unique_jobs/lua/shared/_find_digest_in_sorted_set.lua +24 -0
  55. data/lib/sidekiq_unique_jobs/lua/shared/_hgetall.lua +13 -0
  56. data/lib/sidekiq_unique_jobs/lua/shared/_upgrades.lua +3 -0
  57. data/lib/sidekiq_unique_jobs/lua/unlock.lua +107 -0
  58. data/lib/sidekiq_unique_jobs/lua/update_version.lua +40 -0
  59. data/lib/sidekiq_unique_jobs/lua/upgrade.lua +68 -0
  60. data/lib/sidekiq_unique_jobs/middleware/client.rb +42 -0
  61. data/lib/sidekiq_unique_jobs/middleware/server.rb +31 -0
  62. data/lib/sidekiq_unique_jobs/middleware.rb +29 -43
  63. data/lib/sidekiq_unique_jobs/normalizer.rb +4 -4
  64. data/lib/sidekiq_unique_jobs/on_conflict/log.rb +9 -5
  65. data/lib/sidekiq_unique_jobs/on_conflict/null_strategy.rb +1 -1
  66. data/lib/sidekiq_unique_jobs/on_conflict/raise.rb +1 -1
  67. data/lib/sidekiq_unique_jobs/on_conflict/reject.rb +63 -17
  68. data/lib/sidekiq_unique_jobs/on_conflict/replace.rb +54 -14
  69. data/lib/sidekiq_unique_jobs/on_conflict/reschedule.rb +16 -5
  70. data/lib/sidekiq_unique_jobs/on_conflict/strategy.rb +25 -6
  71. data/lib/sidekiq_unique_jobs/on_conflict.rb +23 -10
  72. data/lib/sidekiq_unique_jobs/options_with_fallback.rb +39 -36
  73. data/lib/sidekiq_unique_jobs/orphans/lua_reaper.rb +29 -0
  74. data/lib/sidekiq_unique_jobs/orphans/manager.rb +241 -0
  75. data/lib/sidekiq_unique_jobs/orphans/null_reaper.rb +24 -0
  76. data/lib/sidekiq_unique_jobs/orphans/observer.rb +42 -0
  77. data/lib/sidekiq_unique_jobs/orphans/reaper.rb +114 -0
  78. data/lib/sidekiq_unique_jobs/orphans/reaper_resurrector.rb +170 -0
  79. data/lib/sidekiq_unique_jobs/orphans/ruby_reaper.rb +298 -0
  80. data/lib/sidekiq_unique_jobs/redis/entity.rb +112 -0
  81. data/lib/sidekiq_unique_jobs/redis/hash.rb +56 -0
  82. data/lib/sidekiq_unique_jobs/redis/list.rb +32 -0
  83. data/lib/sidekiq_unique_jobs/redis/set.rb +32 -0
  84. data/lib/sidekiq_unique_jobs/redis/sorted_set.rb +86 -0
  85. data/lib/sidekiq_unique_jobs/redis/string.rb +51 -0
  86. data/lib/sidekiq_unique_jobs/redis.rb +11 -0
  87. data/lib/sidekiq_unique_jobs/reflectable.rb +26 -0
  88. data/lib/sidekiq_unique_jobs/reflections.rb +79 -0
  89. data/lib/sidekiq_unique_jobs/rspec/matchers/have_valid_sidekiq_options.rb +51 -0
  90. data/lib/sidekiq_unique_jobs/rspec/matchers.rb +26 -0
  91. data/lib/sidekiq_unique_jobs/script/caller.rb +127 -0
  92. data/lib/sidekiq_unique_jobs/script.rb +15 -0
  93. data/lib/sidekiq_unique_jobs/server.rb +61 -0
  94. data/lib/sidekiq_unique_jobs/sidekiq_unique_ext.rb +114 -65
  95. data/lib/sidekiq_unique_jobs/sidekiq_unique_jobs.rb +252 -36
  96. data/lib/sidekiq_unique_jobs/sidekiq_worker_methods.rb +47 -32
  97. data/lib/sidekiq_unique_jobs/testing.rb +102 -29
  98. data/lib/sidekiq_unique_jobs/timer_task.rb +299 -0
  99. data/lib/sidekiq_unique_jobs/timing.rb +58 -0
  100. data/lib/sidekiq_unique_jobs/unlockable.rb +20 -4
  101. data/lib/sidekiq_unique_jobs/update_version.rb +25 -0
  102. data/lib/sidekiq_unique_jobs/upgrade_locks.rb +155 -0
  103. data/lib/sidekiq_unique_jobs/version.rb +3 -1
  104. data/lib/sidekiq_unique_jobs/version_check.rb +23 -4
  105. data/lib/sidekiq_unique_jobs/web/helpers.rb +138 -13
  106. data/lib/sidekiq_unique_jobs/web/views/_paging.erb +4 -4
  107. data/lib/sidekiq_unique_jobs/web/views/changelogs.erb +54 -0
  108. data/lib/sidekiq_unique_jobs/web/views/lock.erb +110 -0
  109. data/lib/sidekiq_unique_jobs/web/views/locks.erb +54 -0
  110. data/lib/sidekiq_unique_jobs/web.rb +82 -32
  111. data/lib/sidekiq_unique_jobs.rb +54 -7
  112. data/lib/tasks/changelog.rake +16 -16
  113. metadata +134 -177
  114. data/lib/sidekiq_unique_jobs/client/middleware.rb +0 -56
  115. data/lib/sidekiq_unique_jobs/scripts.rb +0 -118
  116. data/lib/sidekiq_unique_jobs/server/middleware.rb +0 -46
  117. data/lib/sidekiq_unique_jobs/timeout/calculator.rb +0 -63
  118. data/lib/sidekiq_unique_jobs/timeout.rb +0 -8
  119. data/lib/sidekiq_unique_jobs/unique_args.rb +0 -150
  120. data/lib/sidekiq_unique_jobs/util.rb +0 -103
  121. data/lib/sidekiq_unique_jobs/web/views/unique_digest.erb +0 -28
  122. data/lib/sidekiq_unique_jobs/web/views/unique_digests.erb +0 -46
  123. data/redis/acquire_lock.lua +0 -21
  124. data/redis/convert_legacy_lock.lua +0 -13
  125. data/redis/delete.lua +0 -14
  126. data/redis/delete_by_digest.lua +0 -23
  127. data/redis/delete_job_by_digest.lua +0 -60
  128. data/redis/lock.lua +0 -62
  129. data/redis/release_stale_locks.lua +0 -90
  130. data/redis/unlock.lua +0 -35
@@ -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,59 +1,45 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "sidekiq"
4
-
5
3
  module SidekiqUniqueJobs
6
4
  #
7
5
  # Provides the sidekiq middleware that makes the gem work
8
6
  #
9
- # @author Mikael Henriksson <mikael@zoolutions.se>
7
+ # @author Mikael Henriksson <mikael@mhenrixon.com>
10
8
  #
11
9
  module Middleware
12
- def self.extended(base)
13
- base.class_eval do
14
- configure_middleware
15
- end
16
- end
10
+ include SidekiqUniqueJobs::Logging::Middleware
11
+ include SidekiqUniqueJobs::OptionsWithFallback
12
+ include SidekiqUniqueJobs::JSON
17
13
 
18
- def configure_middleware
19
- configure_server_middleware
20
- configure_client_middleware
21
- end
14
+ # The sidekiq job hash
15
+ # @return [Hash] the Sidekiq job hash
16
+ attr_reader :item
22
17
 
23
- def configure_server_middleware # rubocop:disable Metrics/MethodLength
24
- Sidekiq.configure_server do |config|
25
- config.client_middleware do |chain|
26
- require "sidekiq_unique_jobs/client/middleware"
27
- if defined?(Apartment::Sidekiq::Middleware::Client)
28
- chain.insert_after Apartment::Sidekiq::Middleware::Client, SidekiqUniqueJobs::Client::Middleware
29
- else
30
- chain.add SidekiqUniqueJobs::Client::Middleware
31
- end
32
- 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?
33
37
 
34
- config.server_middleware do |chain|
35
- require "sidekiq_unique_jobs/server/middleware"
36
- if defined?(Apartment::Sidekiq::Middleware::Server)
37
- chain.insert_after Apartment::Sidekiq::Middleware::Server, SidekiqUniqueJobs::Server::Middleware
38
- else
39
- chain.add SidekiqUniqueJobs::Server::Middleware
40
- end
41
- end
42
- end
43
- end
38
+ SidekiqUniqueJobs::Job.prepare(item) unless item[LOCK_DIGEST]
44
39
 
45
- def configure_client_middleware
46
- Sidekiq.configure_client do |config|
47
- config.client_middleware do |chain|
48
- require "sidekiq_unique_jobs/client/middleware"
49
- if defined?(Apartment::Sidekiq::Middleware::Client)
50
- chain.insert_after Apartment::Sidekiq::Middleware::Client, SidekiqUniqueJobs::Client::Middleware
51
- else
52
- chain.add SidekiqUniqueJobs::Client::Middleware
53
- end
54
- end
40
+ with_logging_context do
41
+ super
55
42
  end
56
43
  end
57
44
  end
58
45
  end
59
- SidekiqUniqueJobs.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,7 +4,7 @@ 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
@@ -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
- SidekiqUniqueJobs::Digests.delete_by_digest(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,17 +5,29 @@ 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
@@ -24,6 +36,13 @@ module SidekiqUniqueJobs
24
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
@@ -12,20 +12,33 @@ module SidekiqUniqueJobs
12
12
  #
13
13
  # Provides lock conflict resolutions
14
14
  #
15
- # @author Mikael Henriksson <mikael@zoolutions.se>
15
+ # @author Mikael Henriksson <mikael@mhenrixon.com>
16
16
  #
17
17
  module OnConflict
18
- STRATEGIES = {
19
- log: OnConflict::Log,
20
- raise: OnConflict::Raise,
21
- reject: OnConflict::Reject,
22
- replace: OnConflict::Replace,
23
- reschedule: OnConflict::Reschedule,
24
- }.freeze
18
+ # A convenience method for using the configured strategies
19
+ def self.strategies
20
+ SidekiqUniqueJobs.strategies
21
+ end
25
22
 
26
- # 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
+ #
27
31
  def self.find_strategy(strategy)
28
- 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
29
42
  end
30
43
  end
31
44
  end