celeryd 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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. #
  19. #
  20. # To implement separate init scripts, do NOT copy this script. Instead,
  21. # symlink it. I.e., if my new application, "little-worker" needs an init, I
  22. # should just use:
  23. #
  24. # ln -s /etc/init.d/celeryd /etc/init.d/little-worker
  25. #
  26. # You can then configure this by manipulating /etc/sysconfig/little-worker.
  27. #
  28. # Setting `prog` here allows you to symlink this init script, making it easy
  29. # to run multiple processes on the system.
  30. # If we're invoked via SysV-style runlevel scripts we need to follow the
  31. # link from rcX.d before working out the script name.
  32. if [[ `dirname $0` == /etc/rc*.d ]]; then
  33. target="$(readlink $0)"
  34. else
  35. target=$0
  36. fi
  37. prog="$(basename $target)"
  38. # Source the centos service helper functions
  39. source /etc/init.d/functions
  40. # NOTE: "set -e" does not work with the above functions,
  41. # which use non-zero return codes as non-error return conditions
  42. # some commands work asyncronously, so we'll wait this many seconds
  43. SLEEP_SECONDS=5
  44. DEFAULT_PID_FILE="/var/run/celery/$prog-%n.pid"
  45. DEFAULT_LOG_FILE="/var/log/celery/$prog-%n%I.log"
  46. DEFAULT_LOG_LEVEL="INFO"
  47. DEFAULT_NODES="celery"
  48. DEFAULT_CELERYD="-m celery.bin.celeryd_detach"
  49. CELERY_DEFAULTS=${CELERY_DEFAULTS:-"/etc/sysconfig/$prog"}
  50. test -f "$CELERY_DEFAULTS" && . "$CELERY_DEFAULTS"
  51. # Set CELERY_CREATE_DIRS to always create log/pid dirs.
  52. CELERY_CREATE_DIRS=${CELERY_CREATE_DIRS:-0}
  53. CELERY_CREATE_RUNDIR=$CELERY_CREATE_DIRS
  54. CELERY_CREATE_LOGDIR=$CELERY_CREATE_DIRS
  55. if [ -z "$CELERYD_PID_FILE" ]; then
  56. CELERYD_PID_FILE="$DEFAULT_PID_FILE"
  57. CELERY_CREATE_RUNDIR=1
  58. fi
  59. if [ -z "$CELERYD_LOG_FILE" ]; then
  60. CELERYD_LOG_FILE="$DEFAULT_LOG_FILE"
  61. CELERY_CREATE_LOGDIR=1
  62. fi
  63. CELERYD_LOG_LEVEL=${CELERYD_LOG_LEVEL:-${CELERYD_LOGLEVEL:-$DEFAULT_LOG_LEVEL}}
  64. CELERYD_MULTI=${CELERYD_MULTI:-"${CELERY_BIN} multi"}
  65. CELERYD=${CELERYD:-$DEFAULT_CELERYD}
  66. CELERYD_NODES=${CELERYD_NODES:-$DEFAULT_NODES}
  67. # This is used to change how Celery loads in the configs. It does not need to
  68. # be set to be run.
  69. export CELERY_LOADER
  70. if [ -n "$2" ]; then
  71. CELERYD_OPTS="$CELERYD_OPTS $2"
  72. fi
  73. CELERYD_LOG_DIR=`dirname $CELERYD_LOG_FILE`
  74. CELERYD_PID_DIR=`dirname $CELERYD_PID_FILE`
  75. CELERYD_OPTS=${CELERYD_OPTS:-"--app=$CELERY_APP"}
  76. # Extra start-stop-daemon options, like user/group.
  77. if [ -n "$CELERYD_USER" ]; then
  78. DAEMON_OPTS="$DAEMON_OPTS --uid=$CELERYD_USER"
  79. fi
  80. if [ -n "$CELERYD_GROUP" ]; then
  81. DAEMON_OPTS="$DAEMON_OPTS --gid=$CELERYD_GROUP"
  82. fi
  83. if [ -n "$CELERYD_CHDIR" ]; then
  84. DAEMON_OPTS="$DAEMON_OPTS --workdir=$CELERYD_CHDIR"
  85. fi
  86. check_dev_null() {
  87. if [ ! -c /dev/null ]; then
  88. echo "/dev/null is not a character device!"
  89. exit 75 # EX_TEMPFAIL
  90. fi
  91. }
  92. maybe_die() {
  93. if [ $? -ne 0 ]; then
  94. echo "Exiting: $* (errno $?)"
  95. exit 77 # EX_NOPERM
  96. fi
  97. }
  98. create_default_dir() {
  99. if [ ! -d "$1" ]; then
  100. echo "- Creating default directory: '$1'"
  101. mkdir -p "$1"
  102. maybe_die "Couldn't create directory $1"
  103. echo "- Changing permissions of '$1' to 02755"
  104. chmod 02755 "$1"
  105. maybe_die "Couldn't change permissions for $1"
  106. if [ -n "$CELERYD_USER" ]; then
  107. echo "- Changing owner of '$1' to '$CELERYD_USER'"
  108. chown "$CELERYD_USER" "$1"
  109. maybe_die "Couldn't change owner of $1"
  110. fi
  111. if [ -n "$CELERYD_GROUP" ]; then
  112. echo "- Changing group of '$1' to '$CELERYD_GROUP'"
  113. chgrp "$CELERYD_GROUP" "$1"
  114. maybe_die "Couldn't change group of $1"
  115. fi
  116. fi
  117. }
  118. check_paths() {
  119. if [ $CELERY_CREATE_LOGDIR -eq 1 ]; then
  120. create_default_dir "$CELERYD_LOG_DIR"
  121. fi
  122. if [ $CELERY_CREATE_RUNDIR -eq 1 ]; then
  123. create_default_dir "$CELERYD_PID_DIR"
  124. fi
  125. }
  126. create_paths() {
  127. create_default_dir "$CELERYD_LOG_DIR"
  128. create_default_dir "$CELERYD_PID_DIR"
  129. }
  130. export PATH="${PATH:+$PATH:}/usr/sbin:/sbin"
  131. _get_pid_files() {
  132. [[ ! -d "$CELERYD_PID_DIR" ]] && return
  133. echo $(ls -1 "$CELERYD_PID_DIR"/$prog-*.pid 2> /dev/null)
  134. }
  135. stop() {
  136. local pid_files=$(_get_pid_files)
  137. [[ -z "$pid_files" ]] && echo "$prog is stopped" && return 0
  138. local one_failed=
  139. for pid_file in $pid_files; do
  140. local pid=$(cat "$pid_file")
  141. echo -n $"Stopping $prog (pid $pid): "
  142. # killproc comes from 'functions' and brings three nice features:
  143. # 1. sending TERM, sleeping, then sleeping more if needed, then sending KILL
  144. # 2. handling 'success' and 'failure' output
  145. # 3. removes stale pid files, if any remain
  146. killproc -p "$pid_file" -d "$SLEEP_SECONDS" $prog || one_failed=true
  147. echo
  148. done
  149. [[ "$one_failed" ]] && return 1 || return 0
  150. }
  151. start() {
  152. echo -n $"Starting $prog: "
  153. # If Celery is already running, bail out
  154. local pid_files=$(_get_pid_files)
  155. if [[ "$pid_files" ]]; then
  156. echo -n $"$prog is already running. Use 'restart'."
  157. failure
  158. echo
  159. return 1
  160. fi
  161. $CELERYD_MULTI start $CELERYD_NODES $DAEMON_OPTS \
  162. --pidfile="$CELERYD_PID_FILE" \
  163. --logfile="$CELERYD_LOG_FILE" \
  164. --loglevel="$CELERYD_LOG_LEVEL" \
  165. --cmd="$CELERYD" \
  166. --quiet \
  167. $CELERYD_OPTS
  168. if [[ "$?" == "0" ]]; then
  169. # Sleep a few seconds to give Celery a chance to initialize itself.
  170. # This is useful to prevent scripts following this one from trying to
  171. # use Celery (or its pid files) too early.
  172. sleep $SLEEP_SECONDS
  173. pid_files=$(_get_pid_files)
  174. if [[ "$pid_files" ]]; then
  175. for pid_file in $pid_files; do
  176. local node=$(basename "$pid_file" .pid)
  177. local pid=$(cat "$pid_file")
  178. echo
  179. echo -n " $node (pid $pid):"
  180. success
  181. done
  182. echo
  183. return 0
  184. else # celeryd_multi succeeded but no pid files found
  185. failure
  186. fi
  187. else # celeryd_multi did not succeed
  188. failure
  189. fi
  190. echo
  191. return 1
  192. }
  193. check_status() {
  194. local pid_files=$(_get_pid_files)
  195. [[ -z "$pid_files" ]] && echo "$prog is stopped" && return 1
  196. for pid_file in $pid_files; do
  197. local node=$(basename "$pid_file" .pid)
  198. status -p "$pid_file" $"$prog (node $node)" || return 1 # if one node is down celeryd is down
  199. done
  200. return 0
  201. }
  202. case "$1" in
  203. start)
  204. check_dev_null
  205. check_paths
  206. start
  207. ;;
  208. stop)
  209. check_dev_null
  210. check_paths
  211. stop
  212. ;;
  213. status)
  214. check_status
  215. ;;
  216. restart)
  217. check_dev_null
  218. check_paths
  219. stop && start
  220. ;;
  221. *)
  222. echo "Usage: /etc/init.d/$prog {start|stop|restart|status}"
  223. exit 3
  224. ;;
  225. esac
  226. exit $?