sidekiq-unique-jobs 6.0.23 → 7.1.12

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 (127) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +922 -41
  3. data/README.md +821 -284
  4. data/lib/sidekiq_unique_jobs/batch_delete.rb +123 -0
  5. data/lib/sidekiq_unique_jobs/changelog.rb +78 -0
  6. data/lib/sidekiq_unique_jobs/cli.rb +34 -31
  7. data/lib/sidekiq_unique_jobs/config.rb +314 -0
  8. data/lib/sidekiq_unique_jobs/connection.rb +6 -5
  9. data/lib/sidekiq_unique_jobs/constants.rb +45 -24
  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/job.rb +41 -12
  15. data/lib/sidekiq_unique_jobs/json.rb +47 -0
  16. data/lib/sidekiq_unique_jobs/key.rb +93 -0
  17. data/lib/sidekiq_unique_jobs/lock/base_lock.rb +111 -82
  18. data/lib/sidekiq_unique_jobs/lock/client_validator.rb +28 -0
  19. data/lib/sidekiq_unique_jobs/lock/server_validator.rb +27 -0
  20. data/lib/sidekiq_unique_jobs/lock/until_and_while_executing.rb +40 -15
  21. data/lib/sidekiq_unique_jobs/lock/until_executed.rb +25 -7
  22. data/lib/sidekiq_unique_jobs/lock/until_executing.rb +22 -2
  23. data/lib/sidekiq_unique_jobs/lock/until_expired.rb +26 -16
  24. data/lib/sidekiq_unique_jobs/lock/validator.rb +96 -0
  25. data/lib/sidekiq_unique_jobs/lock/while_executing.rb +23 -12
  26. data/lib/sidekiq_unique_jobs/lock/while_executing_reject.rb +3 -3
  27. data/lib/sidekiq_unique_jobs/lock.rb +325 -0
  28. data/lib/sidekiq_unique_jobs/lock_args.rb +123 -0
  29. data/lib/sidekiq_unique_jobs/lock_config.rb +126 -0
  30. data/lib/sidekiq_unique_jobs/lock_digest.rb +79 -0
  31. data/lib/sidekiq_unique_jobs/lock_info.rb +68 -0
  32. data/lib/sidekiq_unique_jobs/lock_timeout.rb +62 -0
  33. data/lib/sidekiq_unique_jobs/lock_ttl.rb +77 -0
  34. data/lib/sidekiq_unique_jobs/locksmith.rb +275 -102
  35. data/lib/sidekiq_unique_jobs/logging/middleware_context.rb +44 -0
  36. data/lib/sidekiq_unique_jobs/logging.rb +188 -33
  37. data/lib/sidekiq_unique_jobs/lua/delete.lua +51 -0
  38. data/lib/sidekiq_unique_jobs/lua/delete_by_digest.lua +42 -0
  39. data/lib/sidekiq_unique_jobs/lua/delete_job_by_digest.lua +38 -0
  40. data/lib/sidekiq_unique_jobs/lua/find_digest_in_queues.lua +26 -0
  41. data/lib/sidekiq_unique_jobs/lua/lock.lua +93 -0
  42. data/lib/sidekiq_unique_jobs/lua/locked.lua +35 -0
  43. data/lib/sidekiq_unique_jobs/lua/queue.lua +87 -0
  44. data/lib/sidekiq_unique_jobs/lua/reap_orphans.lua +94 -0
  45. data/lib/sidekiq_unique_jobs/lua/shared/_common.lua +40 -0
  46. data/lib/sidekiq_unique_jobs/lua/shared/_current_time.lua +8 -0
  47. data/lib/sidekiq_unique_jobs/lua/shared/_delete_from_queue.lua +22 -0
  48. data/lib/sidekiq_unique_jobs/lua/shared/_delete_from_sorted_set.lua +18 -0
  49. data/lib/sidekiq_unique_jobs/lua/shared/_find_digest_in_process_set.lua +53 -0
  50. data/lib/sidekiq_unique_jobs/lua/shared/_find_digest_in_queues.lua +43 -0
  51. data/lib/sidekiq_unique_jobs/lua/shared/_find_digest_in_sorted_set.lua +24 -0
  52. data/lib/sidekiq_unique_jobs/lua/shared/_hgetall.lua +13 -0
  53. data/lib/sidekiq_unique_jobs/lua/shared/_upgrades.lua +3 -0
  54. data/lib/sidekiq_unique_jobs/lua/unlock.lua +102 -0
  55. data/lib/sidekiq_unique_jobs/lua/update_version.lua +40 -0
  56. data/lib/sidekiq_unique_jobs/lua/upgrade.lua +68 -0
  57. data/lib/sidekiq_unique_jobs/middleware/client.rb +40 -0
  58. data/lib/sidekiq_unique_jobs/middleware/server.rb +29 -0
  59. data/lib/sidekiq_unique_jobs/middleware.rb +29 -31
  60. data/lib/sidekiq_unique_jobs/normalizer.rb +4 -4
  61. data/lib/sidekiq_unique_jobs/on_conflict/log.rb +9 -5
  62. data/lib/sidekiq_unique_jobs/on_conflict/null_strategy.rb +1 -1
  63. data/lib/sidekiq_unique_jobs/on_conflict/raise.rb +1 -1
  64. data/lib/sidekiq_unique_jobs/on_conflict/reject.rb +61 -15
  65. data/lib/sidekiq_unique_jobs/on_conflict/replace.rb +54 -14
  66. data/lib/sidekiq_unique_jobs/on_conflict/reschedule.rb +16 -5
  67. data/lib/sidekiq_unique_jobs/on_conflict/strategy.rb +25 -6
  68. data/lib/sidekiq_unique_jobs/on_conflict.rb +23 -10
  69. data/lib/sidekiq_unique_jobs/options_with_fallback.rb +35 -32
  70. data/lib/sidekiq_unique_jobs/orphans/lua_reaper.rb +29 -0
  71. data/lib/sidekiq_unique_jobs/orphans/manager.rb +248 -0
  72. data/lib/sidekiq_unique_jobs/orphans/null_reaper.rb +24 -0
  73. data/lib/sidekiq_unique_jobs/orphans/observer.rb +42 -0
  74. data/lib/sidekiq_unique_jobs/orphans/reaper.rb +114 -0
  75. data/lib/sidekiq_unique_jobs/orphans/reaper_resurrector.rb +170 -0
  76. data/lib/sidekiq_unique_jobs/orphans/ruby_reaper.rb +231 -0
  77. data/lib/sidekiq_unique_jobs/redis/entity.rb +112 -0
  78. data/lib/sidekiq_unique_jobs/redis/hash.rb +56 -0
  79. data/lib/sidekiq_unique_jobs/redis/list.rb +32 -0
  80. data/lib/sidekiq_unique_jobs/redis/set.rb +32 -0
  81. data/lib/sidekiq_unique_jobs/redis/sorted_set.rb +86 -0
  82. data/lib/sidekiq_unique_jobs/redis/string.rb +49 -0
  83. data/lib/sidekiq_unique_jobs/redis.rb +11 -0
  84. data/lib/sidekiq_unique_jobs/reflectable.rb +26 -0
  85. data/lib/sidekiq_unique_jobs/reflections.rb +79 -0
  86. data/lib/sidekiq_unique_jobs/rspec/matchers/have_valid_sidekiq_options.rb +51 -0
  87. data/lib/sidekiq_unique_jobs/rspec/matchers.rb +26 -0
  88. data/lib/sidekiq_unique_jobs/script/caller.rb +127 -0
  89. data/lib/sidekiq_unique_jobs/script.rb +15 -0
  90. data/lib/sidekiq_unique_jobs/server.rb +61 -0
  91. data/lib/sidekiq_unique_jobs/sidekiq_unique_ext.rb +114 -65
  92. data/lib/sidekiq_unique_jobs/sidekiq_unique_jobs.rb +241 -35
  93. data/lib/sidekiq_unique_jobs/sidekiq_worker_methods.rb +18 -16
  94. data/lib/sidekiq_unique_jobs/testing.rb +62 -21
  95. data/lib/sidekiq_unique_jobs/timer_task.rb +78 -0
  96. data/lib/sidekiq_unique_jobs/timing.rb +58 -0
  97. data/lib/sidekiq_unique_jobs/unlockable.rb +20 -4
  98. data/lib/sidekiq_unique_jobs/update_version.rb +25 -0
  99. data/lib/sidekiq_unique_jobs/upgrade_locks.rb +155 -0
  100. data/lib/sidekiq_unique_jobs/version.rb +3 -1
  101. data/lib/sidekiq_unique_jobs/version_check.rb +23 -4
  102. data/lib/sidekiq_unique_jobs/web/helpers.rb +128 -13
  103. data/lib/sidekiq_unique_jobs/web/views/_paging.erb +4 -4
  104. data/lib/sidekiq_unique_jobs/web/views/changelogs.erb +54 -0
  105. data/lib/sidekiq_unique_jobs/web/views/lock.erb +108 -0
  106. data/lib/sidekiq_unique_jobs/web/views/locks.erb +54 -0
  107. data/lib/sidekiq_unique_jobs/web.rb +57 -27
  108. data/lib/sidekiq_unique_jobs.rb +52 -7
  109. data/lib/tasks/changelog.rake +15 -15
  110. metadata +124 -184
  111. data/lib/sidekiq_unique_jobs/client/middleware.rb +0 -56
  112. data/lib/sidekiq_unique_jobs/scripts.rb +0 -118
  113. data/lib/sidekiq_unique_jobs/server/middleware.rb +0 -46
  114. data/lib/sidekiq_unique_jobs/timeout/calculator.rb +0 -63
  115. data/lib/sidekiq_unique_jobs/timeout.rb +0 -8
  116. data/lib/sidekiq_unique_jobs/unique_args.rb +0 -150
  117. data/lib/sidekiq_unique_jobs/util.rb +0 -103
  118. data/lib/sidekiq_unique_jobs/web/views/unique_digest.erb +0 -28
  119. data/lib/sidekiq_unique_jobs/web/views/unique_digests.erb +0 -46
  120. data/redis/acquire_lock.lua +0 -21
  121. data/redis/convert_legacy_lock.lua +0 -13
  122. data/redis/delete.lua +0 -14
  123. data/redis/delete_by_digest.lua +0 -23
  124. data/redis/delete_job_by_digest.lua +0 -60
  125. data/redis/lock.lua +0 -62
  126. data/redis/release_stale_locks.lua +0 -90
  127. data/redis/unlock.lua +0 -35
