sidekiq-unique-jobs 7.1.5 → 7.1.30

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 (66) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +321 -4
  3. data/README.md +20 -16
  4. data/lib/sidekiq_unique_jobs/batch_delete.rb +8 -7
  5. data/lib/sidekiq_unique_jobs/changelog.rb +3 -3
  6. data/lib/sidekiq_unique_jobs/cli.rb +33 -8
  7. data/lib/sidekiq_unique_jobs/config.rb +48 -4
  8. data/lib/sidekiq_unique_jobs/constants.rb +45 -44
  9. data/lib/sidekiq_unique_jobs/core_ext.rb +1 -1
  10. data/lib/sidekiq_unique_jobs/deprecation.rb +30 -0
  11. data/lib/sidekiq_unique_jobs/digests.rb +8 -11
  12. data/lib/sidekiq_unique_jobs/exceptions.rb +4 -3
  13. data/lib/sidekiq_unique_jobs/expiring_digests.rb +14 -0
  14. data/lib/sidekiq_unique_jobs/job.rb +5 -0
  15. data/lib/sidekiq_unique_jobs/json.rb +7 -0
  16. data/lib/sidekiq_unique_jobs/key.rb +13 -8
  17. data/lib/sidekiq_unique_jobs/lock/base_lock.rb +30 -22
  18. data/lib/sidekiq_unique_jobs/lock/until_and_while_executing.rb +9 -3
  19. data/lib/sidekiq_unique_jobs/lock/until_executed.rb +15 -4
  20. data/lib/sidekiq_unique_jobs/lock/until_executing.rb +14 -4
  21. data/lib/sidekiq_unique_jobs/lock/until_expired.rb +13 -5
  22. data/lib/sidekiq_unique_jobs/lock/while_executing.rb +10 -6
  23. data/lib/sidekiq_unique_jobs/lock.rb +27 -10
  24. data/lib/sidekiq_unique_jobs/lock_args.rb +19 -15
  25. data/lib/sidekiq_unique_jobs/lock_config.rb +6 -6
  26. data/lib/sidekiq_unique_jobs/lock_digest.rb +7 -7
  27. data/lib/sidekiq_unique_jobs/lock_info.rb +2 -2
  28. data/lib/sidekiq_unique_jobs/lock_timeout.rb +4 -4
  29. data/lib/sidekiq_unique_jobs/lock_ttl.rb +5 -5
  30. data/lib/sidekiq_unique_jobs/lock_type.rb +37 -0
  31. data/lib/sidekiq_unique_jobs/locksmith.rb +41 -10
  32. data/lib/sidekiq_unique_jobs/logging.rb +23 -0
  33. data/lib/sidekiq_unique_jobs/lua/lock.lua +18 -12
  34. data/lib/sidekiq_unique_jobs/lua/lock_until_expired.lua +92 -0
  35. data/lib/sidekiq_unique_jobs/lua/reap_orphans.lua +31 -3
  36. data/lib/sidekiq_unique_jobs/lua/shared/_common.lua +1 -1
  37. data/lib/sidekiq_unique_jobs/lua/shared/_find_digest_in_process_set.lua +1 -1
  38. data/lib/sidekiq_unique_jobs/lua/unlock.lua +17 -5
  39. data/lib/sidekiq_unique_jobs/middleware/client.rb +3 -1
  40. data/lib/sidekiq_unique_jobs/middleware/server.rb +2 -0
  41. data/lib/sidekiq_unique_jobs/middleware.rb +4 -4
  42. data/lib/sidekiq_unique_jobs/on_conflict/reject.rb +4 -4
  43. data/lib/sidekiq_unique_jobs/on_conflict/reschedule.rb +3 -3
  44. data/lib/sidekiq_unique_jobs/options_with_fallback.rb +6 -8
  45. data/lib/sidekiq_unique_jobs/orphans/lua_reaper.rb +1 -1
  46. data/lib/sidekiq_unique_jobs/orphans/manager.rb +40 -12
  47. data/lib/sidekiq_unique_jobs/orphans/ruby_reaper.rb +99 -14
  48. data/lib/sidekiq_unique_jobs/redis/string.rb +3 -1
  49. data/lib/sidekiq_unique_jobs/reflectable.rb +11 -2
  50. data/lib/sidekiq_unique_jobs/reflections.rb +12 -1
  51. data/lib/sidekiq_unique_jobs/script/caller.rb +7 -7
  52. data/lib/sidekiq_unique_jobs/server.rb +13 -1
  53. data/lib/sidekiq_unique_jobs/sidekiq_unique_ext.rb +35 -13
  54. data/lib/sidekiq_unique_jobs/sidekiq_unique_jobs.rb +13 -3
  55. data/lib/sidekiq_unique_jobs/sidekiq_worker_methods.rb +40 -21
  56. data/lib/sidekiq_unique_jobs/testing.rb +53 -21
  57. data/lib/sidekiq_unique_jobs/timer_task.rb +266 -45
  58. data/lib/sidekiq_unique_jobs/timing.rb +1 -1
  59. data/lib/sidekiq_unique_jobs/upgrade_locks.rb +6 -6
  60. data/lib/sidekiq_unique_jobs/version.rb +1 -1
  61. data/lib/sidekiq_unique_jobs/web/helpers.rb +11 -1
  62. data/lib/sidekiq_unique_jobs/web/views/lock.erb +5 -3
  63. data/lib/sidekiq_unique_jobs/web.rb +22 -3
  64. data/lib/sidekiq_unique_jobs.rb +2 -0
  65. data/lib/tasks/changelog.rake +16 -16
  66. metadata +26 -13
@@ -68,24 +68,46 @@ module Sidekiq
68
68
  prepend UniqueExtension
69
69
  end
70
70
 
71
- # See Sidekiq::Api
72
- class Job
73
- #
74
- # Provides extensions for unlocking jobs that are removed and deleted
75
- #
76
- # @author Mikael Henriksson <mikael@mhenrixon.com>
77
- #
78
- module UniqueExtension
71
+ if Sidekiq.const_defined?(:JobRecord)
72
+ # See Sidekiq::Api
73
+ class JobRecord
79
74
  #
80
- # Wraps the original method to ensure locks for the job are deleted
75
+ # Provides extensions for unlocking jobs that are removed and deleted
81
76
  #
82
- def delete
83
- SidekiqUniqueJobs::Unlockable.delete!(item)
84
- super
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
86
+ end
85
87
  end
88
+
89
+ prepend UniqueExtension
86
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
87
108
 
88
- prepend UniqueExtension
109
+ prepend UniqueExtension
110
+ end
89
111
  end
90
112
 
91
113
  # See Sidekiq::Api
@@ -17,7 +17,7 @@ module SidekiqUniqueJobs # rubocop:disable Metrics/ModuleLength
17
17
  # @return [SidekiqUniqueJobs::Config] the gem configuration
18
18
  #
19
19
  def config
20
- @config ||= reset!
20
+ @config ||= reset! # rubocop:disable ThreadSafety/InstanceVariableInClassMethod
21
21
  end
22
22
 
23
23
  #
@@ -71,6 +71,16 @@ module SidekiqUniqueJobs # rubocop:disable Metrics/ModuleLength
71
71
  config.logger = other
72
72
  end
73
73
 
74
+ #
75
+ # Check if logging is enabled
76
+ #
77
+ #
78
+ # @return [true, false]
79
+ #
80
+ def logging?
81
+ config.logger_enabled
82
+ end
83
+
74
84
  #
75
85
  # Temporarily use another configuration and reset to the old config after yielding
76
86
  #
@@ -98,7 +108,7 @@ module SidekiqUniqueJobs # rubocop:disable Metrics/ModuleLength
98
108
  # @return [SidekiqUniqueJobs::Config] a default gem configuration
99
109
  #
100
110
  def reset!
