Skip to content

Commit

Permalink
run.sh installs SIGINT and SIGTERM traps to gracefully stop runner (a…
Browse files Browse the repository at this point in the history
…ctions#2233)

* run.sh installs SIGINT and SIGTERM traps to gracefully stop runner

* replaced trap to force wait for child processes and to send kill to run-helper instead of Runner.Listener

* run with signal handling if RUNNER_MANUALLY_TRAP_SIG is set

* Update src/Misc/layoutroot/run.sh

Co-authored-by: Tingluo Huang <tingluohuang@github.com>

Co-authored-by: Tingluo Huang <tingluohuang@github.com>
  • Loading branch information
nikola-jokic and TingluoHuang authored Nov 2, 2022
1 parent 3e19635 commit d301c06
Showing 1 changed file with 49 additions and 13 deletions.
62 changes: 49 additions & 13 deletions src/Misc/layoutroot/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,52 @@ while [ -h "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symli
[[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE" # if $SOURCE was a relative symlink, we need to resolve it relative to the path where the symlink file was located
done
DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
# run the helper process which keep the listener alive
while :;
do
cp -f "$DIR"/run-helper.sh.template "$DIR"/run-helper.sh
"$DIR"/run-helper.sh $*
returnCode=$?
if [[ $returnCode -eq 2 ]]; then
echo "Restarting runner..."
else
echo "Exiting runner..."
exit 0
fi
done

run() {
# run the helper process which keep the listener alive
while :;
do
cp -f "$DIR"/run-helper.sh.template "$DIR"/run-helper.sh
"$DIR"/run-helper.sh $*
returnCode=$?
if [[ $returnCode -eq 2 ]]; then
echo "Restarting runner..."
else
echo "Exiting runner..."
exit 0
fi
done
}

runWithManualTrap() {
# Set job control
set -m

trap 'kill -INT -$PID' INT TERM

# run the helper process which keep the listener alive
while :;
do
cp -f "$DIR"/run-helper.sh.template "$DIR"/run-helper.sh
"$DIR"/run-helper.sh $* &
PID=$!
wait -f $PID
returnCode=$?
if [[ $returnCode -eq 2 ]]; then
echo "Restarting runner..."
else
echo "Exiting runner..."
# Unregister signal handling before exit
trap - INT TERM
# wait for last parts to be logged
wait $PID
exit 0
fi
done
}

if [[ -z "$RUNNER_MANUALLY_TRAP_SIG" ]]; then
run
else
runWithManualTrap
fi

0 comments on commit d301c06

Please sign in to comment.