Module: SidekiqUniqueJobs::Orphans::ReaperResurrector

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

Overview

Restarts orphan manager if it is considered dead

Constant Summary collapse

DRIFT_FACTOR =
0.1
REAPERS =
[:ruby, :lua].freeze

Class Method Summary collapse

Class Method Details

.current_timestampObject



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

def current_timestamp
  Time.now.to_i
end

.drift_reaper_intervalObject



161
162
163
# File 'lib/sidekiq_unique_jobs/orphans/reaper_resurrector.rb', line 161

def drift_reaper_interval
  reaper_interval + (reaper_interval * DRIFT_FACTOR).to_i
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`



142
143
144
145
146
147
148
# File 'lib/sidekiq_unique_jobs/orphans/reaper_resurrector.rb', line 142

def logging_context
  if logger_context_hash?
    { "uniquejobs" => "reaper-resurrector" }
  else
    "uniquejobs=reaper-resurrector"
  end
end

.orphans_managerSidekiqUniqueJobs::Orphans::Manager

Returns orphan manager



65
66
67
# File 'lib/sidekiq_unique_jobs/orphans/reaper_resurrector.rb', line 65

def orphans_manager
  SidekiqUniqueJobs::Orphans::Manager
end

.reaperObject

See Also:

  • Config#reaper


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

def reaper
  SidekiqUniqueJobs.config.reaper
end

.reaper_disabled?true, false

Checks if reaping is disabled

Returns:

  • (true, false)

See Also:



94
95
96
# File 'lib/sidekiq_unique_jobs/orphans/reaper_resurrector.rb', line 94

def reaper_disabled?
  !reaper_enabled?
end

.reaper_enabled?true, false

Checks if reaping is enabled

Returns:

  • (true, false)


103
104
105
# File 'lib/sidekiq_unique_jobs/orphans/reaper_resurrector.rb', line 103

def reaper_enabled?
  REAPERS.include?(reaper)
end

.reaper_intervalObject



157
158
159
# File 'lib/sidekiq_unique_jobs/orphans/reaper_resurrector.rb', line 157

def reaper_interval
  SidekiqUniqueJobs.config.reaper_interval
end

.reaper_registered?true, false

Checks if reaper is registered

Returns:

  • (true, false)


111
112
113
114
115
# File 'lib/sidekiq_unique_jobs/orphans/reaper_resurrector.rb', line 111

def reaper_registered?
  redis do |conn|
    conn.get(UNIQUE_REAPER).to_i + drift_reaper_interval > current_timestamp
  end
end

.reaper_resurrector_intervalObject

See Also:

  • Config#reaper_resurrector_interval


153
154
155
# File 'lib/sidekiq_unique_jobs/orphans/reaper_resurrector.rb', line 153

def reaper_resurrector_interval
  SidekiqUniqueJobs.config.reaper_resurrector_interval
end

.restart_if_deadObject

Starts new instance of orphan reaper if reaper is considered dead (reaper mutex has not been refreshed lately)



54
55
56
57
58
59
# File 'lib/sidekiq_unique_jobs/orphans/reaper_resurrector.rb', line 54

def restart_if_dead
  return if reaper_registered?

  log_info("Reaper is considered dead. Starting new reaper instance")
  orphans_manager.start
end

.resurrector_disabled?true, false

Checks if resurrector is disabled

Returns:

  • (true, false)

See Also:



75
76
77
# File 'lib/sidekiq_unique_jobs/orphans/reaper_resurrector.rb', line 75

def resurrector_disabled?
  !resurrector_enabled?
end

.resurrector_enabled?true, false

Checks if resurrector is enabled

Returns:

  • (true, false)


83
84
85
# File 'lib/sidekiq_unique_jobs/orphans/reaper_resurrector.rb', line 83

def resurrector_enabled?
  SidekiqUniqueJobs.config.reaper_resurrector_enabled
end

.run_taskSidekiqUniqueJobs::TimerTask

Runs reaper resurrector task



33
34
35
36
37
# File 'lib/sidekiq_unique_jobs/orphans/reaper_resurrector.rb', line 33

def run_task
  log_info("Starting Reaper Resurrector")
  task.execute
  task
end

.startSidekiqUniqueJobs::TimerTask

Starts reaper resurrector that watches orphans reaper

Returns:



20
21
22
23
24
25
26
27
# File 'lib/sidekiq_unique_jobs/orphans/reaper_resurrector.rb', line 20

def start
  return if resurrector_disabled?
  return if reaper_disabled?

  with_logging_context do
    run_task
  end
end

.taskSidekiqUniqueJobs::TimerTask

The task that runs the resurrector



43
44
45
46
47
48
49
# File 'lib/sidekiq_unique_jobs/orphans/reaper_resurrector.rb', line 43

def task
  SidekiqUniqueJobs::TimerTask.new(timer_task_options) do
    with_logging_context do
      restart_if_dead
    end
  end
end

.timer_task_optionsHash

Arguments passed on to the timer task

Returns:



130
131
132
133
# File 'lib/sidekiq_unique_jobs/orphans/reaper_resurrector.rb', line 130

def timer_task_options
  { run_now: false,
    execution_interval: reaper_resurrector_interval }
end