Created
December 6, 2014 02:50
-
-
Save rchrd2/9d1a5dbade6c9ce5c794 to your computer and use it in GitHub Desktop.
celery_wrapper.sh
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
#!/bin/bash | |
generic_handler () { | |
kill -s $1 $PID | |
while kill -0 $PID &>/dev/null | |
do | |
wait $PID | |
done | |
} | |
handle_SIGINT () { | |
generic_handler SIGINT | |
} | |
handle_SIGTERM () { | |
generic_handler SIGTERM | |
} | |
handle_SIGHUP () { | |
generic_handler SIGHUP | |
} | |
# Read environment variables | |
read_env() { | |
# From shoreman (https://github.com/hecticjeff/shoreman/blob/master/shoreman.sh) | |
pushd $1 > /dev/null 2>&1 | |
ENV_FILE=${2:-'.env'} | |
if [ -f $ENV_FILE ]; then | |
while read line || [ -n "$line" ]; do | |
if [[ "$line" == *=* ]]; then | |
eval "export $line" > /dev/null 2>&1 | |
fi | |
done < "$ENV_FILE" | |
fi | |
popd > /dev/null 2>&1 | |
} | |
# ------ main program ----- | |
env_dir= | |
cmd= | |
while getopts e:c: opt; do | |
case $opt in | |
e) | |
env_dir=$OPTARG | |
;; | |
c) | |
cmd=$OPTARG | |
;; | |
esac | |
done | |
## example usage | |
# ./celery_wrapper.sh -e "/path/to/my/project" -c "/path/to/venv/bin/celery worker -Q indexers -P prefork --concurrency 2 --loglevel info" | |
# setup | |
#export C_FORCE_ROOT=true | |
read_env $env_dir | |
cd $env_dir | |
exec $cmd & | |
PID=$! | |
# Trap these signals | |
# Note: Intentionally send SIGTERM when SIGHUP is received | |
# because otherwise it spins up zombie processes | |
trap handle_SIGTERM SIGHUP | |
trap handle_SIGINT SIGINT | |
trap handle_SIGTERM SIGTERM | |
wait $PID |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment