sidekiq-unique-jobs 6.0.25 → 7.1.29

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 (129) 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 +41 -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 +29 -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/locksmith.rb +295 -101
  36. data/lib/sidekiq_unique_jobs/logging/middleware_context.rb +44 -0
  37. data/lib/sidekiq_unique_jobs/logging.rb +202 -33
  38. data/lib/sidekiq_unique_jobs/lua/delete.lua +51 -0
  39. data/lib/sidekiq_unique_jobs/lua/delete_by_digest.lua +42 -0
  40. data/lib/sidekiq_unique_jobs/lua/delete_job_by_digest.lua +38 -0
  41. data/lib/sidekiq_unique_jobs/lua/find_digest_in_queues.lua +26 -0
  42. data/lib/sidekiq_unique_jobs/lua/lock.lua +99 -0
  43. data/lib/sidekiq_unique_jobs/lua/lock_until_expired.lua +92 -0
  44. data/lib/sidekiq_unique_jobs/lua/locked.lua +35 -0
  45. data/lib/sidekiq_unique_jobs/lua/queue.lua +87 -0
  46. data/lib/sidekiq_unique_jobs/lua/reap_orphans.lua +122 -0
  47. data/lib/sidekiq_unique_jobs/lua/shared/_common.lua +40 -0
  48. data/lib/sidekiq_unique_jobs/lua/shared/_current_time.lua +8 -0
  49. data/lib/sidekiq_unique_jobs/lua/shared/_delete_from_queue.lua +22 -0
  50. data/lib/sidekiq_unique_jobs/lua/shared/_delete_from_sorted_set.lua +18 -0
  51. data/lib/sidekiq_unique_jobs/lua/shared/_find_digest_in_process_set.lua +53 -0
  52. data/lib/sidekiq_unique_jobs/lua/shared/_find_digest_in_queues.lua +43 -0
  53. data/lib/sidekiq_unique_jobs/lua/shared/_find_digest_in_sorted_set.lua +24 -0
  54. data/lib/sidekiq_unique_jobs/lua/shared/_hgetall.lua +13 -0
  55. data/lib/sidekiq_unique_jobs/lua/shared/_upgrades.lua +3 -0
  56. data/lib/sidekiq_unique_jobs/lua/unlock.lua +107 -0
  57. data/lib/sidekiq_unique_jobs/lua/update_version.lua +40 -0
  58. data/lib/sidekiq_unique_jobs/lua/upgrade.lua +68 -0
  59. data/lib/sidekiq_unique_jobs/middleware/client.rb +42 -0
  60. data/lib/sidekiq_unique_jobs/middleware/server.rb +31 -0
  61. data/lib/sidekiq_unique_jobs/middleware.rb +29 -43
  62. data/lib/sidekiq_unique_jobs/normalizer.rb +4 -4
  63. data/lib/sidekiq_unique_jobs/on_conflict/log.rb +9 -5
  64. data/lib/sidekiq_unique_jobs/on_conflict/null_strategy.rb +1 -1
  65. data/lib/sidekiq_unique_jobs/on_conflict/raise.rb +1 -1
  66. data/lib/sidekiq_unique_jobs/on_conflict/reject.rb +63 -17
  67. data/lib/sidekiq_unique_jobs/on_conflict/replace.rb +54 -14
  68. data/lib/sidekiq_unique_jobs/on_conflict/reschedule.rb +16 -5
  69. data/lib/sidekiq_unique_jobs/on_conflict/strategy.rb +25 -6
  70. data/lib/sidekiq_unique_jobs/on_conflict.rb +23 -10
  71. data/lib/sidekiq_unique_jobs/options_with_fallback.rb +38 -35
  72. data/lib/sidekiq_unique_jobs/orphans/lua_reaper.rb +29 -0
  73. data/lib/sidekiq_unique_jobs/orphans/manager.rb +241 -0
  74. data/lib/sidekiq_unique_jobs/orphans/null_reaper.rb +24 -0
  75. data/lib/sidekiq_unique_jobs/orphans/observer.rb +42 -0
  76. data/lib/sidekiq_unique_jobs/orphans/reaper.rb +114 -0
  77. data/lib/sidekiq_unique_jobs/orphans/reaper_resurrector.rb +170 -0
  78. data/lib/sidekiq_unique_jobs/orphans/ruby_reaper.rb +298 -0
  79. data/lib/sidekiq_unique_jobs/redis/entity.rb +112 -0
  80. data/lib/sidekiq_unique_jobs/redis/hash.rb +56 -0
  81. data/lib/sidekiq_unique_jobs/redis/list.rb +32 -0
  82. data/lib/sidekiq_unique_jobs/redis/set.rb +32 -0
  83. data/lib/sidekiq_unique_jobs/redis/sorted_set.rb +86 -0
  84. data/lib/sidekiq_unique_jobs/redis/string.rb +51 -0
  85. data/lib/sidekiq_unique_jobs/redis.rb +11 -0
  86. data/lib/sidekiq_unique_jobs/reflectable.rb +26 -0
  87. data/lib/sidekiq_unique_jobs/reflections.rb +79 -0
  88. data/lib/sidekiq_unique_jobs/rspec/matchers/have_valid_sidekiq_options.rb +51 -0
  89. data/lib/sidekiq_unique_jobs/rspec/matchers.rb +26 -0
  90. data/lib/sidekiq_unique_jobs/script/caller.rb +127 -0
  91. data/lib/sidekiq_unique_jobs/script.rb +15 -0
  92. data/lib/sidekiq_unique_jobs/server.rb +61 -0
  93. data/lib/sidekiq_unique_jobs/sidekiq_unique_ext.rb +114 -65
  94. data/lib/sidekiq_unique_jobs/sidekiq_unique_jobs.rb +251 -35
  95. data/lib/sidekiq_unique_jobs/sidekiq_worker_methods.rb +47 -32
  96. data/lib/sidekiq_unique_jobs/testing.rb +102 -29
  97. data/lib/sidekiq_unique_jobs/timer_task.rb +299 -0
  98. data/lib/sidekiq_unique_jobs/timing.rb +58 -0
  99. data/lib/sidekiq_unique_jobs/unlockable.rb +20 -4
  100. data/lib/sidekiq_unique_jobs/update_version.rb +25 -0
  101. data/lib/sidekiq_unique_jobs/upgrade_locks.rb +155 -0
  102. data/lib/sidekiq_unique_jobs/version.rb +3 -1
  103. data/lib/sidekiq_unique_jobs/version_check.rb +23 -4
  104. data/lib/sidekiq_unique_jobs/web/helpers.rb +138 -13
  105. data/lib/sidekiq_unique_jobs/web/views/_paging.erb +4 -4
  106. data/lib/sidekiq_unique_jobs/web/views/changelogs.erb +54 -0
  107. data/lib/sidekiq_unique_jobs/web/views/lock.erb +108 -0
  108. data/lib/sidekiq_unique_jobs/web/views/locks.erb +54 -0
  109. data/lib/sidekiq_unique_jobs/web.rb +76 -27
  110. data/lib/sidekiq_unique_jobs.rb +53 -7
  111. data/lib/tasks/changelog.rake +16 -16
  112. metadata +133 -177
  113. data/lib/sidekiq_unique_jobs/client/middleware.rb +0 -56
  114. data/lib/sidekiq_unique_jobs/scripts.rb +0 -118
  115. data/lib/sidekiq_unique_jobs/server/middleware.rb +0 -46
  116. data/lib/sidekiq_unique_jobs/timeout/calculator.rb +0 -63
  117. data/lib/sidekiq_unique_jobs/timeout.rb +0 -8
  118. data/lib/sidekiq_unique_jobs/unique_args.rb +0 -150
  119. data/lib/sidekiq_unique_jobs/util.rb +0 -103
  120. data/lib/sidekiq_unique_jobs/web/views/unique_digest.erb +0 -28
  121. data/lib/sidekiq_unique_jobs/web/views/unique_digests.erb +0 -46
  122. data/redis/acquire_lock.lua +0 -21
  123. data/redis/convert_legacy_lock.lua +0 -13
  124. data/redis/delete.lua +0 -14
  125. data/redis/delete_by_digest.lua +0 -23
  126. data/redis/delete_job_by_digest.lua +0 -60
  127. data/redis/lock.lua +0 -62
  128. data/redis/release_stale_locks.lua +0 -90
  129. data/redis/unlock.lua +0 -35
