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
@@ -2,113 +2,162 @@
2
2
 
3
3
  require "sidekiq/api"
4
4
 
5
+ #
6
+ # @api private
7
+ #
5
8
  module Sidekiq
9
+ # See Sidekiq::Api
6
10
  class SortedEntry
11
+ #
12
+ # Provides extensions for unlocking jobs that are removed and deleted
13
+ #
14
+ # @author Mikael Henriksson <mikael@mhenrixon.com>
15
+ #
7
16
  module UniqueExtension
8
- def self.included(base)
9
- base.class_eval do
10
- alias_method :delete_orig, :delete
11
- alias_method :delete, :delete_ext
12
- alias_method :remove_job_orig, :remove_job
13
- alias_method :remove_job, :remove_job_ext
14
- end
15
- end
16
-
17
- def delete_ext
18
- SidekiqUniqueJobs::Unlockable.unlock(item) if delete_orig
17
+ #
18
+ # Wraps the original method to ensure locks for the job are deleted
19
+ #
20
+ #
21
+ # @return [Hash] the deleted sidekiq job hash
22
+ #
23
+ def delete
24
+ SidekiqUniqueJobs::Unlockable.delete!(item) if super
25
+ item
19
26
  end
20
27
 
21
28
  private
22
29
 
23
- def remove_job_ext
24
- remove_job_orig do |message|
25
- SidekiqUniqueJobs::Unlockable.unlock(Sidekiq.load_json(message))
30
+ #
31
+ # Wraps the original method to ensure locks for the job are deleted
32
+ #
33
+ #
34
+ # @yieldparam [Hash] message the sidekiq job hash
35
+ def remove_job
36
+ super do |message|
37
+ SidekiqUniqueJobs::Unlockable.delete!(Sidekiq.load_json(message))
26
38
  yield message
27
39
  end
28
40
  end
29
41
  end
30
42
 
31
- include UniqueExtension
43
+ prepend UniqueExtension
32
44
  end
33
45
 
46
+ # See Sidekiq::Api
34
47
  class ScheduledSet
48
+ #
49
+ # Provides extensions for unlocking jobs that are removed and deleted
50
+ #
51
+ # @author Mikael Henriksson <mikael@mhenrixon.com>
52
+ #
35
53
  module UniqueExtension
36
- def self.included(base)
37
- base.class_eval do
38
- alias_method :delete_orig, :delete
39
- alias_method :delete, :delete_ext
40
- end
41
- end
42
-
43
- def delete_ext
44
- SidekiqUniqueJobs::Unlockable.unlock(item) if delete_orig
54
+ #
55
+ # Wraps the original method to ensure locks for the job are deleted
56
+ #
57
+ #
58
+ # @param [Integer, Float] score the score in the scheduled set
59
+ # @param [String] job_id the Sidekiq JID
60
+ #
61
+ def delete(score, job_id)
62
+ entry = find_job(job_id)
63
+ SidekiqUniqueJobs::Unlockable.delete!(entry.item) if super(score, job_id)
64
+ entry
45
65
  end
46
66
  end
47
- include UniqueExtension
67
+
68
+ prepend UniqueExtension
48
69
  end
49
70
 
50
- class Job
51
- module UniqueExtension
52
- def self.included(base)
53
- base.class_eval do
54
- alias_method :delete_orig, :delete
55
- alias_method :delete, :delete_ext
71
+ if Sidekiq.const_defined?(:JobRecord)
72
+ # See Sidekiq::Api
73
+ class JobRecord
74
+ #
75
+ # Provides extensions for unlocking jobs that are removed and deleted
76
+ #
77
+ # @author Mikael Henriksson <mikael@mhenrixon.com>
78
+ #
79
+ module UniqueExtension
80
+ #
81
+ # Wraps the original method to ensure locks for the job are deleted
82
+ #
83
+ def delete
84
+ SidekiqUniqueJobs::Unlockable.delete!(item)
85
+ super
56
86
  end
57
87
  end
58
88
 
59
- def delete_ext
60
- delete_orig
61
- SidekiqUniqueJobs::Unlockable.unlock(item)
62
- end
89
+ prepend UniqueExtension
63
90
  end
91
+ else
92
+ # See Sidekiq::Api
93
+ class Job
94
+ #
95
+ # Provides extensions for unlocking jobs that are removed and deleted
96
+ #
97
+ # @author Mikael Henriksson <mikael@mhenrixon.com>
98
+ #
99
+ module UniqueExtension
100
+ #
101
+ # Wraps the original method to ensure locks for the job are deleted
102
+ #
103
+ def delete
104
+ SidekiqUniqueJobs::Unlockable.delete!(item)
105
+ super
106
+ end
107
+ end
64
108
 
