Class: Relay::TaskMonitor

Inherits:
Object
  • Object
show all
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

Constructor Details

#initialize(tasks:) ⇒ Relay::TaskMonitor

Parameters:

  • tasks (Array<String>)

    A list of task names



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

#monitorvoid

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