module SidekiqUniqueJobs::Orphans::Manager

Manages the orphan reaper

@author Mikael Henriksson <mikael@mhenrixon.com>

Constants

DRIFT_FACTOR

@return [Float] the amount to add to the reaper interval

REAPERS

@return [Symbol] allowed reapers (:ruby or :lua)

Public Instance Methods

current_timestamp() click to toggle source

Current time (as integer value)

@return [Integer]

# File lib/sidekiq_unique_jobs/orphans/manager.rb, line 243
def current_timestamp
  Time.now.to_i
end
default_task() click to toggle source

A properly configured timer task

@return [SidekiqUniqueJobs::TimerTask]

# File lib/sidekiq_unique_jobs/orphans/manager.rb, line 82
def default_task
  SidekiqUniqueJobs::TimerTask.new(timer_task_options) do
    with_logging_context do
      redis do |conn|
        refresh_reaper_mutex
        Orphans::Reaper.call(conn)
      end
    end
  end
end
disabled?() click to toggle source

Checks if reaping is disabled

@see enabled?

@return [true, false]

# File lib/sidekiq_unique_jobs/orphans/manager.rb, line 182
def disabled?
  !enabled?
end
drift_reaper_interval() click to toggle source

Reaper interval with a little drift

Redis isn't exact enough so to give a little bufffer,
we add a tiny value to the reaper interval.

@return [Integer] <description>

# File lib/sidekiq_unique_jobs/orphans/manager.rb, line 233
def drift_reaper_interval
  reaper_interval + (reaper_interval * DRIFT_FACTOR).to_i
end
enabled?() click to toggle source

Checks if reaping is enabled

@return [true, false]

# File lib/sidekiq_unique_jobs/orphans/manager.rb, line 191
def enabled?
  REAPERS.include?(reaper)
end
logging_context() click to toggle source

A context to use for all log entries

@return [Hash] when logger responds to `:with_context` @return [String] when logger does not responds to `:with_context`

# File lib/sidekiq_unique_jobs/orphans/manager.rb, line 144
def logging_context
  if logger_context_hash?
    { "uniquejobs" => "reaper" }
  else
    "uniquejobs=orphan-reaper"
  end
end
reaper() click to toggle source

@see SidekiqUniqueJobs::Config#reaper

# File lib/sidekiq_unique_jobs/orphans/manager.rb, line 119
def reaper
  SidekiqUniqueJobs.config.reaper
end
reaper_interval() click to toggle source

@see SidekiqUniqueJobs::Config#reaper_interval

# File lib/sidekiq_unique_jobs/orphans/manager.rb, line 126
def reaper_interval
  SidekiqUniqueJobs.config.reaper_interval
end
reaper_timeout() click to toggle source

@see SidekiqUniqueJobs::Config#reaper_timeout

# File lib/sidekiq_unique_jobs/orphans/manager.rb, line 133
def reaper_timeout
  SidekiqUniqueJobs.config.reaper_timeout
end
refresh_reaper_mutex() click to toggle source

Updates mutex key

@return [void]

# File lib/sidekiq_unique_jobs/orphans/manager.rb, line 211
def refresh_reaper_mutex
  redis { |conn| conn.set(UNIQUE_REAPER, current_timestamp, ex: drift_reaper_interval) }
end
register_reaper_process() click to toggle source

Writes a mutex key to redis

@return [void]

# File lib/sidekiq_unique_jobs/orphans/manager.rb, line 201
def register_reaper_process
  redis { |conn| conn.set(UNIQUE_REAPER, current_timestamp, nx: true, ex: drift_reaper_interval) }
end
registered?() click to toggle source

Checks if a reaper is registered

@return [true, false]

# File lib/sidekiq_unique_jobs/orphans/manager.rb, line 158
def registered?
  redis do |conn|
    conn.get(UNIQUE_REAPER).to_i + drift_reaper_interval > current_timestamp
  end
end
start(test_task = nil) click to toggle source

Starts a separate thread that periodically reaps orphans

@return [SidekiqUniqueJobs::TimerTask] the task that was started

# File lib/sidekiq_unique_jobs/orphans/manager.rb, line 33
def start(test_task = nil) # rubocop:disable
  return if disabled?
  return if registered?

  self.task = test_task || default_task

  with_logging_context do
    register_reaper_process
    log_info("Starting Reaper")

    task.add_observer(Observer.new)
    task.execute
    task
  end
end
stop() click to toggle source

Stops the thread that reaps orphans

@return [Boolean]

# File lib/sidekiq_unique_jobs/orphans/manager.rb, line 55
def stop
  return if disabled?
  return if unregistered?

  with_logging_context do
    log_info("Stopping Reaper")
    unregister_reaper_process
    task.shutdown
  end
end
task() click to toggle source

The task that runs the reaper

@return [<type>] <description>

# File lib/sidekiq_unique_jobs/orphans/manager.rb, line 72
def task
  @task ||= default_task
end
task=(task) click to toggle source

Store a task to use for scheduled execution

@param [SidekiqUniqueJobs::TimerTask] task the task to use

@return [void]

# File lib/sidekiq_unique_jobs/orphans/manager.rb, line 100
def task=(task)
  @task = task
end
timer_task_options() click to toggle source

Arguments passed on to the timer task

@return [Hash]

# File lib/sidekiq_unique_jobs/orphans/manager.rb, line 110
def timer_task_options
  { run_now: true,
    execution_interval: reaper_interval,
    timeout_interval: reaper_timeout }
end
unregister_reaper_process() click to toggle source

Removes mutex key from redis

@return [void]

# File lib/sidekiq_unique_jobs/orphans/manager.rb, line 221
def unregister_reaper_process
  redis { |conn| conn.del(UNIQUE_REAPER) }
end
unregistered?() click to toggle source

Checks if that reapers are not registerd

@see registered?

@return [true, false]

# File lib/sidekiq_unique_jobs/orphans/manager.rb, line 171
def unregistered?
  !registered?
end