101
- @config = SidekiqUniqueJobs::Config.default
111
+ @config = SidekiqUniqueJobs::Config.default # rubocop:disable ThreadSafety/InstanceVariableInClassMethod
102
112
  end
103
113
 
104
114
  #
@@ -278,7 +288,7 @@ module SidekiqUniqueJobs # rubocop:disable Metrics/ModuleLength
278
288
  # @return [Reflections]
279
289
  #
280
290
  def reflections
281
- @reflections ||= Reflections.new
291
+ @reflections ||= Reflections.new # rubocop:disable ThreadSafety/InstanceVariableInClassMethod
282
292
  end
283
293
 
284
294
  #
@@ -5,41 +5,56 @@ module SidekiqUniqueJobs
5
5
  #
6
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.deep_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 # 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,7 +63,7 @@ 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 = @worker_class)
66
+ def job_class_constantize(klazz = @job_class)
52
67
  SidekiqUniqueJobs.safe_constantize(klazz)
53
68
  end
54
69
 
@@ -58,8 +73,12 @@ module SidekiqUniqueJobs
58
73
  #
59
74
  # @return [Hash<Symbol, Object>]
60
75
  #
61
- def default_worker_options
62
- Sidekiq.default_worker_options
76
+ def default_job_options
77
+ if Sidekiq.respond_to?(:default_job_options)
78
+ Sidekiq.default_job_options
79
+ else
80
+ Sidekiq.default_worker_options
81
+ end
63
82
  end
64
83
  end
65
84
  end
@@ -21,16 +21,24 @@ module Sidekiq
21
21
  #
22
22
  # @param [Hash<Symbol, Object>] tmp_config the temporary config to use
23
23
  #
24
- def self.use_options(tmp_config = {})
25
- old_options = default_worker_options.dup
24
+ def self.use_options(tmp_config = {}) # rubocop:disable Metrics/MethodLength
25
+ if respond_to?(:default_job_options)
26
+ default_job_options.clear
27
+ self.default_job_options = tmp_config
28
+ else
29
+ default_worker_options.clear
30
+ self.default_worker_options = tmp_config
31
+ end
26
32
 
27
- default_worker_options.clear
28
- self.default_worker_options = tmp_config
29
33
  yield
30
34
  ensure
31
- default_worker_options.clear
32
- self.default_worker_options = DEFAULT_WORKER_OPTIONS
33
- self.default_worker_options = old_options
35
+ if respond_to?(:default_job_options)
36
+ default_job_options.clear
37
+ self.default_job_options = default_job_options
38
+ else
39
+ default_worker_options.clear
40
+ self.default_worker_options = DEFAULT_WORKER_OPTIONS
41
+ end
34
42
  end
35
43
 
36
44
  #
@@ -54,20 +62,14 @@ module Sidekiq
54
62
 
55
63
  yield
56
64
  ensure
57
- self.sidekiq_options_hash = Sidekiq::DEFAULT_WORKER_OPTIONS
58
- sidekiq_options(old_options)
59
- end
65
+ self.sidekiq_options_hash =
66
+ if Sidekiq.respond_to?(:default_job_options)
67
+ Sidekiq.default_job_options
68
+ else
69
+ DEFAULT_WORKER_OPTIONS
70
+ end
60
71
 
61
- #
62
- # Clears the jobs for this worker and removes all locks
63
- #
64
- def clear
65
- jobs.each do |job|
66
- SidekiqUniqueJobs::Unlockable.unlock(job)
67
- end
68
-
69
- Sidekiq::Queues[queue].clear
70
- jobs.clear
72
+ sidekiq_options(old_options)
71
73
  end
72
74
  end
73
75
 
@@ -88,6 +90,36 @@ module Sidekiq
88
90
  super(options)
89
91
  end
90
92
 
93
+ #
94
+ # Prepends deletion of locks to clear
95
+ #
96
+ module ClassMethods
97
+ #
98
+ # Clears the jobs for this worker and removes all locks
99
+ #
100
+ def clear
101
+ jobs.each do |job|
102
+ SidekiqUniqueJobs::Unlockable.unlock(job)
103
+ end
104
+
105
+ super
106
+ end
107
+ end
108
+ end
109
+
110
+ prepend Overrides
111
+
112
+ #
113
+ # Prepends methods to Sidekiq::Worker
114
+ #
115
+ module ClassMethods
116
+ prepend Overrides::ClassMethods
117
+ end
118
+
119
+ #
120
+ # Prepends singleton methods to Sidekiq::Worker
121
+ #
122
+ module SignletonOverrides
91
123
  #
92
124
  # Clears all jobs for this worker and removes all locks
93
125
  #
@@ -98,6 +130,6 @@ module Sidekiq
98
130
  end
99
131
  end
100
132
 
101
- prepend Overrides
133
+ singleton_class.prepend SignletonOverrides
102
134
  end
103
135
  end
@@ -1,25 +1,279 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "concurrent/collection/copy_on_notify_observer_set"
4
+ require "concurrent/concern/dereferenceable"
5
+ require "concurrent/concern/observable"
6
+ require "concurrent/atomic/atomic_boolean"
7
+ require "concurrent/executor/executor_service"
8
+ require "concurrent/executor/ruby_executor_service"
9
+ require "concurrent/executor/safe_task_executor"
10
+ require "concurrent/scheduled_task"
11
+
3
12
  module SidekiqUniqueJobs
4
- # @see [Concurrent::TimerTask] https://www.rubydoc.info/gems/concurrent-ruby/Concurrent/TimerTask
13
+ # A very common concurrency pattern is to run a thread that performs a task at
14
+ # regular intervals. The thread that performs the task sleeps for the given
15
+ # interval then wakes up and performs the task. Lather, rinse, repeat... This
16
+ # pattern causes two problems. First, it is difficult to test the business
17
+ # logic of the task because the task itself is tightly coupled with the
18
+ # concurrency logic. Second, an exception raised while performing the task can
19
+ # cause the entire thread to abend. In a long-running application where the
20
+ # task thread is intended to run for days/weeks/years a crashed task thread
21
+ # can pose a significant problem. `TimerTask` alleviates both problems.
22
+ #
23
+ # When a `TimerTask` is launched it starts a thread for monitoring the
24
+ # execution interval. The `TimerTask` thread does not perform the task,
25
+ # however. Instead, the TimerTask launches the task on a separate thread.
26
+ # Should the task experience an unrecoverable crash only the task thread will
27
+ # crash. This makes the `TimerTask` very fault tolerant. Additionally, the
28
+ # `TimerTask` thread can respond to the success or failure of the task,
29
+ # performing logging or ancillary operations.
30
+ #
31
+ # One other advantage of `TimerTask` is that it forces the business logic to
32
+ # be completely decoupled from the concurrency logic. The business logic can
33
+ # be tested separately then passed to the `TimerTask` for scheduling and
34
+ # running.
35
+ #
36
+ # In some cases it may be necessary for a `TimerTask` to affect its own
37
+ # execution cycle. To facilitate this, a reference to the TimerTask instance
38
+ # is passed as an argument to the provided block every time the task is
39
+ # executed.
40
+ #
41
+ # The `TimerTask` class includes the `Dereferenceable` mixin module so the
42
+ # result of the last execution is always available via the `#value` method.
43
+ # Dereferencing options can be passed to the `TimerTask` during construction or
44
+ # at any later time using the `#set_deref_options` method.
45
+ #
46
+ # `TimerTask` supports notification through the Ruby standard library
47
+ # {http://ruby-doc.org/stdlib-2.0/libdoc/observer/rdoc/Observable.html
48
+ # Observable} module. On execution the `TimerTask` will notify the observers
49
+ # with three arguments: time of execution, the result of the block (or nil on
50
+ # failure), and any raised exceptions (or nil on success).
51
+ #
52
+ # @!macro copy_options
53
+ #
54
+ # @example Basic usage
55
+ # task = Concurrent::TimerTask.new{ puts 'Boom!' }
56
+ # task.execute
57
+ #
58
+ # task.execution_interval #=> 60 (default)
59
+ #
60
+ # # wait 60 seconds...
61
+ # #=> 'Boom!'
62
+ #
63
+ # task.shutdown #=> true
64
+ #
65
+ # @example Configuring `:execution_interval`
66
+ # task = Concurrent::TimerTask.new(execution_interval: 5) do
67
+ # puts 'Boom!'
68
+ # end
69
+ #
70
+ # task.execution_interval #=> 5
71
+ #
72
+ # @example Immediate execution with `:run_now`
73
+ # task = Concurrent::TimerTask.new(run_now: true){ puts 'Boom!' }
74
+ # task.execute
75
+ #
76
+ # #=> 'Boom!'
77
+ #
78
+ # @example Last `#value` and `Dereferenceable` mixin
79
+ # task = Concurrent::TimerTask.new(
80
+ # dup_on_deref: true,
81
+ # execution_interval: 5
82
+ # ){ Time.now }
5
83
  #
