From 7888ee8c52964e29ca5e460b7872f73824966d38 Mon Sep 17 00:00:00 2001 From: David Dollar Date: Tue, 22 Jun 2010 16:15:27 -0400 Subject: [PATCH] print to stdout, die when any child process dies --- lib/foreman/engine.rb | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/lib/foreman/engine.rb b/lib/foreman/engine.rb index d7d125d..59dc5ef 100644 --- a/lib/foreman/engine.rb +++ b/lib/foreman/engine.rb @@ -1,5 +1,6 @@ require "foreman" require "foreman/process" +require "pty" require "tempfile" class Foreman::Engine @@ -32,7 +33,7 @@ class Foreman::Engine trap("TERM") { kill_and_exit("TERM") } trap("INT") { kill_and_exit("INT") } - run_loop + watch_for_termination end def screen @@ -69,9 +70,12 @@ private ###################################################################### Dir.chdir directory do FileUtils.mkdir_p "log" command = process.command - command << " >>log/#{process.name}.log 2>&1" if log_to_file - system command - exit $?.exitstatus || 255 + + PTY.spawn("#{process.command} 2>&1") do |stdin, stdout, pid| + until stdin.eof? + info stdin.gets, process + end + end end end @@ -85,7 +89,7 @@ private ###################################################################### end def info(message, process=nil) - puts "[foreman] [#{Time.now.utc}] [#{process ? process.name : "system"}] #{message}" + puts "[#{Time.now.strftime("%H:%M:%S")}] [#{process ? process.name : "system"}] #{message}" end def print_info @@ -103,13 +107,11 @@ private ###################################################################### File.read(procfile) end - def run_loop - while true - pid, status = Process.wait2 - process = running_processes.delete(pid) - info "exited with code #{status}", process - fork process - end + def watch_for_termination + pid, status = Process.wait2 + process = running_processes.delete(pid) + info "process terminated", process + kill_and_exit end def running_processes