65
- include UniqueExtension
109
+ prepend UniqueExtension
110
+ end
66
111
  end
67
112
 
113
+ # See Sidekiq::Api
68
114
  class Queue
115
+ #
116
+ # Provides extensions for unlocking jobs that are removed and deleted
117
+ #
118
+ # @author Mikael Henriksson <mikael@mhenrixon.com>
119
+ #
69
120
  module UniqueExtension
70
- def self.included(base)
71
- base.class_eval do
72
- alias_method :clear_orig, :clear
73
- alias_method :clear, :clear_ext
74
- end
75
- end
76
-
77
- def clear_ext
121
+ #
122
+ # Wraps the original method to ensure locks for the job are deleted
123
+ #
124
+ def clear
78
125
  each(&:delete)
79
- clear_orig
126
+ super
80
127
  end
81
128
  end
82
129
 
83
- include UniqueExtension
130
+ prepend UniqueExtension
84
131
  end
85
132
 
133
+ # See Sidekiq::Api
86
134
  class JobSet
135
+ #
136
+ # Provides extensions for unlocking jobs that are removed and deleted
137
+ #
138
+ # @author Mikael Henriksson <mikael@mhenrixon.com>
139
+ #
87
140
  module UniqueExtension
88
- def self.included(base)
89
- base.class_eval do
90
- if base.method_defined?(:clear)
91
- alias_method :clear_orig, :clear
92
- alias_method :clear, :clear_ext
93
- end
94
-
95
- if base.method_defined?(:delete_by_value)
96
- alias_method :delete_by_value_orig, :delete_by_value
97
- alias_method :delete_by_value, :delete_by_value_ext
98
- end
99
- end
100
- end
101
-
102
- def clear_ext
141
+ #
142
+ # Wraps the original method to ensure locks for the job are deleted
143
+ #
144
+ def clear
103
145
  each(&:delete)
104
- clear_orig
146
+ super
105
147
  end
106
148
 
107
- def delete_by_value_ext(name, value)
108
- SidekiqUniqueJobs::Unlockable.unlock(Sidekiq.load_json(value)) if delete_by_value_orig(name, value)
149
+ #
150
+ # Wraps the original method to ensure locks for the job are deleted
151
+ #
152
+ #
153
+ # @param [String] name the name of the key
154
+ # @param [String] value a sidekiq job hash
155
+ #
156
+ def delete_by_value(name, value)
157
+ SidekiqUniqueJobs::Unlockable.delete!(Sidekiq.load_json(value)) if super(name, value)
109
158
  end
110
159
  end
111
160
 
112
- include UniqueExtension
161
+ prepend UniqueExtension
113
162
  end
114
163
  end
@@ -3,73 +3,182 @@
3
3
  #
4
4
  # Contains configuration and utility methods that belongs top level
5
5
  #
6
- # @author Mikael Henriksson <mikael@zoolutions.se>
7
- module SidekiqUniqueJobs
6
+ # @author Mikael Henriksson <mikael@mhenrixon.com>
7
+ module SidekiqUniqueJobs # rubocop:disable Metrics/ModuleLength
8
8
  include SidekiqUniqueJobs::Connection
9
+ extend SidekiqUniqueJobs::JSON
9
10
 
10
11
  module_function
11
12
 
12
- Config = Concurrent::MutableStruct.new(
13
- :default_lock_timeout,
14
- :enabled,
15
- :unique_prefix,
16
- :logger,
17
- )
18
-
13
+ #
19
14
  # The current configuration (See: {.configure} on how to configure)
15
+ #
16
+ #
17
+ # @return [SidekiqUniqueJobs::Config] the gem configuration
18
+ #
20
19
  def config
21
- # Arguments here need to match the definition of the new class (see above)
22
- @config ||= Config.new(
23
- 0,
24
- true,
25
- "uniquejobs",
26
- Sidekiq.logger,
27
- )
20
+ @config ||= reset! # rubocop:disable ThreadSafety/InstanceVariableInClassMethod
21
+ end
22
+
23
+ #
24
+ # The current strategies
25
+ #
26
+ #
27
+ # @return [Hash<Symbol, SidekiqUniqueJobs::Strategy>] the configured locks
28
+ #
29
+ def strategies
30
+ config.strategies
31
+ end
32
+
33
+ #
34
+ # The current locks
35
+ #
36
+ #
37
+ # @return [Hash<Symbol, SidekiqUniqueJobs::BaseLock>] the configured locks
38
+ #
39
+ def locks
40
+ config.locks
28
41
  end
29
42
 
43
+ #
30
44
  # The current logger
45
+ #
46
+ #
31
47
  # @return [Logger] the configured logger
48
+ #
32
49
  def logger
33
50
  config.logger
34
51
  end
35
52
 
36
- # :reek:ManualDispatch
37
- def with_context(context)
38
- if logger.respond_to?(:with_context)
39
- logger.with_context(context) { yield }
40
- elsif defined?(Sidekiq::Context)
41
- Sidekiq::Context.with(context) { yield }
42
- elsif defined?(Sidekiq::Logging)
43
- Sidekiq::Logging.with_context(context) { yield }
44
- else
45
- yield
46
- end
53
+ #
54
+ # The current gem version
55
+ #
56
+ #
57
+ # @return [String] the current gem version
58
+ #
59
+ def version
60
+ VERSION
47
61
  end
48
62
 
63
+ #
49
64
  # Set a new logger
50
- # @param [Logger] other a new logger
65
+ #
66
+ # @param [Logger] other another logger
67
+ #
68
+ # @return [Logger] the new logger
69
+ #
51
70
  def logger=(other)
52
71
  config.logger = other
53
72
  end
54
73
 
55
- # Change global configuration while yielding
74
+ #
75
+ # Check if logging is enabled
76
+ #
77
+ #
78
+ # @return [true, false]
79
+ #
80
+ def logging?
81
+ config.logger_enabled
82
+ end
83
+
84
+ #
85
+ # Temporarily use another configuration and reset to the old config after yielding
86
+ #
87
+ # @param [Hash] tmp_config the temporary configuration to use
88
+ #
89
+ # @return [void]
90
+ #
56
91
  # @yield control to the caller
57
- def use_config(tmp_config)
92
+ def use_config(tmp_config = {})
58
93
  raise ::ArgumentError, "#{name}.#{__method__} needs a block" unless block_given?
59
94
 
60
95
  old_config = config.to_h
96
+ reset!
61
97
  configure(tmp_config)
62
98
  yield
63
- configure(old_config)
99
+ ensure
100
+ reset!
101
+ configure(old_config.to_h)
102
+ end
103
+
104
+ #
105
+ # Resets configuration to deafult
106
+ #
107
+ #
108
+ # @return [SidekiqUniqueJobs::Config] a default gem configuration
109
+ #
110
+ def reset!
111
+ @config = SidekiqUniqueJobs::Config.default # rubocop:disable ThreadSafety/InstanceVariableInClassMethod
112
+ end
113
+
114
+ #
115
+ # Enable SidekiqUniuqeJobs either temporarily in a block or for good
116
+ #
117
+ #
118
+ # @return [true] when not given a block
119
+ # @return [true, false] the previous value of enable when given a block
120
+ #
121
+ # @yieldreturn [void] temporarily enable sidekiq unique jobs while executing a block of code
122
+ def enable!(&block)
123
+ toggle(true, &block)
124
+ end
125
+
126
+ #
127
+ # Disable SidekiqUniuqeJobs either temporarily in a block or for good
128
+ #
129
+ #
130
+ # @return [false] when not given a block
131
+ # @return [true, false] the previous value of enable when given a block
132
+ #
133
+ # @yieldreturn [void] temporarily disable sidekiq unique jobs while executing a block of code
134
+ def disable!(&block)
135
+ toggle(false, &block)
136
+ end
137
+
138
+ #
139
+ # Checks if the gem has been disabled
140
+ #
141
+ # @return [true] when config.enabled is true
142
+ # @return [false] when config.enabled is false
143
+ #
144
+ def enabled?
145
+ config.enabled
146
+ end
147
+
148
+ #
149
+ # Checks if the gem has been disabled
150
+ #
151
+ # @return [true] when config.enabled is false
152
+ # @return [false] when config.enabled is true
153
+ #
154
+ def disabled?
155
+ !enabled?
156
+ end
157
+
158
+ #
159
+ # Toggles enabled on or off
160
+ #
161
+ # @api private
162
+ # :nodoc:
163
+ def toggle(enabled)
164
+ if block_given?
165
+ enabled_was = config.enabled
166
+ config.enabled = enabled
167
+ yield
168
+ config.enabled = enabled_was
169
+ else
170
+ config.enabled = enabled
171
+ end
64
172
  end
65
173
 
66
174
  # Configure the gem
67
175
  #
68
176
  # This is usually called once at startup of an application
69
177
  # @param [Hash] options global gem options
70
- # @option options [Integer] :default_lock_timeout (default is 0)
178
+ # @option options [Integer] :lock_timeout (default is 0)
179
+ # @option options [Integer] :lock_ttl (default is 0)
71
180
  # @option options [true,false] :enabled (default is true)
72
- # @option options [String] :unique_prefix (default is 'uniquejobs')
181
+ # @option options [String] :lock_prefix (default is 'uniquejobs')
73
182
  # @option options [Logger] :logger (default is Sidekiq.logger)
74
183
  # @yield control to the caller when given block
75
184
  def configure(options = {})
@@ -77,12 +186,119 @@ module SidekiqUniqueJobs
77
186
  yield config
78
187
  else
79
188
  options.each do |key, val|
80
- config.send("#{key}=", val)
189
+ config.send(:"#{key}=", val)
81
190
  end
82
191
  end
83
192
  end
84
193
 
85
- def redis_version
86
- @redis_version ||= redis { |conn| conn.info("server")["redis_version"] }
194
+ #
195
+ # Returns the current redis version
196
+ #
197
+ #
198
+ # @return [String] a string like `5.0.2`
199
+ #
200
+ def fetch_redis_version
201
+ Sidekiq.redis_info["redis_version"]
202
+ end
203
+
204
+ #
205
+ # Current time as float
206
+ #
207
+ #
208
+ # @return [Float]
209
+ #
210
+ def now_f
211
+ now.to_f
212
+ end
213
+
214
+ #
215
+ # Current time
216
+ #
217
+ #
218
+ # @return [Time]
219
+ #
220
+ def now
221
+ Time.now
222
+ end
223
+
224
+ #
225
+ # Checks that the worker is valid with the given options
226
+ #
227
+ # @param [Hash] options the `sidekiq_options` to validate
228
+ #
229
+ # @return [Boolean]
230
+ #
231
+ def validate_worker(options)
232
+ raise NotUniqueWorker, options unless (lock_type = options[LOCK])
233
+
234
+ lock_class = locks[lock_type]
235
+ lock_class.validate_options(options)
236
+ end
237
+
238
+ #
239
+ # Checks that the worker is valid with the given options
240
+ #
241
+ # @param [Hash] options the `sidekiq_options` to validate
242
+ #
243
+ # @raise [InvalidWorker] when {#validate_worker} returns false or nil
244
+ #
245
+ def validate_worker!(options)
246
+ lock_config = validate_worker(options)
247
+ raise InvalidWorker, lock_config unless lock_config.errors.empty?
248
+ end
249
+
250
+ # Attempt to constantize a string worker_class argument, always
251
+ # failing back to the original argument when the constant can't be found
252
+ #
253
+ # @return [Sidekiq::Worker]
254
+ def constantize(str)
255
+ return str.class if str.is_a?(Sidekiq::Worker) # sidekiq v6.x
256
+ return str unless str.is_a?(String)
257
+ return Object.const_get(str) unless str.include?("::")
258
+
259
+ names = str.split("::")
260
+ names.shift if names.empty? || names.first.empty?
261
+
262
+ names.inject(Object) do |constant, name|
263
+ # the false flag limits search for name to under the constant namespace
264
+ # which mimics Rails' behaviour
265
+ constant.const_get(name, false)
266
+ end
267
+ end
268
+
269
+ # Attempt to constantize a string worker_class argument, always
270
+ # failing back to the original argument when the constant can't be found
271
+ #
272
+ # @return [Sidekiq::Worker, String]
273
+ def safe_constantize(str)
274
+ constantize(str)
275
+ rescue NameError => ex
276
+ case ex.message
277
+ when /uninitialized constant/
278
+ str
279
+ else
280
+ raise
281
+ end
282
+ end
283
+
284
+ #
285
+ # Collection with notifications
286
+ #
287
+ #
288
+ # @return [Reflections]
289
+ #
290
+ def reflections
291
+ @reflections ||= Reflections.new # rubocop:disable ThreadSafety/InstanceVariableInClassMethod
292
+ end
293
+
294
+ #
295
+ # Yields notification stack for sidekiq unique jobs to configure notifications
296
+ #
297
+ #
298
+ # @return [void] <description>
299
+ #
300
+ # @yieldparam [Reflections] x used to configure notifications
301
+ def reflect
302
+ yield reflections if block_given?
87
303
  end
88
304
  end
@@ -3,43 +3,58 @@
3
3
  module SidekiqUniqueJobs
4
4
  # Module with convenience methods for the Sidekiq::Worker class
5
5
  #
