Class: Gitlab::ProcessSupervisor

Inherits:
Daemon
  • Object
show all
Defined in:
lib/gitlab/process_supervisor.rb

Overview

Given a set of process IDs, the supervisor can monitor processes for being alive and invoke a callback if some or all should go away. The receiver of the callback can then act on this event, for instance by restarting those processes or performing clean-up work.

The supervisor will also trap termination signals if provided and propagate those to the supervised processes. Any supervised processes that do not terminate within a specified grace period will be killed.

Constant Summary collapse

DEFAULT_HEALTH_CHECK_INTERVAL_SECONDS =
5
DEFAULT_TERMINATE_INTERVAL_SECONDS =
1
DEFAULT_TERMINATE_TIMEOUT_SECONDS =
10

Instance Attribute Summary collapse

Attributes inherited from Daemon

#thread

Instance Method Summary collapse

Methods inherited from Daemon

#enabled?, initialize_instance, instance, #start, #stop, #thread?, #thread_name

Constructor Details

#initialize(health_check_interval_seconds: DEFAULT_HEALTH_CHECK_INTERVAL_SECONDS, check_terminate_interval_seconds: DEFAULT_TERMINATE_INTERVAL_SECONDS, terminate_timeout_seconds: DEFAULT_TERMINATE_TIMEOUT_SECONDS, term_signals: [], forwarded_signals: [], **options) ⇒ ProcessSupervisor

Returns a new instance of ProcessSupervisor.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/gitlab/process_supervisor.rb', line 21

def initialize(
  health_check_interval_seconds: DEFAULT_HEALTH_CHECK_INTERVAL_SECONDS,
  check_terminate_interval_seconds: DEFAULT_TERMINATE_INTERVAL_SECONDS,
  terminate_timeout_seconds: DEFAULT_TERMINATE_TIMEOUT_SECONDS,
  term_signals: [],
  forwarded_signals: [],
  **options)
  super(**options)

  @term_signals = term_signals
  @forwarded_signals = forwarded_signals
  @health_check_interval_seconds = health_check_interval_seconds
  @check_terminate_interval_seconds = check_terminate_interval_seconds
  @terminate_timeout_seconds = terminate_timeout_seconds

  @pids = Set.new
  @alive = false
end

Instance Attribute Details

#aliveObject (readonly)

Returns the value of attribute alive.



19
20
21
# File 'lib/gitlab/process_supervisor.rb', line 19

def alive
  @alive
end

Instance Method Details

#shutdown(signal = :TERM) ⇒ Object

Shuts down the supervisor and all supervised processes with the given signal.



57
58
59
60
61
# File 'lib/gitlab/process_supervisor.rb', line 57

def shutdown(signal = :TERM)
  return unless @alive

  stop_processes(signal)
end

#supervise(pid_or_pids, &on_process_death) ⇒ Object

Starts a supervision loop for the given process ID(s).

If any or all processes go away, the IDs of any dead processes will be yielded to the given block, so callers can act on them.

If the block returns a non-empty list of IDs, the supervisor will start observing those processes instead. Otherwise it will shut down.



47
48
49
50
51
52
53
54
# File 'lib/gitlab/process_supervisor.rb', line 47

def supervise(pid_or_pids, &on_process_death)
  @pids = Array(pid_or_pids).to_set
  @on_process_death = on_process_death

  trap_signals!

  start
end

#supervised_pidsObject



63
64
65
# File 'lib/gitlab/process_supervisor.rb', line 63

def supervised_pids
  @pids
end