Class: Relay::TaskMonitor
- Inherits:
-
Object
- Object
- Relay::TaskMonitor
- Defined in:
- lib/relay/task_monitor.rb
Overview
A task monitor that runs a list of tasks in parallel and monitors them afterwards. When a task fails, the monitor will exit and kill all other tasks.
A Task becomes a process group leader, so killing the process group will kill all processes in the group which includes the task and all of its subprocesses.
Instance Method Summary collapse
- #initialize(tasks:) ⇒ Relay::TaskMonitor constructor
-
#monitor ⇒ void
Start the task monitor.
-
#prefork(&block) ⇒ void
Assign a block that is run before tasks are forked.
Constructor Details
#initialize(tasks:) ⇒ Relay::TaskMonitor
16 17 18 19 |
# File 'lib/relay/task_monitor.rb', line 16 def initialize(tasks:) @tasks = tasks.map { Relay::Task.new(_1) } @pids = [] end |
Instance Method Details
#monitor ⇒ void
This method returns an undefined value.
Start the task monitor
31 32 33 34 35 36 37 38 |
# File 'lib/relay/task_monitor.rb', line 31 def monitor @prefork&.call run wait rescue Interrupt @pids.each { Process.kill("TERM", -_1) } wait end |
#prefork(&block) ⇒ void
This method returns an undefined value.
Assign a block that is run before tasks are forked
24 25 26 |
# File 'lib/relay/task_monitor.rb', line 24 def prefork(&block) @prefork = block end |