@@ -3,73 +3,172 @@
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!
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
28
31
  end
29
32
 
33
+ #
34
+ # The current locks
35
+ #
36
+ #
37
+ # @return [Hash<Symbol, SidekiqUniqueJobs::BaseLock>] the configured locks
38
+ #
39
+ def locks
40
+ config.locks
41
+ end
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
+ # Temporarily use another configuration and reset to the old config after yielding
76
+ #
77
+ # @param [Hash] tmp_config the temporary configuration to use
78
+ #
79
+ # @return [void]
80
+ #
56
81
  # @yield control to the caller
57
- def use_config(tmp_config)
82
+ def use_config(tmp_config = {})
58
83
  raise ::ArgumentError, "#{name}.#{__method__} needs a block" unless block_given?
59
84
 
60
85
  old_config = config.to_h
86
+ reset!
61
87
  configure(tmp_config)
62
88
  yield
63
- configure(old_config)
89
+ ensure
90
+ reset!
91
+ configure(old_config.to_h)
92
+ end
93
+
94
+ #
95
+ # Resets configuration to deafult
96
+ #
97
+ #
98
+ # @return [SidekiqUniqueJobs::Config] a default gem configuration
99
+ #
100
+ def reset!
101
+ @config = SidekiqUniqueJobs::Config.default
102
+ end
103
+
104
+ #
105
+ # Enable SidekiqUniuqeJobs either temporarily in a block or for good
106
+ #
107
+ #
108
+ # @return [true] when not given a block
109
+ # @return [true, false] the previous value of enable when given a block
110
+ #
111
+ # @yieldreturn [void] temporarily enable sidekiq unique jobs while executing a block of code
112
+ def enable!(&block)
113
+ toggle(true, &block)
114
+ end
115
+
116
+ #
117
+ # Disable SidekiqUniuqeJobs either temporarily in a block or for good
118
+ #
119
+ #
120
+ # @return [false] when not given a block
121
+ # @return [true, false] the previous value of enable when given a block
122
+ #
123
+ # @yieldreturn [void] temporarily disable sidekiq unique jobs while executing a block of code
124
+ def disable!(&block)
125
+ toggle(false, &block)
126
+ end
127
+
128
+ #
129
+ # Checks if the gem has been disabled
130
+ #
131
+ # @return [true] when config.enabled is true
132
+ # @return [false] when config.enabled is false
133
+ #
134
+ def enabled?
135
+ config.enabled
136
+ end
137
+
138
+ #
139
+ # Checks if the gem has been disabled
140
+ #
141
+ # @return [true] when config.enabled is false
142
+ # @return [false] when config.enabled is true
143
+ #
144
+ def disabled?
145
+ !enabled?
146
+ end
147
+
148
+ #
149
+ # Toggles enabled on or off
150
+ #
151
+ # @api private
152
+ # :nodoc:
153
+ def toggle(enabled)
154
+ if block_given?
155
+ enabled_was = config.enabled
156
+ config.enabled = enabled
157
+ yield
158
+ config.enabled = enabled_was
159
+ else
160
+ config.enabled = enabled
161
+ end
64
162
  end