@@ -1,62 +1,135 @@
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
9
- def self.use_options(tmp_config = {})
10
- old_config = default_worker_options
11
- default_worker_options.clear
12
- self.default_worker_options = tmp_config
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
+ #
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
13
32
 
14
33
  yield
15
34
  ensure
16
- default_worker_options.clear
17
- self.default_worker_options = old_config
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
18
42
  end
19
43
 
44
+ #
45
+ # See Sidekiq::Worker in Sidekiq gem for more details
46
+ #
20
47
  module Worker
48
+ #
49
+ # Adds class methods to Sidekiq::Worker
50
+ #
21
51
  module ClassMethods
52
+ #
53
+ # Temporarily turn a workers sidekiq_options into something different
54
+ #
55
+ # @note this method will restore the original configuration after yielding
56
+ #
57
+ # @param [Hash<Symbol, Object>] tmp_config the temporary config to use
58
+ #
22
59
  def use_options(tmp_config = {})
23
- old_config = get_sidekiq_options
24
- sidekiq_options(tmp_config)
60
+ old_options = sidekiq_options_hash.dup
61
+ sidekiq_options(old_options.merge(tmp_config))
25
62
 
26
63
  yield
27
64
  ensure
28
- sidekiq_options(old_config)
29
- end
30
-
31
- def clear
32
- jobs.each do |job|
33
- SidekiqUniqueJobs::Unlockable.delete(job)
34
- 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
35
71
 
