Class: Consul::Async::ProcessHandler
- Inherits:
-
Object
- Object
- Consul::Async::ProcessHandler
- Defined in:
- lib/consul/async/process_handler.rb
Overview
Handle the full lifecycle of a process and allows to forward Posix signals to child process when needed.
Instance Attribute Summary collapse
-
#command ⇒ Object
readonly
Returns the value of attribute command.
-
#exit_status ⇒ Object
readonly
Returns the value of attribute exit_status.
-
#last_signal_sent ⇒ Object
readonly
Returns the value of attribute last_signal_sent.
-
#pid ⇒ Object
readonly
Returns the value of attribute pid.
-
#reload_scheduled ⇒ Object
Returns the value of attribute reload_scheduled.
-
#sig_reload ⇒ Object
readonly
Returns the value of attribute sig_reload.
-
#sig_term ⇒ Object
readonly
Returns the value of attribute sig_term.
Instance Method Summary collapse
-
#initialize(command, sig_reload: 'HUP', sig_term: 'TERM') ⇒ ProcessHandler
constructor
A new instance of ProcessHandler.
- #kill ⇒ Object
- #process_status ⇒ Object
- #reload ⇒ Object
- #start ⇒ Object
Constructor Details
#initialize(command, sig_reload: 'HUP', sig_term: 'TERM') ⇒ ProcessHandler
Returns a new instance of ProcessHandler.
12 13 14 15 16 17 18 19 20 21 22 |
# 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 |
Instance Attribute Details
#command ⇒ Object (readonly)
Returns the value of attribute command.
10 11 12 |
# File 'lib/consul/async/process_handler.rb', line 10 def command @command end |
#exit_status ⇒ Object (readonly)
Returns the value of attribute exit_status.
10 11 12 |
# File 'lib/consul/async/process_handler.rb', line 10 def exit_status @exit_status end |
#last_signal_sent ⇒ Object (readonly)
Returns the value of attribute last_signal_sent.
10 11 12 |
# File 'lib/consul/async/process_handler.rb', line 10 def last_signal_sent @last_signal_sent end |
#pid ⇒ Object (readonly)
Returns the value of attribute pid.
10 11 12 |
# File 'lib/consul/async/process_handler.rb', line 10 def pid @pid end |
#reload_scheduled ⇒ Object
Returns the value of attribute reload_scheduled.
10 11 12 |
# File 'lib/consul/async/process_handler.rb', line 10 def reload_scheduled @reload_scheduled end |
#sig_reload ⇒ Object (readonly)
Returns the value of attribute sig_reload.
10 11 12 |
# File 'lib/consul/async/process_handler.rb', line 10 def sig_reload @sig_reload end |
#sig_term ⇒ Object (readonly)
Returns the value of attribute sig_term.
10 11 12 |
# File 'lib/consul/async/process_handler.rb', line 10 def sig_term @sig_term end |
Instance Method Details
#kill ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# 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 ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 |
# 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.}", e.backtrace end end |
#reload ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 |
# 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 ⇒ Object
24 25 26 27 28 29 |
# 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 |