celeryd 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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. _get_pid_files() {
  111. [[ ! -d "$CELERYD_PID_DIR" ]] && return
  112. echo $(ls -1 "$CELERYD_PID_DIR"/*.pid 2> /dev/null)
  113. }
  114. stop() {
  115. local pid_files=$(_get_pid_files)
  116. [[ -z "$pid_files" ]] && echo "celeryd is stopped" && return 0
  117. local one_failed=
  118. for pid_file in $pid_files; do
  119. local pid=$(cat "$pid_file")
  120. echo -n $"Stopping celeryd (pid $pid): "
  121. # killproc comes from 'functions' and brings three nice features:
  122. # 1. sending TERM, sleeping, then sleeping more if needed, then sending KILL
  123. # 2. handling 'success' and 'failure' output
  124. # 3. removes stale pid files, if any remain
  125. killproc -p "$pid_file" -d "$SLEEP_SECONDS" celeryd || one_failed=true
  126. echo
  127. done
  128. [[ "$one_failed" ]] && return 1 || return 0
  129. }
  130. start() {
  131. echo -n $"Starting celeryd: "
  132. # If Celery is already running, bail out
  133. local pid_files=$(_get_pid_files)
  134. if [[ "$pid_files" ]]; then
  135. echo -n $"celeryd is already running. Use 'restart'."
  136. failure
  137. echo
  138. return 1
  139. fi
  140. $CELERYD_MULTI start $CELERYD_NODES $DAEMON_OPTS \
  141. --pidfile="$CELERYD_PID_FILE" \
  142. --logfile="$CELERYD_LOG_FILE" \
  143. --loglevel="$CELERYD_LOG_LEVEL" \
  144. --cmd="$CELERYD" \
  145. --quiet \
  146. $CELERYD_OPTS
  147. if [[ "$?" == "0" ]]; then
  148. # Sleep a few seconds to give Celery a chance to initialize itself.
  149. # This is useful to prevent scripts following this one from trying to
  150. # use Celery (or its pid files) too early.
  151. sleep $SLEEP_SECONDS
  152. pid_files=$(_get_pid_files)
  153. if [[ "$pid_files" ]]; then
  154. for pid_file in $pid_files; do
  155. local node=$(basename "$pid_file" .pid)
  156. local pid=$(cat "$pid_file")
  157. echo
  158. echo -n " $node (pid $pid):"
  159. success
  160. done
  161. echo
  162. return 0
  163. else # celeryd_multi succeeded but no pid files found
  164. failure
  165. fi
  166. else # celeryd_multi did not succeed
  167. failure
  168. fi
  169. echo
  170. return 1
  171. }
  172. check_status() {
  173. local pid_files=$(_get_pid_files)
  174. [[ -z "$pid_files" ]] && echo "celeryd is stopped" && return 1
  175. for pid_file in $pid_files; do
  176. local node=$(basename "$pid_file" .pid)
  177. status -p "$pid_file" $"celeryd (node $node)" || 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 && start
  199. ;;
  200. *)
  201. echo "Usage: /etc/init.d/celeryd {start|stop|restart|status}"
  202. exit 3
  203. ;;
  204. esac
  205. exit $?