feat: handle interrupt signal
This commit is contained in:
parent
d3982daed1
commit
6b15ee0684
1 changed files with 12 additions and 3 deletions
15
src/main.cr
15
src/main.cr
|
@ -55,15 +55,17 @@ puts "docker_image = #{docker_image}"
|
|||
puts "action = #{action}"
|
||||
|
||||
docker_name = "docmachine-#{basehash}"
|
||||
docker_cid = %x{docker ps -f "name=#{docker_name} -q)}.strip
|
||||
docker_cid = %x{docker ps -f "name=#{docker_name}" -q}.strip
|
||||
uid = %x{id -u}.strip
|
||||
gid = %x{id -g}.strip
|
||||
puts "uid: #{uid}"
|
||||
puts "cid: #{gid}"
|
||||
|
||||
puts "docker_name: #{docker_name}"
|
||||
puts "docker_cid: #{docker_cid}"
|
||||
|
||||
if !docker_cid.empty?
|
||||
Process.run("docker", ["kill", docker_cid])
|
||||
Process.run("docker", ["kill", docker_name])
|
||||
end
|
||||
|
||||
docker_opts << "run"
|
||||
|
@ -127,5 +129,12 @@ docker_opts << docker_image
|
|||
docker_opts << action
|
||||
|
||||
puts docker_opts.inspect.colorize(:yellow)
|
||||
process = Process.run("docker", docker_opts, output: STDOUT, error: STDERR)
|
||||
process = Process.new("docker", docker_opts, output: STDOUT, error: STDERR)
|
||||
|
||||
Signal::INT.trap do
|
||||
STDERR.puts "Received CTRL-C"
|
||||
process.signal(Signal::KILL)
|
||||
Process.run("docker", ["kill", docker_name])
|
||||
end
|
||||
process.wait
|
||||
|
||||
|
|
Loading…
Reference in a new issue