celeryd 7.3 KB

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