From 40616f0d7c90141805ddb23dd9aad1f488fba39c Mon Sep 17 00:00:00 2001 From: Harry Waye Date: Thu, 21 Jul 2022 14:32:38 +0100 Subject: [PATCH] chore(dev): clean up background jobs on EXIT and prop. exit code (#10916) We were for instance calling trap at a point where it wouldn't get called, and giving special status to some processes to run in the foreground. Instead we: 1. wait for any process exit 2. use it's exit code for the calling process 3. kill background processes on EXIT --- bin/docker-worker | 9 ++++++++- bin/docker-worker-celery | 11 ++++++++--- bin/plugin-server | 2 +- 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/bin/docker-worker b/bin/docker-worker index 194ca1e7df6..11d090175e4 100755 --- a/bin/docker-worker +++ b/bin/docker-worker @@ -3,5 +3,12 @@ set -e ./bin/migrate-check +# Stop any background jobs on exit +trap 'kill $(jobs -p)' EXIT + ./bin/plugin-server & -./bin/docker-worker-celery --with-scheduler +./bin/docker-worker-celery --with-scheduler & + +# Exit if any processes exit, and exit with it's exit code +wait -n +exit $? diff --git a/bin/docker-worker-celery b/bin/docker-worker-celery index c36511b54e9..1f5166e9524 100755 --- a/bin/docker-worker-celery +++ b/bin/docker-worker-celery @@ -59,6 +59,9 @@ while test $# -gt 0; do esac done +# Stop any background jobs on exit +trap 'kill $(jobs -p)' EXIT + if [ "$with_scheduler" == "true" ]; then ./bin/docker-worker-beat & fi @@ -80,7 +83,9 @@ echo ./bin/migrate-check -SKIP_ASYNC_MIGRATIONS_SETUP=0 celery -A posthog worker ${FLAGS[*]} +SKIP_ASYNC_MIGRATIONS_SETUP=0 celery -A posthog worker ${FLAGS[*]} & + +# Exit if any processes exit, and exit with it's exit code +wait -n +exit $? -# Stop the beat! -trap 'kill $(jobs -p)' EXIT diff --git a/bin/plugin-server b/bin/plugin-server index 96333865d2b..66427756036 100755 --- a/bin/plugin-server +++ b/bin/plugin-server @@ -44,7 +44,7 @@ fi if [[ -n $NO_RESTART_LOOP ]]; then echo "▶️ Starting plugin server..." - trap 'kill -TERM $child 2>/dev/null; while kill -0 $child 2>/dev/null; do sleep 1; done' SIGTERM + trap 'kill -TERM $child 2>/dev/null; while kill -0 $child 2>/dev/null; do sleep 1; done' EXIT yarn $cmd & child=$! wait $child