Class: Ductwork::Processes::ProcessSupervisor

Inherits:
Object
  • Object
show all
Defined in:
lib/ductwork/processes/process_supervisor.rb

Overview

rubocop:todo Metrics/ClassLength

Constant Summary collapse

KILL_REAP_TIMEOUT =

Upper bound on how long sig_kill_process will block the supervisor loop waiting for a KILL'd child to be reaped (see sig_kill_process).

5
KILL_REAP_INTERVAL =

seconds

0.1

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeProcessSupervisor

Returns a new instance of ProcessSupervisor.



13
14
15
16
17
18
19
20
21
22
# File 'lib/ductwork/processes/process_supervisor.rb', line 13

def initialize
  @running_context = Ductwork::RunningContext.new
  @workers = []

  run_hooks_for(:start)

  Signal.trap(:INT) { @running_context.shutdown! }
  Signal.trap(:TERM) { @running_context.shutdown! }
  Signal.trap(:TTIN) { puts "No threads to dump" }
end

Instance Attribute Details

#workersObject (readonly)

Returns the value of attribute workers.



11
12
13
# File 'lib/ductwork/processes/process_supervisor.rb', line 11

def workers
  @workers
end

Instance Method Details

#add_worker(metadata: {}, &block) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/ductwork/processes/process_supervisor.rb', line 24

def add_worker(metadata: {}, &block)
  pid = fork do
    block.call()
  end

  workers << { metadata:, pid:, block: }
  Ductwork.logger.debug(
    msg: "Started child process (#{pid}) with metadata #{}",
    pid: pid
  )
end

#runObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/ductwork/processes/process_supervisor.rb', line 36

def run
  adopt_or_create_process!

  Ductwork.logger.debug(
    msg: "Entering main work loop",
    role: :process_supervisor,
    pid: ::Process.pid
  )

  while running_context.running?
    sleep(Ductwork.configuration.supervisor_polling_timeout)
    check_workers
    reap_process_records
  end

  shutdown
end

#shutdownObject



54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/ductwork/processes/process_supervisor.rb', line 54

def shutdown
  running_context.shutdown!
  Ductwork.logger.debug(
    msg: "Beginning shutdown",
    role: :process_supervisor
  )

  terminate_gracefully
  wait_for_workers_to_exit
  terminate_immediately
  run_hooks_for(:stop)
end