Class: Gitlab::ProcessSupervisor
- 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
-
#alive ⇒ Object
readonly
Returns the value of attribute alive.
Attributes inherited from Daemon
Instance Method Summary collapse
-
#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
constructor
A new instance of ProcessSupervisor.
-
#shutdown(signal = :TERM) ⇒ Object
Shuts down the supervisor and all supervised processes with the given signal.
-
#supervise(pid_or_pids, &on_process_death) ⇒ Object
Starts a supervision loop for the given process ID(s).
- #supervised_pids ⇒ Object
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: [], **) super(**) @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
#alive ⇒ Object (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_pids ⇒ Object
63 64 65 |
# File 'lib/gitlab/process_supervisor.rb', line 63 def supervised_pids @pids end |