lib/frontkick/command.rb in frontkick-0.4.6 vs lib/frontkick/command.rb in frontkick-0.4.7
- old
+ new
@@ -24,14 +24,22 @@
stdin, out, err, wait_thr = Open3.popen3(*cmd_array)
out_reader = Thread.new { out.read }
err_reader = Thread.new { err.read }
stdin.close
pid = wait_thr.pid
+
+ trapped_signal = nil
+ if opts[:kill_child]
+ trap_signal(pid) {|sig| trapped_signal = sig }
+ end
+
stdout = out_reader.value
stderr = err_reader.value
exit_code = wait_thr.value.exitstatus
process_wait(pid)
+
+ exit_signal(trapped_signal) if trapped_signal
end
end
rescue Frontkick::TimeoutLocal => e
if opts[:timeout_kill]
Process.kill('SIGINT', pid)
@@ -46,9 +54,31 @@
wait_thr.kill if wait_thr and !wait_thr.stop?
lock_fd.flock(File::LOCK_UN) if lock_fd
end
Result.new(:stdout => stdout, :stderr => stderr, :exit_code => exit_code, :duration => duration)
+ end
+
+ def self.trap_signal(pid)
+ trap :INT do
+ Process.kill(:INT, pid)
+ yield(:INT)
+ end
+ trap :TERM do
+ Process.kill(:TERM, pid)
+ yield(:TERM)
+ end
+ end
+
+ def self.exit_signal(signal)
+ case signal
+ when :INT
+ exit(130)
+ when :TERM
+ exit(143)
+ else
+ raise 'Non supported signal'
+ end
end
def self.process_wait(pid)
begin
pid, status = Process.waitpid2(pid) # wait child processes finish