Module: SidekiqUniqueJobs::Orphans::Manager

Defined in:
lib/sidekiq_unique_jobs/orphans/manager.rb

Overview

Manages the orphan reaper

Author:

Constant Summary collapse

DRIFT_FACTOR =

Returns the amount to add to the reaper interval.

Returns:

  • (Float)

    the amount to add to the reaper interval

0.02
REAPERS =

Returns allowed reapers (:ruby or :lua).

Returns:

  • (Symbol)

    allowed reapers (:ruby or :lua)

[:ruby, :lua].freeze

Class Method Summary collapse

Class Method Details

.current_timestampInteger

Current time (as integer value)

Returns:

  • (Integer)


237
238
239
# File 'lib/sidekiq_unique_jobs/orphans/manager.rb', line 237

def current_timestamp
  Time.now.to_i
end

.default_taskSidekiqUniqueJobs::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(timer_task_options) 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

Returns:

  • (true, false)

See Also:



176
177
178
# File 'lib/sidekiq_unique_jobs/orphans/manager.rb', line 176

def disabled?
  !enabled?
end

.drift_reaper_intervalInteger

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.

Returns:

  • (Integer)

    <description>



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

Returns:

  • (true, false)


185
186
187
# File 'lib/sidekiq_unique_jobs/orphans/manager.rb', line 185

def enabled?
  REAPERS.include?(reaper)
end

.logging_contextHash, String

A context to use for all log entries

Returns:

  • (Hash)

    when logger responds to ‘:with_context`

  • (String)

    when logger does not responds to ‘:with_context`



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

.reaperObject

See Also:

  • Config#reaper


120
121
122
# File 'lib/sidekiq_unique_jobs/orphans/manager.rb', line 120

def reaper
  SidekiqUniqueJobs.config.reaper
end

.reaper_intervalObject

See Also:

  • Config#reaper_interval


127
128
129
# File 'lib/sidekiq_unique_jobs/orphans/manager.rb', line 127

def reaper_interval
  SidekiqUniqueJobs.config.reaper_interval
end

.refresh_reaper_mutexvoid

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, current_timestamp, "ex", drift_reaper_interval) }
end

.register_reaper_processvoid

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, current_timestamp, "nx", "ex", drift_reaper_interval) }
end

.registered?true, false

Checks if a reaper is registered

Returns:

  • (true, false)


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 > current_timestamp
  end
end

.start(test_task = nil) ⇒ SidekiqUniqueJobs::TimerTask

Starts a separate thread that periodically reaps orphans

Returns:



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

.stopBoolean

Stops the thread that reaps orphans

Returns:

  • (Boolean)


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

Returns:

  • (<type>)

    <description>



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

Parameters:



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_optionsHash

Arguments passed on to the timer task

Returns:



113
114
115
# File 'lib/sidekiq_unique_jobs/orphans/manager.rb', line 113

def timer_task_options
  { run_now: true, execution_interval: reaper_interval }
end

.unregister_reaper_processvoid

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

Returns:

  • (true, false)

See Also:



165
166
167
# File 'lib/sidekiq_unique_jobs/orphans/manager.rb', line 165

def unregistered?
  !registered?
end