/
kill_child.rb
77 lines (67 loc) · 1.79 KB
/
kill_child.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
require 'frontkick'
class SignalHandler
def self.register
@pids ||= []
@main_threads ||= []
if @signal_pipe_r.nil? and @signal_pipe_w.nil?
@signal_pipe_r, @signal_pipe_w = IO.pipe
end
# For example, loogger `can't be called from trap context`
trap :INT do
@signal_pipe_w.puts 'INT'
end
trap :TERM do
@signal_pipe_w.puts 'TERM'
end
# Create a new thread to handle signals
# This example kills child (and self)
@signal_handler_thr ||= Thread.new do
begin
while readable_io = IO.select([@signal_pipe_r])
signal = readable_io.first[0].gets.strip
puts "#{signal} signal received"
@pids.each_with_index do |pid, idx|
main_thread = @main_threads[idx]
begin
puts "kill -#{signal} #{pid}"
Process.kill(signal, pid) rescue nil
rescue => e
main_thread.raise e
end
end
# case signal
# when 'INT'
# Kernel.exit(130)
# when 'TERM'
# Kernel.exit(148)
# end
end
rescue Exception => e
Thread.main.raise e
end
end
end
def self.push(pid)
@pids << pid
@main_threads << Thread.current
end
def self.pop(pid)
@pids -= [pid]
@main_threads -= [Thread.current]
end
end
SignalHandler.register
puts "## Frontkick started"
puts "kill -TERM #{Process.pid} or Hit Ctrl-C in 10 seconds"
result = Frontkick.exec(["sleep 10"]) do |wait_thr|
begin
SignalHandler.push(wait_thr.pid)
puts wait_thr.value # wait child to finish (for normal situation)
ensure
SignalHandler.pop(wait_thr.pid)
end
end
puts "## Fronkick finished"
puts "kill -TERM #{Process.pid} or Hit Ctrl-C in 5 seconds"
sleep 5
puts result.inspect