Class: Gitlab::Memory::Watchdog::Handlers::SidekiqHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/gitlab/memory/watchdog/handlers/sidekiq_handler.rb

Instance Method Summary collapse

Constructor Details

#initialize(shutdown_timeout_seconds, sleep_time_seconds) ⇒ SidekiqHandler

Returns a new instance of SidekiqHandler.



8
9
10
11
12
# File 'lib/gitlab/memory/watchdog/handlers/sidekiq_handler.rb', line 8

def initialize(shutdown_timeout_seconds, sleep_time_seconds)
  @shutdown_timeout_seconds = shutdown_timeout_seconds
  @sleep_time_seconds = sleep_time_seconds
  @alive = true
end

Instance Method Details

#callObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/gitlab/memory/watchdog/handlers/sidekiq_handler.rb', line 14

def call
  # Tell Sidekiq to stop fetching new jobs
  # We first SIGNAL and then wait given time
  send_signal(:TSTP, $$, 'stop fetching new jobs', @shutdown_timeout_seconds)
  return true unless @alive

  # Tell sidekiq to restart itself
  # Keep extra safe to wait `Sidekiq[:timeout] + 2` seconds before SIGKILL
  send_signal(:TERM, $$, 'gracefully shut down', Sidekiq[:timeout] + 2)
  return true unless @alive

  # Ideally we should never reach this condition
  # Wait for Sidekiq to shutdown gracefully, and kill it if it didn't
  # If process is group leader, kill the whole pgroup, so we can be sure no children are left behind
  send_signal(:KILL, Process.getpgrp == $$ ? 0 : $$, 'hard shut down')

  true
end

#stopObject



33
34
35
# File 'lib/gitlab/memory/watchdog/handlers/sidekiq_handler.rb', line 33

def stop
  @alive = false
end