celeryd.init 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. #!/bin/sh
  2. # ============================================
  3. # celeryd - Starts the Celery worker daemon.
  4. # ============================================
  5. #
  6. # :Usage: /etc/init.d/celeryd {start|stop|restart|status}
  7. # :Configuration file: /etc/sysconfig/celeryd
  8. #
  9. # See http://docs.celeryproject.org/en/latest/tutorials/daemonizing.html
  10. ### BEGIN INIT INFO
  11. # Provides: celeryd
  12. # Required-Start: $network $local_fs $remote_fs
  13. # Required-Stop: $network $local_fs $remote_fs
  14. # Default-Start: 2 3 4 5
  15. # Default-Stop: 0 1 6
  16. # Short-Description: celery task worker daemon
  17. ### END INIT INFO
  18. # Source the centos service helper functions
  19. source /etc/init.d/functions
  20. # NOTE: "set -e" does not work with the above functions,
  21. # which use non-zero return codes as non-error return conditions
  22. # some commands work asyncronously, so we'll wait this many seconds
  23. SLEEP_SECONDS=5
  24. DEFAULT_PID_FILE="/var/run/celery/%n.pid"
  25. DEFAULT_LOG_FILE="/var/log/celery/%n.log"
  26. DEFAULT_LOG_LEVEL="INFO"
  27. DEFAULT_NODES="celery"
  28. DEFAULT_CELERYD="-m celery.bin.celeryd_detach"
  29. CELERY_DEFAULTS=${CELERY_DEFAULTS:-"/etc/sysconfig/celeryd"}
  30. test -f "$CELERY_DEFAULTS" && . "$CELERY_DEFAULTS"
  31. # Set CELERY_CREATE_DIRS to always create log/pid dirs.
  32. CELERY_CREATE_DIRS=${CELERY_CREATE_DIRS:-0}
  33. CELERY_CREATE_RUNDIR=$CELERY_CREATE_DIRS
  34. CELERY_CREATE_LOGDIR=$CELERY_CREATE_DIRS
  35. if [ -z "$CELERYD_PID_FILE" ]; then
  36. CELERYD_PID_FILE="$DEFAULT_PID_FILE"
  37. CELERY_CREATE_RUNDIR=1
  38. fi
  39. if [ -z "$CELERYD_LOG_FILE" ]; then
  40. CELERYD_LOG_FILE="$DEFAULT_LOG_FILE"
  41. CELERY_CREATE_LOGDIR=1
  42. fi
  43. CELERYD_LOG_LEVEL=${CELERYD_LOG_LEVEL:-${CELERYD_LOGLEVEL:-$DEFAULT_LOG_LEVEL}}
  44. CELERYD_MULTI=${CELERYD_MULTI:-"celeryd-multi"}
  45. CELERYD=${CELERYD:-$DEFAULT_CELERYD}
  46. CELERYD_NODES=${CELERYD_NODES:-$DEFAULT_NODES}
  47. # This is used to change how Celery loads in the configs. It does not need to
  48. # be set to be run.
  49. export CELERY_LOADER
  50. if [ -n "$2" ]; then
  51. CELERYD_OPTS="$CELERYD_OPTS $2"
  52. fi
  53. CELERYD_LOG_DIR=`dirname $CELERYD_LOG_FILE`
  54. CELERYD_PID_DIR=`dirname $CELERYD_PID_FILE`
  55. # Starting the celeryd service can bring online multiple nodes, each of which running in
  56. # its own process with its own pid file. This is accomplished by means of including the
  57. # wildcard '%n' in the variable CELERYD_PID_FILE. Since CentOS services don't know how
  58. # to deal with a service that may spawn multiple pid files, we will create our own
  59. # single pid file that will contain all pids from worker node processes spawned by Celery.
  60. SERVICE_PID_FILE=${CELERYD_PID_DIR}/celeryd.pid
  61. # Extra start-stop-daemon options, like user/group.
  62. if [ -n "$CELERYD_USER" ]; then
  63. DAEMON_OPTS="$DAEMON_OPTS --uid=$CELERYD_USER"
  64. fi
  65. if [ -n "$CELERYD_GROUP" ]; then
  66. DAEMON_OPTS="$DAEMON_OPTS --gid=$CELERYD_GROUP"
  67. fi
  68. if [ -n "$CELERYD_CHDIR" ]; then
  69. DAEMON_OPTS="$DAEMON_OPTS --workdir=\"$CELERYD_CHDIR\""
  70. fi
  71. check_dev_null() {
  72. if [ ! -c /dev/null ]; then
  73. echo "/dev/null is not a character device!"
  74. exit 75 # EX_TEMPFAIL
  75. fi
  76. }
  77. maybe_die() {
  78. if [ $? -ne 0 ]; then
  79. echo "Exiting: $* (errno $?)"
  80. exit 77 # EX_NOPERM
  81. fi
  82. }
  83. create_default_dir() {
  84. if [ ! -d "$1" ]; then
  85. echo "- Creating default directory: '$1'"
  86. mkdir -p "$1"
  87. maybe_die "Couldn't create directory $1"
  88. echo "- Changing permissions of '$1' to 02755"
  89. chmod 02755 "$1"
  90. maybe_die "Couldn't change permissions for $1"
  91. if [ -n "$CELERYD_USER" ]; then
  92. echo "- Changing owner of '$1' to '$CELERYD_USER'"
  93. chown "$CELERYD_USER" "$1"
  94. maybe_die "Couldn't change owner of $1"
  95. fi
  96. if [ -n "$CELERYD_GROUP" ]; then
  97. echo "- Changing group of '$1' to '$CELERYD_GROUP'"
  98. chgrp "$CELERYD_GROUP" "$1"
  99. maybe_die "Couldn't change group of $1"
  100. fi
  101. fi
  102. }
  103. check_paths() {
  104. if [ $CELERY_CREATE_LOGDIR -eq 1 ]; then
  105. create_default_dir "$CELERYD_LOG_DIR"
  106. fi
  107. if [ $CELERY_CREATE_RUNDIR -eq 1 ]; then
  108. create_default_dir "$CELERYD_PID_DIR"
  109. fi
  110. }
  111. create_paths() {
  112. create_default_dir "$CELERYD_LOG_DIR"
  113. create_default_dir "$CELERYD_PID_DIR"
  114. }
  115. export PATH="${PATH:+$PATH:}/usr/sbin:/sbin"
  116. stop() {
  117. echo -n $"Stopping celeryd: "
  118. # Configuration management packages sometimes issue a "stop" command
  119. # in preparation for installing the software for the first time.
  120. # In those cases we don't need celeryd-multi to tell us celeryd is
  121. # not running.
  122. if [ ! -f "$SERVICE_PID_FILE" ]; then
  123. echo $"already stopped"
  124. return 0
  125. fi
  126. # killproc comes from 'functions' and brings two nice features:
  127. # 1. sending TERM, sleeping, then sleeping more if needed, then sending KILL
  128. # 2. handling 'success' and 'failure' output
  129. killproc -p "$SERVICE_PID_FILE" -d "$SLEEP_SECONDS" celeryd
  130. RETVAL=$?
  131. [[ "$RETVAL" == "0" ]] && rm -f "$SERVICE_PID_FILE"
  132. echo
  133. }
  134. start() {
  135. echo -n $"Starting celeryd: "
  136. # If Celery is already running, bail out
  137. if [[ -f "$SERVICE_PID_FILE" ]]; then
  138. echo -n $"already running. Use 'restart'."
  139. failure
  140. echo
  141. return 1
  142. fi
  143. $CELERYD_MULTI start $CELERYD_NODES $DAEMON_OPTS \
  144. --pidfile="$CELERYD_PID_FILE" \
  145. --logfile="$CELERYD_LOG_FILE" \
  146. --loglevel="$CELERYD_LOG_LEVEL" \
  147. --cmd="$CELERYD" \
  148. --quiet \
  149. $CELERYD_OPTS
  150. RETVAL=$?
  151. if [[ "$RETVAL" == "0" ]]; then
  152. # Sleep a few seconds to give Celery a chance to initialize itself.
  153. # This is useful to prevent scripts following this one from trying to
  154. # use Celery (or its pid files) too early.
  155. sleep $SLEEP_SECONDS
  156. # Collect the pids of all nodes into one service pid file
  157. # (this step will fail if no nodes have started yet)
  158. cat "${CELERYD_PID_DIR}"/* > "$SERVICE_PID_FILE"
  159. fi
  160. [[ -f "$SERVICE_PID_FILE" ]] && success || failure
  161. echo
  162. }
  163. case "$1" in
  164. start)
  165. check_dev_null
  166. check_paths
  167. start
  168. ;;
  169. stop)
  170. check_dev_null
  171. check_paths
  172. stop
  173. ;;
  174. status)
  175. status -p $SERVICE_PID_FILE celeryd
  176. ;;
  177. restart)
  178. check_dev_null
  179. check_paths
  180. stop
  181. start
  182. ;;
  183. *)
  184. echo "Usage: /etc/init.d/celeryd {start|stop|restart|status}"
  185. exit 3
  186. ;;
  187. esac
  188. exit $RETVAL