celeryd 7.1 KB

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