Skip to content

Instantly share code, notes, and snippets.

@brunosimsenhor
Last active October 4, 2018 01:09
Show Gist options
  • Save brunosimsenhor/55468b5fbbdf01ebad2029b3891bc04b to your computer and use it in GitHub Desktop.
Save brunosimsenhor/55468b5fbbdf01ebad2029b3891bc04b to your computer and use it in GitHub Desktop.
Registering entrypoint
#!/usr/bin/env bash
set -x
pid=0
# SIGUSR1-handler (Register handler)
register () {
echo "Registering container $(hostname) with ip $(hostname -i)";
redis-cli -h cache -n 0 hset discovery $(hostname) $(hostname -i);
}
# SIGTERM-handler (Unregister handler)
unregister () {
echo "Unregistering container $(hostname) with ip $(hostname -i)";
redis-cli -h cache -n 0 hdel discovery $(hostname) $(hostname -i);
if [ $pid -ne 0 ]; then
kill -SIGTERM "$pid"
wait "$pid"
fi
exit 143; # 128 + 15 -- SIGTERM
}
# setup handlers
# on callback, kill the last background process, which is `tail -f /dev/null` and execute the specified handler
trap 'kill ${!}; register' SIGUSR1
trap 'kill ${!}; unregister' SIGTERM
# run application
register;
pid="$!"
# wait forever
while true
do
tail -f /dev/null & wait ${!}
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment