class Consul::Async::ProcessHandler

Handle the full lifecycle of a process and allows to forward Posix signals to child process when needed.

Attributes

command[R]
exit_status[R]
last_signal_sent[R]
pid[R]
reload_scheduled[RW]
sig_reload[R]
sig_term[R]

Public Class Methods

new(command, sig_reload: 'HUP', sig_term: 'TERM') click to toggle source
# File lib/consul/async/process_handler.rb, line 12
def initialize(command, sig_reload: 'HUP', sig_term: 'TERM')
  raise 'empty sig_term is not supported' unless sig_term

  @command = command
  @sig_reload = sig_reload
  @sig_term = sig_term
  @pid = nil
  @exit_status = nil
  @last_signal_sent = Time.now
  @reload_scheduled = false
end

Public Instance Methods

kill() click to toggle source
# File lib/consul/async/process_handler.rb, line 45
def kill
  return exit_status if pid.nil?

  the_pid = pid
  @pid = nil
  warn "[KILL] Sending SIG #{sig_term} to #{the_pid}..."
  begin
    warn "[KILL] waiting for #{the_pid}..."
    Process.kill(sig_term, the_pid)
  rescue Errno::ESRCH
    warn "[KILL] *** Process #{the_pid} has already been killed"
  end
  begin
    _pid, @exit_status = Process.waitpid2 the_pid
  rescue SystemCallError
    warn "[KILL] *** UNEXPECTED ERROR *** Failed to get return code for #{the_pid}"
  end
  exit_status
end
process_status() click to toggle source
# File lib/consul/async/process_handler.rb, line 65
def process_status
  raise ProcessDoesNotExist, 'No child process' if pid.nil?

  begin
    cpid, result = Process.waitpid2(pid, Process::WNOHANG)
    raise ProcessDoesNotExist, "Unexpected PID: #{cpid}, was expecting #{pid}" unless cpid.nil? || cpid == pid

    result
  rescue Errno::ECHILD => e
    e2 = ProcessDoesNotExist.new e
    raise e2, "ChildProcess has been killed: #{e.message}", e.backtrace
  end
end
reload() click to toggle source
# File lib/consul/async/process_handler.rb, line 31
def reload
  return if sig_reload.nil?

  @last_signal_sent = Time.now
  warn "Sending SIG #{sig_reload} to #{pid}..."
  @reload_scheduled = false
  begin
    Process.kill(sig_reload, pid)
  rescue Errno::ESRCH => e
    warn "*** Process #{pid} has already been killed: #{e.inspect}"
    raise e
  end
end
start() click to toggle source
# File lib/consul/async/process_handler.rb, line 24
def start
  return pid unless pid.nil?

  @pid = Process.spawn(command)
  @last_signal_sent = Time.now
end