Handle celery PIDs more reliably

This addresses some problems that existed in the previous approach:

1. There was a race condition that could occur between the time we were
looking for the existence of the .pid files and actually reading them.

2. If for some reason the .pid file was left behind after a process had
died, the script would never know because we do:

    kill -s ${1} ${APP_PID} || true
This commit is contained in:
Athanasios Voutsadakis
2019-01-15 17:42:14 +00:00
parent 8d36d72494
commit 4427827b2f

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 {