buildkite-plugins / docker-compose-buildkite-plugin

🐳⚡️ Run build scripts, and build + push images, w/ Docker Compose
MIT License
172 stars 139 forks source link

Better signal handling (for Job cancellations) #392

Closed toote closed 1 year ago

toote commented 1 year ago

This adds signal handling on INT, TERM and QUIT on the command hook script so that the main container is stopped gracefully. It also gets logs for the container if not removed (though technically only strictly necessary for compose v1 as v2 still shows the output until the container is killed).

I have tested this with the following ruby code:

begin
  (1..20).each do |x|
    puts "#{x}\n"
    STDOUT.flush
    sleep 10
  end
rescue SignalException => e
  puts  "\nSignal: #{e.signo} / #{e}"
  sleep 2
  exit e.signo
end

Save that file and use docker compose to run the file in a vanilla ruby container. Once it is running, cancel the job.

Without this code, the step cancels immediately, but the main container is still running and (by default) killed during the pre-exit hook. Even when using graceful-termination so that the container is stopped instead, you are missing the actual container output since the job was cancelled and then removed (which is now guaranteed by #386).

With this code, instead, the container gets sent its corresponding stop signal (as defined in its dockerfile)

Closes #389