Last active
October 4, 2018 01:09
-
-
Save brunosimsenhor/55468b5fbbdf01ebad2029b3891bc04b to your computer and use it in GitHub Desktop.
Registering entrypoint
This file contains hidden or 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
| #!/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