36
- Sidekiq::Queues[queue].clear
37
- jobs.clear
72
+ sidekiq_options(old_options)
38
73
  end
39
74
  end
40
75
 
76
+ #
77
+ # Prepends deletion of locks to clear_all
78
+ #
41
79
  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
80
+ #
81
+ # Overrides sidekiq_options on the worker class to prepend validation
82
+ #
83
+ # @param [Hash] options worker options
84
+ #
85
+ # @return [void]
86
+ #
87
+ def sidekiq_options(options = {})
88
+ SidekiqUniqueJobs.validate_worker!(options) if SidekiqUniqueJobs.config.raise_on_config_error
89
+
90
+ super(options)
91
+ end
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)
48
103
  end
104
+
105
+ super
49
106
  end
50
107
  end
108
+ end
51
109
 
52
- module Testing
53
- def clear_all_ext
54
- clear_all_orig
55
- SidekiqUniqueJobs::Util.del("*", 1000)
56
- end
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
123
+ #
124
+ # Clears all jobs for this worker and removes all locks
125
+ #
126
+ def clear_all
127
+ super
128
+
129
+ SidekiqUniqueJobs::Digests.new.delete_by_pattern("*", count: 10_000)
57
130
  end
58
131
  end
59
132
 
60
- include Overrides
133
+ singleton_class.prepend SignletonOverrides
61
134
  end
62
135
  end
@@ -0,0 +1,299 @@
1
+ # frozen_string_literal: true
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
+
12
+ module SidekiqUniqueJobs
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 }
83
+ #
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
+
247
+ private
248
+
249
+ def ns_initialize(opts, &task)
250
+ set_deref_options(opts)
251
+
252
+ self.execution_interval = opts[:execution] || opts[:execution_interval] || EXECUTION_INTERVAL
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
260
+
261
+ self.observers = Concurrent::Collection::CopyOnNotifyObserverSet.new
262
+ end
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
277
+ def schedule_next_task(interval = execution_interval)
278
+ exec_task = ->(completion) { execute_task(completion) }
279
+ Concurrent::ScheduledTask.execute(interval, args: [Concurrent::Event.new], &exec_task)
280
+ nil
281
+ end
282
+
283
+ # @!visibility private
284
+ def execute_task(completion)
285
+ return nil unless @running.true?
286
+
287
+ _success, value, reason = @executor.execute(self)
288
+ if completion.try?
289
+ self.value = value
290
+ schedule_next_task
291
+ time = Time.now
292
+ observers.notify_observers do
293
+ [time, self.value, reason]
294
+ end
295
+ end
296
+ nil
297
+ end
298
+ end
299
+ 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
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SidekiqUniqueJobs
4
+ #
5
+ # Class UpdateVersion sets the right version in redis
6
+ #
7
+ # @author Mikael Henriksson <mikael@mhenrixon.com>
8
+ #
9
+ class UpdateVersion
10
+ #
11
+ # Sets the right versions in redis
12
+ #
13
+ # @note the version isn't used yet but will be for automatic upgrades
14
+ #
15
+ # @return [true] when version changed
16
+ #
17
+ def self.call
18
+ Script::Caller.call_script(
19
+ :update_version,
20
+ keys: [LIVE_VERSION, DEAD_VERSION],
21
+ argv: [SidekiqUniqueJobs.version],
22
+ )
23
+ end
24
+ end
25
+ end