Skip to content

Commit

Permalink
Merge pull request #392 from buildkite-plugins/toote_signal_handling
Browse files Browse the repository at this point in the history
Better signal handling (for Job cancellations)
  • Loading branch information
pzeballos authored Jun 17, 2023
2 parents 07dd03a + d8b3cc1 commit 73c55e7
Showing 1 changed file with 17 additions and 7 deletions.
24 changes: 17 additions & 7 deletions commands/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -417,21 +417,31 @@ elif [[ ${#command[@]} -gt 0 ]] ; then
done
fi

# Disable -e outside of the subshell; since the subshell returning a failure
# would exit the parent shell (here) early.
set +e
ensure_stopped() {
echo '+++ :warning: Signal received, stopping container'
docker stop "${container_name}" || true
echo '~~~ Last log lines that may be missing above (if container was not already removed)'
docker logs "${container_name}" || true
exitcode='TRAP'
}

trap ensure_stopped SIGINT SIGTERM SIGQUIT

(
# Disable -e to prevent cancelling step if the command fails for whatever reason
set +e
( # subshell is necessary to trap signals (compose v2 fails to stop otherwise)
echo "+++ :docker: Running ${display_command[*]:-} in service $run_service" >&2
run_docker_compose "${run_params[@]}"
)

exitcode=$?

# Restore -e as an option.
set -e

if [[ $exitcode -ne 0 ]] ; then
if [[ $exitcode = "TRAP" ]]; then
# command failed due to cancellation signal, make sure there is an error but no further output
exitcode=-1
elif [[ $exitcode -ne 0 ]] ; then
echo "^^^ +++"
echo "+++ :warning: Failed to run command, exited with $exitcode, run params:"
echo "${run_params[@]}"
Expand All @@ -445,4 +455,4 @@ if [[ -n "${BUILDKITE_AGENT_ACCESS_TOKEN:-}" ]] ; then
fi
fi

return $exitcode
return "$exitcode"

0 comments on commit 73c55e7

Please sign in to comment.