#!/bin/sh
### BEGIN INIT INFO
# Provides: celeryd
# Required-Start: $network $local_fs $remote_fs
# Required-Stop: $network $local_fs $remote_fs
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: celery task worker daemon
### END INIT INFO
#
# ============================================
# celeryd - Starts the Celery worker daemon.
# ============================================
#
# :Usage: /etc/init.d/${basename $0} {start|stop|restart|status}
# :Configuration file: /etc/default/celeryd
#
# To implement separate init scripts, do NOT copy this script.  Instead,
# symlink it.  I.e., if my new application, "little-worker" needs an init, I
# should just use:
#
#   ln -s /etc/init.d/celeryd /etc/init.d/little-worker
#
# You can then configure this by manipulating /etc/sysconfig/little-worker.  It
# will still read defaults from /etc/defaults/celeryd, but everything can be
# overriden by sysconfig.
#

# Setting `prog` here allows you to symlink this init script, making it easy
# to run multiple processes on the system.
prog="$(basename $0)"

# Source the centos stuff
. /etc/init.d/functions

# Also look at sysconfig; this is where environmental variables should be set
# on RHEL systems.
[ -f "/etc/sysconfig/$prog" ] && . /etc/sysconfig/$prog

# Start virtual environment
. /home/modilabs/.virtualenvs/bamboo/bin/activate

CELERYD=${CELERYD:-"-m celery.bin.celeryd_detach"}
CELERYD_MULTI=${CELERYD_MULTI:-"celeryd-multi"}
CELERYD_PID_FILE=${CELERYD_PID_FILE:-"celeryd/$prog.pid"}
CELERYD_LOG_FILE=${CELERYD_LOG_FILE:-"celeryd/$prog.log"}
CELERYD_LOG_LEVEL=${CELERYD_LOG_LEVEL:-"INFO"}
CELERYD_LOCK_FILE=celeryd/$prog
CELERYD_OPTS="--config=bamboo.config.celeryconfig_test"

# This is used to change how Celery loads in the configs.  It does not need to
# be set to be run.
export CELERY_LOADER

start_workers () {
    CELERYD_LOG_DIR=$(dirname $CELERYD_LOG_FILE)
    CELERYD_PID_DIR=$(dirname $CELERYD_PID_FILE)
    # Ensure that the directories exist.
    mkdir -p $CELERYD_LOG_DIR $CELERYD_PID_DIR

    # If we specified a user, and/or a group, chown as needed
    if [ -n "$CELERYD_USER" ]; then
        CHOWN_UG="${CELERYD_USER}"

        # If the group is specified, also use that in the chown.
        [ -n "$CELERYD_GROUP" ] && CHOWN_UG="$CHOWN_UG:$CELERYD_GROUP"

        # Execute the chown on the directory only
        chown $CHOWN_UG $CELERYD_LOG_DIR $CELERYD_PID_DIR

        CELERYD_OPTS="$CELERYD_OPTS --uid=$CELERYD_USER"
    fi

    # If we need to be run from a specific location, cd to it before launch
    if [ -n "$CELERYD_CHDIR" ]; then
        cd $CELERYD_CHDIR
    fi

    echo -n $"Starting $prog: "
    $CELERYD_MULTI start $prog \
                               --pidfile=$CELERYD_PID_FILE \
                               --logfile=$CELERYD_LOG_FILE \
                               --loglevel=$CELERYD_LOG_LEVEL \
                               --cmd="$CELERYD" \
			                   --quiet \
                               $CELERYD_OPTS
    RETVAL=$?

    if [ "$RETVAL" == "0" ]; then
        touch $CELERYD_LOCK_FILE
        success
    else
        failure
    fi
    echo
}

stop_workers () {
    echo -n $"Stopping $prog: "

    # If we haven't ended, explicitly kill it!
    if [ ! -f $CELERYD_PID_FILE ] || [ ! -e /proc/$(cat $CELERYD_PID_FILE) ]; then
        failure
	echo
        return
    fi

    # First, try to nicely shut it down.
    $CELERYD_MULTI stop $prog --pidfile=$CELERYD_PID_FILE --quiet
    RETVAL=$?

    # SLeep a few seconds. (this was part of the original script; we can't
    # trust that it will end immediately, or that running the command will
    # stop it.
    sleep 3

    # If we haven't ended, explicitly kill it!
    if [ -f $CELERYD_PID_FILE ] && [ -e /proc/$(cat $CELERYD_PID_FILE) ]; then
        $CELERYD_MULTI stop $prog -KILL --pidfile=$CELERYD_PID_FILE --quiet
    fi

    if [ "$RETVAL" == "0" ]; then
        rm -f /var/lock/sybsys/$prog
        success
    else
        failure
    fi
    echo
}

case "$1" in
    start)
        start_workers ;;
    stop)
        stop_workers ;;
    status)
        status -p $CELERYD_PID_FILE $prog ;;
    restart)
        stop_workers
        start_workers ;;
    *)
        echo "Usage: /etc/init.d/$prog {start|stop|restart|status}"
        exit 1
    ;;
esac

exit 0