65
163
 
66
164
  # Configure the gem
67
165
  #
68
166
  # This is usually called once at startup of an application
69
167
  # @param [Hash] options global gem options
70
- # @option options [Integer] :default_lock_timeout (default is 0)
168
+ # @option options [Integer] :lock_timeout (default is 0)
169
+ # @option options [Integer] :lock_ttl (default is 0)
71
170
  # @option options [true,false] :enabled (default is true)
72
- # @option options [String] :unique_prefix (default is 'uniquejobs')
171
+ # @option options [String] :lock_prefix (default is 'uniquejobs')
73
172
  # @option options [Logger] :logger (default is Sidekiq.logger)
74
173
  # @yield control to the caller when given block
75
174
  def configure(options = {})
@@ -82,7 +181,114 @@ module SidekiqUniqueJobs
82
181
  end
83
182
  end
84
183
 
85
- def redis_version
86
- @redis_version ||= redis { |conn| conn.info("server")["redis_version"] }
184
+ #
185
+ # Returns the current redis version
186
+ #
187
+ #
188
+ # @return [String] a string like `5.0.2`
189
+ #
190
+ def fetch_redis_version
191
+ Sidekiq.redis_info["redis_version"]
192
+ end
193
+
194
+ #
195
+ # Current time as float
196
+ #
197
+ #
198
+ # @return [Float]
199
+ #
200
+ def now_f
201
+ now.to_f
202
+ end
203
+
204
+ #
205
+ # Current time
206
+ #
207
+ #
208
+ # @return [Time]
209
+ #
210
+ def now
211
+ Time.now
212
+ end
213
+
214
+ #
215
+ # Checks that the worker is valid with the given options
216
+ #
217
+ # @param [Hash] options the `sidekiq_options` to validate
218
+ #
219
+ # @return [Boolean]
220
+ #
221
+ def validate_worker(options)
222
+ raise NotUniqueWorker, options unless (lock_type = options[LOCK])
223
+
224
+ lock_class = locks[lock_type]
225
+ lock_class.validate_options(options)
226
+ end
227
+
228
+ #
229
+ # Checks that the worker is valid with the given options
230
+ #
231
+ # @param [Hash] options the `sidekiq_options` to validate
232
+ #
233
+ # @raise [InvalidWorker] when {#validate_worker} returns false or nil
234
+ #
235
+ def validate_worker!(options)
236
+ lock_config = validate_worker(options)
237
+ raise InvalidWorker, lock_config unless lock_config.errors.empty?
238
+ end
239
+
240
+ # Attempt to constantize a string worker_class argument, always
241
+ # failing back to the original argument when the constant can't be found
242
+ #
243
+ # @return [Sidekiq::Worker]
244
+ def constantize(str)
245
+ return str.class if str.is_a?(Sidekiq::Worker) # sidekiq v6.x
246
+ return str unless str.is_a?(String)
247
+ return Object.const_get(str) unless str.include?("::")
248
+
249
+ names = str.split("::")
250
+ names.shift if names.empty? || names.first.empty?
251
+
252
+ names.inject(Object) do |constant, name|
253
+ # the false flag limits search for name to under the constant namespace
254
+ # which mimics Rails' behaviour
255
+ constant.const_get(name, false)
256
+ end
257
+ end
258
+
259
+ # Attempt to constantize a string worker_class argument, always
260
+ # failing back to the original argument when the constant can't be found
261
+ #
262
+ # @return [Sidekiq::Worker, String]
263
+ def safe_constantize(str)
264
+ constantize(str)
265
+ rescue NameError => ex
266
+ case ex.message
267
+ when /uninitialized constant/
268
+ str
269
+ else
270
+ raise
271
+ end
272
+ end
273
+
274
+ #
275
+ # Collection with notifications
276
+ #
277
+ #
278
+ # @return [Reflections]
279
+ #
280
+ def reflections
281
+ @reflections ||= Reflections.new
282
+ end
283
+
284
+ #
285
+ # Yields notification stack for sidekiq unique jobs to configure notifications
286
+ #
287
+ #
288
+ # @return [void] <description>
289
+ #
290
+ # @yieldparam [Reflections] x used to configure notifications
291
+ def reflect
292
+ yield reflections if block_given?
87
293
  end
