Module: SidekiqUniqueJobs::Orphans::Manager
- Defined in:
- lib/sidekiq_unique_jobs/orphans/manager.rb
Overview
Manages the orphan reaper
Constant Summary collapse
- DRIFT_FACTOR =
Returns the amount to add to the reaper interval.
0.02
- REAPERS =
Returns allowed reapers (:ruby or :lua).
[:ruby, :lua].freeze
Class Method Summary collapse
-
.current_timestamp ⇒ Integer
Current time (as integer value).
-
.default_task ⇒ SidekiqUniqueJobs::TimerTask
A properly configured timer task.
-
.disabled? ⇒ true, false
Checks if reaping is disabled.
-
.drift_reaper_interval ⇒ Integer
Reaper interval with a little drift Redis isn’t exact enough so to give a little buffer, we add a tiny value to the reaper interval.
-
.enabled? ⇒ true, false
Checks if reaping is enabled.
-
.logging_context ⇒ Hash, String
A context to use for all log entries.
- .reaper ⇒ Object
- .reaper_interval ⇒ Object
-
.refresh_reaper_mutex ⇒ void
Updates mutex key.
-
.register_reaper_process ⇒ void
Writes a mutex key to redis.
-
.registered? ⇒ true, false
Checks if a reaper is registered.
-
.start(test_task = nil) ⇒ SidekiqUniqueJobs::TimerTask
Starts a separate thread that periodically reaps orphans.
-
.stop ⇒ Boolean
Stops the thread that reaps orphans.
-
.task ⇒ <type>
The task that runs the reaper.
-
.task=(task) ⇒ void
Store a task to use for scheduled execution.
-
.timer_task_options ⇒ Hash
Arguments passed on to the timer task.
-
.unregister_reaper_process ⇒ void
Removes mutex key from redis.
-
.unregistered? ⇒ true, false
Checks if that reapers are not registerd.
Class Method Details
.current_timestamp ⇒ Integer
Current time (as integer value)
237 238 239 |
# File 'lib/sidekiq_unique_jobs/orphans/manager.rb', line 237 def Time.now.to_i end |
.default_task ⇒ SidekiqUniqueJobs::TimerTask
A properly configured timer task
85 86 87 88 89 90 91 92 93 94 |
# File 'lib/sidekiq_unique_jobs/orphans/manager.rb', line 85 def default_task SidekiqUniqueJobs::TimerTask.new() do with_logging_context do redis do |conn| refresh_reaper_mutex Orphans::Reaper.call(conn) end end end end |
.disabled? ⇒ true, false
Checks if reaping is disabled
176 177 178 |
# File 'lib/sidekiq_unique_jobs/orphans/manager.rb', line 176 def disabled? !enabled? end |
.drift_reaper_interval ⇒ Integer
Reaper interval with a little drift
Redis isn't exact enough so to give a little buffer,
we add a tiny value to the reaper interval.
227 228 229 |
# File 'lib/sidekiq_unique_jobs/orphans/manager.rb', line 227 def drift_reaper_interval reaper_interval + (reaper_interval * DRIFT_FACTOR).to_i end |
.enabled? ⇒ true, false
Checks if reaping is enabled
185 186 187 |
# File 'lib/sidekiq_unique_jobs/orphans/manager.rb', line 185 def enabled? REAPERS.include?(reaper) end |
.logging_context ⇒ Hash, String
A context to use for all log entries
138 139 140 141 142 143 144 |
# File 'lib/sidekiq_unique_jobs/orphans/manager.rb', line 138 def logging_context if logger_context_hash? { "uniquejobs" => "reaper" } else "uniquejobs=orphan-reaper" end end |
.reaper ⇒ Object
120 121 122 |
# File 'lib/sidekiq_unique_jobs/orphans/manager.rb', line 120 def reaper SidekiqUniqueJobs.config.reaper end |
.reaper_interval ⇒ Object
127 128 129 |
# File 'lib/sidekiq_unique_jobs/orphans/manager.rb', line 127 def reaper_interval SidekiqUniqueJobs.config.reaper_interval end |
.refresh_reaper_mutex ⇒ void
This method returns an undefined value.
Updates mutex key
205 206 207 |
# File 'lib/sidekiq_unique_jobs/orphans/manager.rb', line 205 def refresh_reaper_mutex redis { |conn| conn.set(UNIQUE_REAPER, , "ex", drift_reaper_interval) } end |
.register_reaper_process ⇒ void
This method returns an undefined value.
Writes a mutex key to redis
195 196 197 |
# File 'lib/sidekiq_unique_jobs/orphans/manager.rb', line 195 def register_reaper_process redis { |conn| conn.set(UNIQUE_REAPER, , "nx", "ex", drift_reaper_interval) } end |
.registered? ⇒ true, false
Checks if a reaper is registered
152 153 154 155 156 |
# File 'lib/sidekiq_unique_jobs/orphans/manager.rb', line 152 def registered? redis do |conn| conn.get(UNIQUE_REAPER).to_i + drift_reaper_interval > end end |
.start(test_task = nil) ⇒ SidekiqUniqueJobs::TimerTask
Starts a separate thread that periodically reaps orphans
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/sidekiq_unique_jobs/orphans/manager.rb', line 35 def start(test_task = nil) # rubocop:disable return if disabled? return if registered? self.task = test_task || default_task with_logging_context do if register_reaper_process log_info("Starting Reaper") task.add_observer(Observer.new) task.execute task end end end |
.stop ⇒ Boolean
Stops the thread that reaps orphans
58 59 60 61 62 63 64 65 66 67 |
# File 'lib/sidekiq_unique_jobs/orphans/manager.rb', line 58 def stop return if disabled? return if unregistered? with_logging_context do log_info("Stopping Reaper") unregister_reaper_process task.shutdown end end |
.task ⇒ <type>
The task that runs the reaper
75 76 77 |
# File 'lib/sidekiq_unique_jobs/orphans/manager.rb', line 75 def task @task ||= default_task # rubocop:disable ThreadSafety/InstanceVariableInClassMethod end |
.task=(task) ⇒ void
This method returns an undefined value.
Store a task to use for scheduled execution
103 104 105 |
# File 'lib/sidekiq_unique_jobs/orphans/manager.rb', line 103 def task=(task) @task = task # rubocop:disable ThreadSafety/InstanceVariableInClassMethod end |
.timer_task_options ⇒ Hash
Arguments passed on to the timer task
113 114 115 |
# File 'lib/sidekiq_unique_jobs/orphans/manager.rb', line 113 def { run_now: true, execution_interval: reaper_interval } end |
.unregister_reaper_process ⇒ void
This method returns an undefined value.
Removes mutex key from redis
215 216 217 |
# File 'lib/sidekiq_unique_jobs/orphans/manager.rb', line 215 def unregister_reaper_process redis { |conn| conn.del(UNIQUE_REAPER) } end |
.unregistered? ⇒ true, false
Checks if that reapers are not registerd
165 166 167 |
# File 'lib/sidekiq_unique_jobs/orphans/manager.rb', line 165 def unregistered? !registered? end |