#!/bin/sh # ============================================ # celeryd - Starts the Celery worker daemon. # ============================================ # # :Usage: /etc/init.d/celeryd {start|stop|restart|status} # :Configuration file: /etc/sysconfig/celeryd # # See http://docs.celeryproject.org/en/latest/tutorials/daemonizing.html ### 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 # Source the centos service helper functions source /etc/init.d/functions # NOTE: "set -e" does not work with CentOS functions, so DO NOT USE! # the lock file is used by above elper functions (e.g., status) LOCK_FILE=/var/lock/subsys/celeryd # some commands work asyncronously, so we'll wait this many seconds SLEEP_SECONDS=5 DEFAULT_PID_FILE="/var/run/celery/%n.pid" DEFAULT_LOG_FILE="/var/log/celery/%n.log" DEFAULT_LOG_LEVEL="INFO" DEFAULT_NODES="celery" DEFAULT_CELERYD="-m celery.bin.celeryd_detach" CELERY_DEFAULTS=${CELERY_DEFAULTS:-"/etc/sysconfig/celeryd"} test -f "$CELERY_DEFAULTS" && . "$CELERY_DEFAULTS" # Set CELERY_CREATE_DIRS to always create log/pid dirs. CELERY_CREATE_DIRS=${CELERY_CREATE_DIRS:-0} CELERY_CREATE_RUNDIR=$CELERY_CREATE_DIRS CELERY_CREATE_LOGDIR=$CELERY_CREATE_DIRS if [ -z "$CELERYD_PID_FILE" ]; then CELERYD_PID_FILE="$DEFAULT_PID_FILE" CELERY_CREATE_RUNDIR=1 fi if [ -z "$CELERYD_LOG_FILE" ]; then CELERYD_LOG_FILE="$DEFAULT_LOG_FILE" CELERY_CREATE_LOGDIR=1 fi CELERYD_LOG_LEVEL=${CELERYD_LOG_LEVEL:-${CELERYD_LOGLEVEL:-$DEFAULT_LOG_LEVEL}} CELERYD_MULTI=${CELERYD_MULTI:-"celeryd-multi"} CELERYD=${CELERYD:-$DEFAULT_CELERYD} CELERYCTL=${CELERYCTL:="celeryctl"} CELERYD_NODES=${CELERYD_NODES:-$DEFAULT_NODES} # This is used to change how Celery loads in the configs. It does not need to # be set to be run. export CELERY_LOADER if [ -n "$2" ]; then CELERYD_OPTS="$CELERYD_OPTS $2" fi CELERYD_LOG_DIR=`dirname $CELERYD_LOG_FILE` CELERYD_PID_DIR=`dirname $CELERYD_PID_FILE` # Extra start-stop-daemon options, like user/group. if [ -n "$CELERYD_USER" ]; then DAEMON_OPTS="$DAEMON_OPTS --uid=$CELERYD_USER" fi if [ -n "$CELERYD_GROUP" ]; then DAEMON_OPTS="$DAEMON_OPTS --gid=$CELERYD_GROUP" fi if [ -n "$CELERYD_CHDIR" ]; then DAEMON_OPTS="$DAEMON_OPTS --workdir=\"$CELERYD_CHDIR\"" fi check_dev_null() { if [ ! -c /dev/null ]; then echo "/dev/null is not a character device!" exit 75 # EX_TEMPFAIL fi } maybe_die() { if [ $? -ne 0 ]; then echo "Exiting: $* (errno $?)" exit 77 # EX_NOPERM fi } create_default_dir() { if [ ! -d "$1" ]; then echo "- Creating default directory: '$1'" mkdir -p "$1" maybe_die "Couldn't create directory $1" echo "- Changing permissions of '$1' to 02755" chmod 02755 "$1" maybe_die "Couldn't change permissions for $1" if [ -n "$CELERYD_USER" ]; then echo "- Changing owner of '$1' to '$CELERYD_USER'" chown "$CELERYD_USER" "$1" maybe_die "Couldn't change owner of $1" fi if [ -n "$CELERYD_GROUP" ]; then echo "- Changing group of '$1' to '$CELERYD_GROUP'" chgrp "$CELERYD_GROUP" "$1" maybe_die "Couldn't change group of $1" fi fi } check_paths() { if [ $CELERY_CREATE_LOGDIR -eq 1 ]; then create_default_dir "$CELERYD_LOG_DIR" fi if [ $CELERY_CREATE_RUNDIR -eq 1 ]; then create_default_dir "$CELERYD_PID_DIR" fi } create_paths() { create_default_dir "$CELERYD_LOG_DIR" create_default_dir "$CELERYD_PID_DIR" } export PATH="${PATH:+$PATH:}/usr/sbin:/sbin" stop_workers () { # Configuration management packages sometimes issue a "stop" command # in preparation for installing the software for the first time. # In those cases we don't need celeryd-multi to tell us celeryd is # not running. if [ ! -f "$LOCK_FILE" ]; then echo "celeryd is already stopped." success return 0 fi # First, try to nicely shut it down. $CELERYD_MULTI stopwait $CELERYD_NODES --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 $SLEEP_SECONDS # If we haven't ended, explicitly kill it! if [ "$RETVAL" != "0" ] || [ -f "$CELERYD_PID_FILE" ]; then $CELERYD_MULTI stop $CELERYD_NODES -KILL --pidfile="$CELERYD_PID_FILE" --quiet RETVAL=$? sleep $SLEEP_SECONDS fi if [ "$RETVAL" == "0" ]; then rm -f "$LOCK_FILE" success else failure fi } start_workers () { $CELERYD_MULTI start $CELERYD_NODES $DAEMON_OPTS \ --pidfile="$CELERYD_PID_FILE" \ --logfile="$CELERYD_LOG_FILE" \ --loglevel="$CELERYD_LOG_LEVEL" \ --cmd="$CELERYD" \ --quiet \ $CELERYD_OPTS RETVAL=$? if [ "$RETVAL" == "0" ]; then touch "$LOCK_FILE" success else failure fi # Sleep a few seconds to give Celery a chance to initialize itself. # This is useful to prevent scripts following this one from trying to # use Celery too early. sleep $SLEEP_SECONDS } case "$1" in start) check_dev_null check_paths start_workers ;; stop) check_dev_null check_paths stop_workers ;; status) status -p $LOCK_FILE celeryd ;; restart) check_dev_null check_paths stop_workers start_workers ;; *) echo "Usage: /etc/init.d/celeryd {start|stop|restart|status}" exit 64 # EX_USAGE ;; esac exit 0