6
- class TimerTask < ::Concurrent::TimerTask
84
+ # task.execute
85
+ # Time.now #=> 2013-11-07 18:06:50 -0500
86
+ # sleep(10)
87
+ # task.value #=> 2013-11-07 18:06:55 -0500
88
+ #
89
+ # @example Controlling execution from within the block
90
+ # timer_task = Concurrent::TimerTask.new(execution_interval: 1) do |task|
91
+ # task.execution_interval.times{ print 'Boom! ' }
92
+ # print "\n"
93
+ # task.execution_interval += 1
94
+ # if task.execution_interval > 5
95
+ # puts 'Stopping...'
96
+ # task.shutdown
97
+ # end
98
+ # end
99
+ #
100
+ # timer_task.execute # blocking call - this task will stop itself
101
+ # #=> Boom!
102
+ # #=> Boom! Boom!
103
+ # #=> Boom! Boom! Boom!
104
+ # #=> Boom! Boom! Boom! Boom!
105
+ # #=> Boom! Boom! Boom! Boom! Boom!
106
+ # #=> Stopping...
107
+ #
108
+ # @example Observation
109
+ # class TaskObserver
110
+ # def update(time, result, ex)
111
+ # if result
112
+ # print "(#{time}) Execution successfully returned #{result}\n"
113
+ # else
114
+ # print "(#{time}) Execution failed with error #{ex}\n"
115
+ # end
116
+ # end
117
+ # end
118
+ #
119
+ # task = Concurrent::TimerTask.new(execution_interval: 1){ 42 }
120
+ # task.add_observer(TaskObserver.new)
121
+ # task.execute
122
+ # sleep 4
123
+ #
124
+ # #=> (2013-10-13 19:08:58 -0400) Execution successfully returned 42
125
+ # #=> (2013-10-13 19:08:59 -0400) Execution successfully returned 42
126
+ # #=> (2013-10-13 19:09:00 -0400) Execution successfully returned 42
127
+ # task.shutdown
128
+ #
129
+ # task = Concurrent::TimerTask.new(execution_interval: 1){ sleep }
130
+ # task.add_observer(TaskObserver.new)
131
+ # task.execute
132
+ #
133
+ # #=> (2013-10-13 19:07:25 -0400) Execution timed out
134
+ # #=> (2013-10-13 19:07:27 -0400) Execution timed out
135
+ # #=> (2013-10-13 19:07:29 -0400) Execution timed out
136
+ # task.shutdown
137
+ #
138
+ # task = Concurrent::TimerTask.new(execution_interval: 1){ raise StandardError }
139
+ # task.add_observer(TaskObserver.new)
140
+ # task.execute
141
+ #
142
+ # #=> (2013-10-13 19:09:37 -0400) Execution failed with error StandardError
143
+ # #=> (2013-10-13 19:09:38 -0400) Execution failed with error StandardError
144
+ # #=> (2013-10-13 19:09:39 -0400) Execution failed with error StandardError
145
+ # task.shutdown
146
+ #
147
+ # @see http://ruby-doc.org/stdlib-2.0/libdoc/observer/rdoc/Observable.html
148
+ # @see http://docs.oracle.com/javase/7/docs/api/java/util/TimerTask.html
149
+ class TimerTask < Concurrent::RubyExecutorService
150
+ include Concurrent::Concern::Dereferenceable
151
+ include Concurrent::Concern::Observable
152
+
153
+ # Default `:execution_interval` in seconds.
154
+ EXECUTION_INTERVAL = 60
155
+
156
+ # Default `:timeout_interval` in seconds.
157
+ TIMEOUT_INTERVAL = 30
158
+
159
+ # Create a new TimerTask with the given task and configuration.
160
+ #
161
+ # @!macro timer_task_initialize
162
+ # @param [Hash] opts the options defining task execution.
163
+ # @option opts [Integer] :execution_interval number of seconds between
164
+ # task executions (default: EXECUTION_INTERVAL)
165
+ # @option opts [Boolean] :run_now Whether to run the task immediately
166
+ # upon instantiation or to wait until the first # execution_interval
167
+ # has passed (default: false)
168
+ #
169
+ # @!macro deref_options
170
+ #
171
+ # @raise ArgumentError when no block is given.
172
+ #
173
+ # @yield to the block after :execution_interval seconds have passed since
174
+ # the last yield
175
+ # @yieldparam task a reference to the `TimerTask` instance so that the
176
+ # block can control its own lifecycle. Necessary since `self` will
177
+ # refer to the execution context of the block rather than the running
178
+ # `TimerTask`.
179
+ #
180
+ # @return [TimerTask] the new `TimerTask`
181
+ def initialize(opts = {}, &task)
182
+ raise ArgumentError, "no block given" unless task
183
+
184
+ super
185
+ set_deref_options opts
186
+ end
187
+
188
+ # Is the executor running?
189
+ #
190
+ # @return [Boolean] `true` when running, `false` when shutting down or shutdown
191
+ def running?
192
+ @running.true?
193
+ end
194
+
195
+ # Execute a previously created `TimerTask`.
196
+ #
197
+ # @return [TimerTask] a reference to `self`
198
+ #
199
+ # @example Instance and execute in separate steps
200
+ # task = Concurrent::TimerTask.new(execution_interval: 10){ print "Hello World\n" }
201
+ # task.running? #=> false
202
+ # task.execute
203
+ # task.running? #=> true
204
+ #
205
+ # @example Instance and execute in one line
206
+ # task = Concurrent::TimerTask.new(execution_interval: 10){ print "Hello World\n" }.execute
207
+ # task.running? #=> true
208
+ def execute
209
+ synchronize do
210
+ if @running.false?
211
+ @running.make_true
212
+ schedule_next_task(@run_now ? 0 : @execution_interval)
213
+ end
214
+ end
215
+ self
216
+ end
217
+
218
+ # Create and execute a new `TimerTask`.
219
+ #
220
+ # @!macro timer_task_initialize
221
+ #
222
+ # @example
223
+ # task = Concurrent::TimerTask.execute(execution_interval: 10){ print "Hello World\n" }
224
+ # task.running? #=> true
225
+ def self.execute(opts = {}, &task)
226
+ TimerTask.new(opts, &task).execute
227
+ end
228
+
229
+ # @!attribute [rw] execution_interval
230
+ # @return [Fixnum] Number of seconds after the task completes before the
231
+ # task is performed again.
232
+ def execution_interval
233
+ synchronize { @execution_interval }
234
+ end
235
+
236
+ # @!attribute [rw] execution_interval
237
+ # @return [Fixnum] Number of seconds after the task completes before the
238
+ # task is performed again.
239
+ def execution_interval=(value)
240
+ raise ArgumentError, "must be greater than zero" if (value = value.to_f) <= 0.0
241
+
242
+ synchronize { @execution_interval = value }
243
+ end
244
+
245
+ private :post, :<<
246
+
7
247
  private
8
248
 
9
249
  def ns_initialize(opts, &task)
10
250
  set_deref_options(opts)
11
251
 
12
252
  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
253
+ if opts[:timeout] || opts[:timeout_interval]
254
+ warn "TimeTask timeouts are now ignored as these were not able to be implemented correctly"
255
+ end
256
+ @run_now = opts[:now] || opts[:run_now]
257
+ @executor = Concurrent::SafeTaskExecutor.new(task)
258
+ @running = Concurrent::AtomicBoolean.new(false)
259
+ @value = nil
19
260
 
20
261
  self.observers = Concurrent::Collection::CopyOnNotifyObserverSet.new
21
262
  end
22
263
 
264
+ # @!visibility private
265
+ def ns_shutdown_execution
266
+ @running.make_false
267
+ super
268
+ end
269
+
270
+ # @!visibility private
271
+ def ns_kill_execution
272
+ @running.make_false
273
+ super
274
+ end
275
+
276
+ # @!visibility private
23
277
  def schedule_next_task(interval = execution_interval)
24
278
  exec_task = ->(completion) { execute_task(completion) }
25
279
  Concurrent::ScheduledTask.execute(interval, args: [Concurrent::Event.new], &exec_task)
@@ -27,52 +281,19 @@ module SidekiqUniqueJobs
27
281
  end