88
294
  end
@@ -3,7 +3,7 @@
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
8
  # Avoids duplicating worker_class.respond_to? in multiple places
9
9
  # @return [true, false]
@@ -16,7 +16,7 @@ module SidekiqUniqueJobs
16
16
  def worker_options
17
17
  return {} unless sidekiq_worker_class?
18
18
 
19
- worker_class.get_sidekiq_options.stringify_keys
19
+ worker_class.get_sidekiq_options.deep_stringify_keys
20
20
  end
21
21
 
22
22
  # Tests that the
@@ -29,33 +29,35 @@ module SidekiqUniqueJobs
29
29
  # The Sidekiq::Worker implementation
30
30
  # @return [Sidekiq::Worker]
31
31
  def worker_class
32
- @_worker_class ||= worker_class_constantize(@worker_class) # rubocop:disable Naming/MemoizedInstanceVariableName
32
+ @_worker_class ||= worker_class_constantize # rubocop:disable Naming/MemoizedInstanceVariableName
33
33
  end
34
34
 
35
35
  # The hook to call after a successful unlock
36
36
  # @return [Proc]
37
37
  def after_unlock_hook
38
- -> { worker_class.after_unlock if worker_method_defined?(:after_unlock) }
38
+ 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
43
+ end
44
+ end
39
45
  end
40
46
 
41
47
  # Attempt to constantize a string worker_class argument, always
42
48
  # failing back to the original argument when the constant can't be found
43
49
  #
44
50
  # @return [Sidekiq::Worker]
45
- def worker_class_constantize(klazz)
46
- return klazz.class if klazz.is_a?(Sidekiq::Worker) # sidekiq v6.x
47
- return klazz unless klazz.is_a?(String)
48
-
49
- Object.const_get(klazz)
50
- rescue NameError => ex
51
- case ex.message
52
- when /uninitialized constant/
53
- klazz
54
- else
55
- raise
56
- end
51
+ def worker_class_constantize(klazz = @worker_class)
52
+ SidekiqUniqueJobs.safe_constantize(klazz)
57
53
  end
58
54
 
55
+ #
56
+ # Returns the default worker options from Sidekiq
57
+ #
58
+ #
59
+ # @return [Hash<Symbol, Object>]
60
+ #
59
61
  def default_worker_options
60
62
  Sidekiq.default_worker_options
61
63
  end
@@ -1,36 +1,69 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  # :nocov:
4
+ # :nodoc:
4
5
 
5
6
  require "sidekiq"
6
7
  require "sidekiq/testing"
8
+ require "sidekiq_unique_jobs/rspec/matchers"
9
+ require "sidekiq_unique_jobs/lock/validator"
10
+ require "sidekiq_unique_jobs/lock/client_validator"
11
+ require "sidekiq_unique_jobs/lock/server_validator"
7
12
 
13
+ #
14
+ # See Sidekiq gem for more details
15
+ #
8
16
  module Sidekiq
17
+ #
18
+ # Temporarily turn Sidekiq's options into something different
19
+ #
20
+ # @note this method will restore the original options after yielding
21
+ #
22
+ # @param [Hash<Symbol, Object>] tmp_config the temporary config to use
23
+ #
9
24
  def self.use_options(tmp_config = {})
10
- old_config = default_worker_options
25
+ old_options = default_worker_options.dup
26
+
11
27
  default_worker_options.clear
12
28
  self.default_worker_options = tmp_config
13
-
14
29
  yield
15
30
  ensure
16
31
  default_worker_options.clear
17
- self.default_worker_options = old_config
32
+ self.default_worker_options = DEFAULT_WORKER_OPTIONS
33
+ self.default_worker_options = old_options
18
34
  end
19
35
 
36
+ #
37
+ # See Sidekiq::Worker in Sidekiq gem for more details
38
+ #
20
39
  module Worker
40
+ #
41
+ # Adds class methods to Sidekiq::Worker
42
+ #
21
43
  module ClassMethods
44
+ #
45
+ # Temporarily turn a workers sidekiq_options into something different
46
+ #
47
+ # @note this method will restore the original configuration after yielding
48
+ #
49
+ # @param [Hash<Symbol, Object>] tmp_config the temporary config to use
50
+ #
22
51
  def use_options(tmp_config = {})
23
- old_config = get_sidekiq_options
24
- sidekiq_options(tmp_config)
52
+ old_options = sidekiq_options_hash.dup
53
+ sidekiq_options(old_options.merge(tmp_config))
25
54
 
26
55
  yield
27
56
  ensure
28
- sidekiq_options(old_config)
57
+ self.sidekiq_options_hash = Sidekiq::DEFAULT_WORKER_OPTIONS
58
+ sidekiq_options(old_options)
29
59
  end
30
60
 
61
+ #
62
+ # Clears the jobs for this worker and removes all locks
63
+ #
31
64
  def clear
32
65
  jobs.each do |job|
33
- SidekiqUniqueJobs::Unlockable.delete(job)
66
+ SidekiqUniqueJobs::Unlockable.unlock(job)
34
67
  end
35
68
 
36
69
  Sidekiq::Queues[queue].clear
@@ -38,25 +71,33 @@ module Sidekiq
38
71
  end
39
72
  end
40
73
 
74
+ #
75
+ # Prepends deletion of locks to clear_all
76
+ #
41
77
  module Overrides
42
- def self.included(base)
43
- base.extend Testing
44
- base.class_eval do
45
- class << self
46
- alias_method :clear_all_orig, :clear_all
47
- alias_method :clear_all, :clear_all_ext
48
- end
49
- end
78
+ #
79
+ # Overrides sidekiq_options on the worker class to prepend validation
80
+ #
81
+ # @param [Hash] options worker options
82
+ #
83
+ # @return [void]
84
+ #
85
+ def sidekiq_options(options = {})
86
+ SidekiqUniqueJobs.validate_worker!(options) if SidekiqUniqueJobs.config.raise_on_config_error
87
+
88
+ super(options)
50
89
  end
51
90
 
52
- module Testing
53
- def clear_all_ext
54
- clear_all_orig
55
- SidekiqUniqueJobs::Util.del("*", 1000)
56
- end
91
+ #
92
+ # Clears all jobs for this worker and removes all locks
93
+ #
94
+ def clear_all
95
+ super
96
+
97
+ SidekiqUniqueJobs::Digests.new.delete_by_pattern("*", count: 10_000)
57
98
  end
58
99
  end
59
100
 
60
- include Overrides
101
+ prepend Overrides
61
102
  end
62
103
  end
@@ -0,0 +1,78 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SidekiqUniqueJobs
4
+ # @see [Concurrent::TimerTask] https://www.rubydoc.info/gems/concurrent-ruby/Concurrent/TimerTask
5
+ #
6
+ class TimerTask < ::Concurrent::TimerTask
7
+ private
8
+
9
+ def ns_initialize(opts, &task)
10
+ set_deref_options(opts)
11
+
12
+ self.execution_interval = opts[:execution] || opts[:execution_interval] || EXECUTION_INTERVAL
13
+ self.timeout_interval = opts[:timeout] || opts[:timeout_interval] || TIMEOUT_INTERVAL
14
+ @run_now = opts[:now] || opts[:run_now]
15
+ @executor = Concurrent::RubySingleThreadExecutor.new
16
+ @running = Concurrent::AtomicBoolean.new(false)
17
+ @task = task
18
+ @value = nil
19
+
20
+ self.observers = Concurrent::Collection::CopyOnNotifyObserverSet.new
21
+ end
22
+
23
+ def schedule_next_task(interval = execution_interval)
24
+ exec_task = ->(completion) { execute_task(completion) }
25
+ Concurrent::ScheduledTask.execute(interval, args: [Concurrent::Event.new], &exec_task)
26
+ nil
27
+ end
28
+
29
+ # @!visibility private
30
+ def execute_task(completion) # rubocop:disable Metrics/MethodLength
31
+ return nil unless @running.true?
32
+
33
+ timeout_task = -> { timeout_task(completion) }
34
+
35
+ Concurrent::ScheduledTask.execute(
36
+ timeout_interval,
37
+ args: [completion],
38
+ &timeout_task
39
+ )
40
+ @thread_completed = Concurrent::Event.new
41
+
42
+ @value = @reason = nil
43
+ @executor.post do
44
+ @value = @task.call(self)
45
+ rescue Exception => ex # rubocop:disable Lint/RescueException
46
+ @reason = ex
47
+ ensure
48
+ @thread_completed.set
49
+ end
50
+
51
+ @thread_completed.wait
52
+
53
+ if completion.try?
54
+ schedule_next_task
55
+ time = Time.now
56
+ observers.notify_observers do
57
+ [time, value, @reason]
58
+ end
59
+ end
60
+ nil
61
+ end
62
+
63
+ # @!visibility private
64
+ def timeout_task(completion)
65
+ return unless @running.true?
66
+ return unless completion.try?
67
+
68
+ @executor.kill
69
+ @executor.wait_for_termination
70
+ @executor = Concurrent::RubySingleThreadExecutor.new
71
+
72
+ @thread_completed.set
73
+
74
+ schedule_next_task
75
+ observers.notify_observers(Time.now, nil, Concurrent::TimeoutError.new)
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,58 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SidekiqUniqueJobs
4
+ # Handles timing of things
5
+ #
6
+ # @author Mikael Henriksson <mikael@mhenrixon.com>
7
+ module Timing
8
+ module_function
9
+
10
+ #
11
+ # Used for timing method calls
12
+ #
13
+ #
14
+ # @return [yield return, Float]
15
+ #
16
+ def timed
17
+ start_time = time_source.call
18
+
19
+ [yield, time_source.call - start_time]
20
+ end
21
+
22
+ #
23
+ # Used to get a current representation of time as Integer
24
+ #
25
+ #
26
+ # @return [Integer]
27
+ #
28
+ def time_source
29
+ -> { (clock_stamp * 1000).to_i }
30
+ end
31
+
32
+ #
33
+ # Returns the current time as float
34
+ #
35
+ # @see SidekiqUniqueJobs.now_f
36
+ #
37
+ # @return [Float]
38
+ #
39
+ def now_f
40
+ SidekiqUniqueJobs.now_f
41
+ end
42
+
43
+ #
44
+ # Returns a float representation of the current time.
45
+ # Either from Process or Time
46
+ #
47
+ #
48
+ # @return [Float]
49
+ #
50
+ def clock_stamp
51
+ if Process.const_defined?("CLOCK_MONOTONIC")
52
+ Process.clock_gettime(Process::CLOCK_MONOTONIC)
53
+ else
54
+ now_f
55
+ end
56
+ end
57
+ end
58
+ end
@@ -4,23 +4,39 @@ module SidekiqUniqueJobs
4
4
  # Utility module to help manage unique keys in redis.
5
5
  # Useful for deleting keys that for whatever reason wasn't deleted
6
6
  #
7
- # @author Mikael Henriksson <mikael@zoolutions.se>
7
+ # @author Mikael Henriksson <mikael@mhenrixon.com>
8
8
  module Unlockable
9
9
  module_function
10
10
 
11
11
  # Unlocks a job.
12
12
  # @param [Hash] item a Sidekiq job hash
13
13
  def unlock(item)
14
- SidekiqUniqueJobs::UniqueArgs.digest(item)
14
+ SidekiqUniqueJobs::Job.add_digest(item)
15
15
  SidekiqUniqueJobs::Locksmith.new(item).unlock
16
16
  end
17
17
 
18
- # Deletes a lock regardless of if it was locked or not.
18
+ # Unlocks a job.
19
+ # @param [Hash] item a Sidekiq job hash
20
+ def unlock!(item)
21
+ SidekiqUniqueJobs::Job.add_digest(item)
22
+ SidekiqUniqueJobs::Locksmith.new(item).unlock!
23
+ end
24
+
25
+ # Deletes a lock unless it has ttl
19
26
  #
20
27
  # This is good for situations when a job is locked by another item
21
28
  # @param [Hash] item a Sidekiq job hash
22
29
  def delete(item)
23
- SidekiqUniqueJobs::UniqueArgs.digest(item)
30
+ SidekiqUniqueJobs::Job.add_digest(item)
31
+ SidekiqUniqueJobs::Locksmith.new(item).delete
32
+ end
33
+
34
+ # Deletes a lock regardless of if it was locked or has ttl.
35
+ #
36
+ # This is good for situations when a job is locked by another item
37
+ # @param [Hash] item a Sidekiq job hash
38
+ def delete!(item)
39
+ SidekiqUniqueJobs::Job.add_digest(item)
24
40
  SidekiqUniqueJobs::Locksmith.new(item).delete!
25
41
  end
26
42
  end