lib/foreman/engine.rb in foreman-0.17.0 vs lib/foreman/engine.rb in foreman-0.18.0

- old
+ new

@@ -1,10 +1,11 @@ require "foreman" require "foreman/process" require "foreman/utils" require "pty" require "tempfile" +require "timeout" require "term/ansicolor" require "fileutils" class Foreman::Engine @@ -56,23 +57,23 @@ processes_in_order.each do |name, process| fork process, options, environment end - trap("TERM") { puts "SIGTERM received"; kill_all("TERM") } - trap("INT") { puts "SIGINT received"; kill_all("TERM") } + trap("TERM") { puts "SIGTERM received"; terminate_gracefully } + trap("INT") { puts "SIGINT received"; terminate_gracefully } watch_for_termination end def execute(name, options={}) environment = read_environment(options[:env]) fork processes[name], options, environment - trap("TERM") { puts "SIGTERM received"; kill_all("TERM") } - trap("INT") { puts "SIGINT received"; kill_all("TERM") } + trap("TERM") { puts "SIGTERM received"; terminate_gracefully } + trap("INT") { puts "SIGINT received"; terminate_gracefully } watch_for_termination end def port_for(process, num, base_port=nil) @@ -105,16 +106,16 @@ running_processes[pid] = process end def run(process) proctitle "ruby: foreman #{process.name}" + trap("SIGINT", "IGNORE") begin Dir.chdir directory do - command = process.command - - PTY.spawn("#{process.command} 2>&1") do |stdin, stdout, pid| + PTY.spawn(runner, process.command) do |stdin, stdout, pid| + trap("SIGTERM") { Process.kill("SIGTERM", pid) } until stdin.eof? info stdin.gets, process end end end @@ -124,15 +125,13 @@ rescue Interrupt end end end - def kill_all(signal="TERM") - info "terminating" + def kill_all(signal="SIGTERM") running_processes.each do |pid, process| - info "killing #{process.name} in pid #{pid}" - Process.kill(signal, pid) + Process.kill(signal, pid) rescue Errno::ESRCH end end def info(message, process=nil) print process.color if process @@ -177,12 +176,13 @@ def watch_for_termination pid, status = Process.wait2 process = running_processes.delete(pid) info "process terminated", process + terminate_gracefully kill_all - Process.waitall + rescue Errno::ECHILD end def running_processes @running_processes ||= {} end @@ -213,8 +213,21 @@ end end end environment + end + + def runner + File.expand_path("../../../bin/foreman-runner", __FILE__) + end + + def terminate_gracefully + info "sending SIGTERM to all processes" + kill_all "SIGTERM" + Timeout.timeout(3) { Process.waitall } + rescue Timeout::Error + info "sending SIGKILL to all processes" + kill_all "SIGKILL" end end