forked from CAVEconnectome/MaterializationEngine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gracefully_shutdown_celery.sh
28 lines (23 loc) · 1.11 KB
/
gracefully_shutdown_celery.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#!/bin/bash -e
echo "*** WARNING: Celery worker shutdown started"
# prevent the worker accepting new tasks
echo "*** EVENT: Worker not accepting new tasks"
celery control --destination $WORKER_NAME@$HOSTNAME cancel_consumer $QUEUE_NAME
sleep 5
# loop until all active task are finished
echo "*** EVENT: Waiting for remaining tasks to finish"
while : ; do
IS_ACTIVE=$( celery inspect --destination $WORKER_NAME@$HOSTNAME --json active | python3 -c "import json; active_tasks = json.loads(input())['$WORKER_NAME@$HOSTNAME']; print(len(active_tasks))")
if (( $IS_ACTIVE > 0 ))
then
sleep 10
dt=$(date '+%d/%m/%Y %H:%M:%S');
echo -n -e "*** STATUS: Number of tasks being run on ${WORKER_NAME}@${HOSTNAME} is: ${IS_ACTIVE}. Current time: ${dt}, waiting...\r"
else
echo -n -e "*** STATUS: Number of tasks being run on ${WORKER_NAME}@${HOSTNAME} is: ${IS_ACTIVE}. Current time: ${dt}, stopping...\r"
echo "*** EVENT: No active running tasks found, shutting down"
break
fi
done
echo "*** EVENT: Sending kill signal to sidecars..."
touch /home/nginx/tmp/shutdown/kill_sidecar