28
282
 
29
283
  # @!visibility private
30
- def execute_task(completion) # rubocop:disable Metrics/MethodLength
284
+ def execute_task(completion)
31
285
  return nil unless @running.true?
32
286
 
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
-
287
+ _success, value, reason = @executor.execute(self)
53
288
  if completion.try?
289
+ self.value = value
54
290
  schedule_next_task
55
291
  time = Time.now
56
292
  observers.notify_observers do
57
- [time, value, @reason]
293
+ [time, self.value, reason]
58
294
  end
59
295
  end
60
296
  nil
61
297
  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
298
  end
78
299
  end
@@ -48,7 +48,7 @@ module SidekiqUniqueJobs
48
48
  # @return [Float]
49
49
  #
50
50
  def clock_stamp
51
- if Process.const_defined?("CLOCK_MONOTONIC")
51
+ if Process.const_defined?(:CLOCK_MONOTONIC)
52
52
  Process.clock_gettime(Process::CLOCK_MONOTONIC)
53
53
  else
54
54
  now_f
@@ -87,9 +87,9 @@ module SidekiqUniqueJobs
87
87
  digest = grabbed_key.gsub(":GRABBED", "")
88
88
  locks = conn.hgetall(grabbed_key)
89
89
 
90
- conn.pipelined do
91
- conn.hmset(locked_key, *locks.to_a)
92
- conn.zadd(DIGESTS, locks.values.first, digest)
90
+ conn.pipelined do |pipeline|
91
+ pipeline.hmset(locked_key, *locks.to_a)
92
+ pipeline.zadd(DIGESTS, locks.values.first, digest)
93
93
  end
94
94
  end
95
95
 
@@ -114,11 +114,11 @@ module SidekiqUniqueJobs
114
114
  def batch_delete(*keys)
115
115
  return if keys.empty?
116
116
 
117
- conn.pipelined do
117
+ conn.pipelined do |pipeline|
118
118
  if VersionCheck.satisfied?(redis_version, ">= 4.0.0")
119
- conn.unlink(*keys)
119
+ pipeline.unlink(*keys)
120
120
  else
121
- conn.del(*keys)
121
+ pipeline.del(*keys)
122
122
  end
123
123
  end
124
124
  end
@@ -3,5 +3,5 @@
3
3
  module SidekiqUniqueJobs
4
4
  #
5
5
  # @return [String] the current SidekiqUniqueJobs version
6
- VERSION = "7.1.5"
6
+ VERSION = "7.1.30"
7
7
  end