celeryd.init 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. # Extra start-stop-daemon options, like user/group.
  56. if [ -n "$CELERYD_USER" ]; then
  57. DAEMON_OPTS="$DAEMON_OPTS --uid=$CELERYD_USER"
  58. fi
  59. if [ -n "$CELERYD_GROUP" ]; then
  60. DAEMON_OPTS="$DAEMON_OPTS --gid=$CELERYD_GROUP"
  61. fi
  62. if [ -n "$CELERYD_CHDIR" ]; then
  63. DAEMON_OPTS="$DAEMON_OPTS --workdir=\"$CELERYD_CHDIR\""
  64. fi
  65. check_dev_null() {
  66. if [ ! -c /dev/null ]; then
  67. echo "/dev/null is not a character device!"
  68. exit 75 # EX_TEMPFAIL
  69. fi
  70. }
  71. maybe_die() {
  72. if [ $? -ne 0 ]; then
  73. echo "Exiting: $* (errno $?)"
  74. exit 77 # EX_NOPERM
  75. fi
  76. }
  77. create_default_dir() {
  78. if [ ! -d "$1" ]; then
  79. echo "- Creating default directory: '$1'"
  80. mkdir -p "$1"
  81. maybe_die "Couldn't create directory $1"
  82. echo "- Changing permissions of '$1' to 02755"
  83. chmod 02755 "$1"
  84. maybe_die "Couldn't change permissions for $1"
  85. if [ -n "$CELERYD_USER" ]; then
  86. echo "- Changing owner of '$1' to '$CELERYD_USER'"
  87. chown "$CELERYD_USER" "$1"
  88. maybe_die "Couldn't change owner of $1"
  89. fi
  90. if [ -n "$CELERYD_GROUP" ]; then
  91. echo "- Changing group of '$1' to '$CELERYD_GROUP'"
  92. chgrp "$CELERYD_GROUP" "$1"
  93. maybe_die "Couldn't change group of $1"
  94. fi
  95. fi
  96. }
  97. check_paths() {
  98. if [ $CELERY_CREATE_LOGDIR -eq 1 ]; then
  99. create_default_dir "$CELERYD_LOG_DIR"
  100. fi
  101. if [ $CELERY_CREATE_RUNDIR -eq 1 ]; then
  102. create_default_dir "$CELERYD_PID_DIR"
  103. fi
  104. }
  105. create_paths() {
  106. create_default_dir "$CELERYD_LOG_DIR"
  107. create_default_dir "$CELERYD_PID_DIR"
  108. }
  109. export PATH="${PATH:+$PATH:}/usr/sbin:/sbin"
  110. stop() {
  111. # Configuration management packages sometimes issue a "stop" command
  112. # in preparation for installing the software for the first time.
  113. # In those cases we don't need celeryd-multi to tell us celeryd is
  114. # not running.
  115. [[ ! -d "$CELERYD_PID_DIR" ]] && echo "celeryd is stopped" && exit 0
  116. local pid_files=$(find "$CELERYD_PID_DIR" -name "*.pid")
  117. [[ -z $pid_files ]] && echo "celeryd is stopped" && exit 0
  118. local one_failed=
  119. for pid_file in $pid_files; do
  120. local pid=$(cat "$pid_file")
  121. echo -n $"Stopping celeryd (pid $pid): "
  122. # killproc comes from 'functions' and brings three nice features:
  123. # 1. sending TERM, sleeping, then sleeping more if needed, then sending KILL
  124. # 2. handling 'success' and 'failure' output
  125. # 3. removes stale pid files, if any remain
  126. killproc -p "$pid_file" -d "$SLEEP_SECONDS" celeryd || one_failed=true
  127. echo
  128. done
  129. [[ "$one_failed" ]] && RETVAL=1 || RETVAL=0
  130. }
  131. start() {
  132. echo -n $"Starting celeryd: "
  133. # If Celery is already running, bail out
  134. local pid_files=$(find "$CELERYD_PID_DIR" -name "*.pid")
  135. if [[ "$pid_files" ]]; then
  136. echo -n $"celeryd is already running. Use 'restart'."
  137. failure
  138. echo
  139. exit 1
  140. fi
  141. $CELERYD_MULTI start $CELERYD_NODES $DAEMON_OPTS \
  142. --pidfile="$CELERYD_PID_FILE" \
  143. --logfile="$CELERYD_LOG_FILE" \
  144. --loglevel="$CELERYD_LOG_LEVEL" \
  145. --cmd="$CELERYD" \
  146. --quiet \
  147. $CELERYD_OPTS
  148. RETVAL=$?
  149. if [[ "$RETVAL" == "0" ]]; then
  150. # Sleep a few seconds to give Celery a chance to initialize itself.
  151. # This is useful to prevent scripts following this one from trying to
  152. # use Celery (or its pid files) too early.
  153. sleep $SLEEP_SECONDS
  154. pid_files=$(find "$CELERYD_PID_DIR" -name "*.pid")
  155. if [[ "$pid_files" ]]; then
  156. for pid_file in $pid_files; do
  157. local pid=$(cat "$pid_file")
  158. echo
  159. echo -n " Node (pid $pid):"
  160. success
  161. done
  162. else # celeryd_multi succeeded but no pid files found
  163. failure
  164. RETVAL=1
  165. fi
  166. else
  167. failure
  168. fi
  169. echo
  170. }
  171. check_status() {
  172. [[ ! -d "$CELERYD_PID_DIR" ]] && echo "celeryd is stopped" && exit 1
  173. local pid_files=$(find "$CELERYD_PID_DIR" -name "*.pid")
  174. [[ -z $pid_files ]] && echo "celeryd is stopped" && exit 1
  175. for pid_file in $pid_files; do
  176. local pid_basename=$(basename "$pid_file")
  177. status -p "$pid_file" $"celeryd ($pid_basename)" || return 1 # if one node is down celeryd is down
  178. done
  179. return 0
  180. }
  181. case "$1" in
  182. start)
  183. check_dev_null
  184. check_paths
  185. start
  186. ;;
  187. stop)
  188. check_dev_null
  189. check_paths
  190. stop
  191. ;;
  192. status)
  193. check_status
  194. ;;
  195. restart)
  196. check_dev_null
  197. check_paths
  198. stop
  199. start
  200. ;;
  201. *)
  202. echo "Usage: /etc/init.d/celeryd {start|stop|restart|status}"
  203. exit 3
  204. ;;
  205. esac
  206. exit $RETVAL