Last active
September 23, 2019 08:22
-
-
Save kavehmz/203fe665e456632f06f0b08d015c7b21 to your computer and use it in GitHub Desktop.
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 | |
set -e | |
COMMAND="$@" | |
INIT_NAME=${INIT_NAME:-myapp} | |
HEALTH_PORT=${HEALTH_PORT:-8080} | |
HEALTH_PATH=${HEALTH_PATH:-ping} | |
HEALTH_PERIOD_SECONDS=${HEALTH_PERIOD_SECONDS:-1} | |
HEALTHY_INDICATOR=${HEALTHY_INDICATOR:-/tmp/ready} | |
APP_STOP_DELAY=${APP_STOP_DELAY:-5} | |
SIGNAL_RECIEVED_INDICATOR=$(mktemp --dry-run /tmp/$INIT_NAME-teminating.XXXXXX) | |
_terminating() { | |
touch ${SIGNAL_RECIEVED_INDICATOR} | |
rm -f ${HEALTHY_INDICATOR} | |
} | |
_term() { | |
ls /tmp | |
echo "Caught SIGTERM signal!" | |
_terminating | |
sleep ${APP_STOP_DELAY} | |
echo "stopping the child ${child}" | |
kill -9 ${child} | |
} | |
_health_check() { | |
while [ ! -f ${SIGNAL_RECIEVED_INDICATOR} ] | |
do | |
# either you check the port (nc version) or you are checking the http call (curl version) | |
# if ! nc -vz locahost ${HEALTH_PORT} >/dev/null 2>&1 | |
if ! curl --fail --silent http://localhost:${HEALTH_PORT}/${HEALTH_PATH} > /dev/null | |
then | |
rm -f ${HEALTHY_INDICATOR} | |
else | |
touch rm -f ${HEALTHY_INDICATOR} | |
fi | |
sleep ${HEALTH_PERIOD_SECONDS} | |
done | |
} | |
trap _term SIGTERM | |
${COMMAND} & | |
child=$! | |
_health_check & | |
wait "${child}" | |
_terminating |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment