Merge pull request #2307 from alphagov/reliable-celery-multi-pids

Handle celery PIDs more reliably
This commit is contained in:
Sakis
2019-01-21 10:49:42 +00:00
committed by GitHub

View File

@@ -54,7 +54,7 @@ function on_exit {
# https://unix.stackexchange.com/a/298942/230401 # https://unix.stackexchange.com/a/298942/230401
PROCESS_COUNT="${#APP_PIDS[@]}" PROCESS_COUNT="${#APP_PIDS[@]}"
if [[ "${PROCESS_COUNT}" -eq "0" ]]; then if [[ "${PROCESS_COUNT}" -eq "0" ]]; then
echo "No more .pid files found, exiting" echo "No celery process is running any more, exiting"
return 0 return 0
fi fi
@@ -66,21 +66,18 @@ function on_exit {
} }
function get_celery_pids { function get_celery_pids {
if [[ $(ls /home/vcap/app/celery*.pid) ]]; then # get the PIDs of the process whose parent is the root process
APP_PIDS=`cat /home/vcap/app/celery*.pid` # print only pid and their command, get the ones with "celery" in their name
else # and keep only these PIDs
APP_PIDS=() APP_PIDS=$(pgrep -P 1 | xargs ps -o pid=,command= -p | grep celery | cut -f1 -d/)
fi
} }
function send_signal_to_celery_processes { function send_signal_to_celery_processes {
# refresh pids to account for the case that some workers may have terminated but others not # refresh pids to account for the case that some workers may have terminated but others not
get_celery_pids get_celery_pids
# send signal to all remaining apps # send signal to all remaining apps
for APP_PID in ${APP_PIDS}; do echo ${APP_PIDS} | tr -d '\n' | tr -s ' ' | xargs echo "Sending signal ${1} to processes with pids: "
echo "Sending signal ${1} to process with pid ${APP_PID}" echo ${APP_PIDS} | xargs kill -s ${1}
kill -s ${1} ${APP_PID} || true
done
} }
function start_application { function start_application {