6
- # @author Mikael Henriksson <mikael@zoolutions.se>
6
+ # @author Mikael Henriksson <mikael@mhenrixon.com>
7
7
  module SidekiqWorkerMethods
8
+ #
9
+ # @!attribute [r] job_class
10
+ # @return [Sidekiq::Worker] The Sidekiq::Worker implementation
11
+ attr_reader :job_class
12
+
8
13
  # Avoids duplicating worker_class.respond_to? in multiple places
9
14
  # @return [true, false]
10
- def worker_method_defined?(method_sym)
11
- worker_class.respond_to?(method_sym)
15
+ def job_method_defined?(method_sym)
16
+ job_class.respond_to?(method_sym)
12
17
  end
13
18
 
14
19
  # Wraps #get_sidekiq_options to always work with a hash
15
20
  # @return [Hash] of the worker class sidekiq options
16
- def worker_options
17
- return {} unless sidekiq_worker_class?
21
+ def job_options
22
+ return {} unless sidekiq_job_class?
18
23
 
19
- worker_class.get_sidekiq_options.stringify_keys
24
+ job_class.get_sidekiq_options.deep_stringify_keys
20
25
  end
21
26
 
22
27
  # Tests that the
23
- # @return [true] if worker_class responds to get_sidekiq_options
24
- # @return [false] if worker_class does not respond to get_sidekiq_options
25
- def sidekiq_worker_class?
26
- worker_method_defined?(:get_sidekiq_options)
28
+ # @return [true] if job_class responds to get_sidekiq_options
29
+ # @return [false] if job_class does not respond to get_sidekiq_options
30
+ def sidekiq_job_class?
31
+ job_method_defined?(:get_sidekiq_options)
27
32
  end
28
33
 
29
- # The Sidekiq::Worker implementation
30
- # @return [Sidekiq::Worker]
31
- def worker_class
32
- @_worker_class ||= worker_class_constantize(@worker_class) # rubocop:disable Naming/MemoizedInstanceVariableName
34
+ def job_class=(obj)
35
+ # this is what was originally passed in, it can be an instance or a class depending on sidekiq version
36
+ @original_job_class = obj
37
+ @job_class = job_class_constantize(obj)
33
38
  end
34
39
 
35
40
  # The hook to call after a successful unlock
36
41
  # @return [Proc]
37
- def after_unlock_hook
42
+ def after_unlock_hook # rubocop:disable Metrics/MethodLength
38
43
  lambda do
39
- if @worker_class.respond_to?(:after_unlock)
40
- @worker_class.after_unlock # instance method in sidekiq v6
41
- elsif worker_class.respond_to?(:after_unlock)
42
- worker_class.after_unlock # class method regardless of sidekiq version
44
+ if @original_job_class.respond_to?(:after_unlock)
45
+ # instance method in sidekiq v6
46
+ if @original_job_class.method(:after_unlock).arity.positive? # arity check to maintain backwards compatibility
47
+ @original_job_class.after_unlock(item)
48
+ else
49
+ @original_job_class.after_unlock
50
+ end
51
+ elsif job_class.respond_to?(:after_unlock)
52
+ # class method regardless of sidekiq version
53
+ if job_class.method(:after_unlock).arity.positive? # arity check to maintain backwards compatibility
54
+ job_class.after_unlock(item)
55
+ else
56
+ job_class.after_unlock
57
+ end
43
58
  end
44
59
  end
45
60
  end
@@ -48,22 +63,22 @@ module SidekiqUniqueJobs
48
63
  # failing back to the original argument when the constant can't be found
49
64
  #
50
65
  # @return [Sidekiq::Worker]
51
- def worker_class_constantize(klazz)
52
- return klazz.class if klazz.is_a?(Sidekiq::Worker) # sidekiq v6.x
53
- return klazz unless klazz.is_a?(String)
66
+ def job_class_constantize(klazz = @job_class)
67
+ SidekiqUniqueJobs.safe_constantize(klazz)
68
+ end
54
69
 
55
- Object.const_get(klazz)
56
- rescue NameError => ex
57
- case ex.message
58
- when /uninitialized constant/
59
- klazz
70
+ #
71
+ # Returns the default worker options from Sidekiq
72
+ #
73
+ #
74
+ # @return [Hash<Symbol, Object>]
75
+ #
76
+ def default_job_options
77
+ if Sidekiq.respond_to?(:default_job_options)
78
+ Sidekiq.default_job_options
60
79
  else
61
- raise
80
+ Sidekiq.default_worker_options
62
81
  end
63
82
  end
64
-
65
- def default_worker_options
66
- Sidekiq.default_worker_options
67
- end
68
83
  end
69
84
  end