Created
July 14, 2022 14:11
-
-
Save daronco/62a3bf0dc81d44e81969c9bf74d6d871 to your computer and use it in GitHub Desktop.
A script to be used as a preStop hook on kubernetes for resque workers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# This script issues a SIGQUIT to the resque worker and waits for all jobs | |
# (forked processes) to be finished before exiting. To be used as a preStop hook | |
# for resque processes on Kubernetes. | |
# Kubernetes by default issues a SIGTERM when terminating a pod, but that causes | |
# resque to immediately kill child processes and then exit. A SIGQUIT tells resque | |
# to wait for child to finish before exiting. | |
# Set `terminationGracePeriodSeconds` to the number of seconds to wait before | |
# ignoring this preStop script and forcing the pod to terminate. | |
# | |
# References: | |
# * https://gist.github.com/Cryptophobia/447ecea7858d26141359b22161e70ee2 | |
# * https://github.com/resque/resque/tree/v1.25.2#signals | |
set -x | |
kill -QUIT 1; | |
#if resque workers present, wait 10 seconds and check again | |
numWorkers=$(ps aux | grep -i '[p]rocessing' | wc -l); | |
echo "Running the preStop hook"; | |
while true; do | |
if [ "$numWorkers" -eq "0" ]; then | |
#exit when all resque workers are terminated | |
echo "Time to terminate this pod"; | |
break; | |
else | |
echo "Not ready to exit, workers are processing, more sleepy sleepy."; | |
echo "$numWorkers -- number of workers processing."; | |
sleep 5; | |
kill -QUIT 1; | |
numWorkers=$(ps aux | grep -i '[p]rocessing' | wc -l); | |
fi | |
done; | |
echo "Exiting from the bash script"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment