celeryd 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. #!/bin/sh -e
  2. # ============================================
  3. # celeryd - Starts the Celery worker daemon.
  4. # ============================================
  5. #
  6. # :Usage: /etc/init.d/celeryd {start|stop|force-reload|restart|try-restart|status}
  7. # :Configuration file: /etc/default/celeryd
  8. #
  9. # See http://docs.celeryproject.org/en/latest/tutorials/daemonizing.html#generic-init-scripts
  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/default/little-worker.
  27. #
  28. # If you want to have separate LSB headers in each script you can source this
  29. # script instead of symlinking:
  30. # # ...
  31. # ### END INIT INFO
  32. # source /etc/init.d/celeryd
  33. #
  34. # Setting `SCRIPT_NAME` here allows you to symlink/source this init script,
  35. # making it easy to run multiple processes on the system.
  36. SCRIPT_NAME="$(basename $0)"
  37. # some commands work asyncronously, so we'll wait this many seconds
  38. SLEEP_SECONDS=5
  39. DEFAULT_PID_FILE="/var/run/celery/${SCRIPT_NAME}/%n.pid"
  40. DEFAULT_LOG_FILE="/var/log/celery/${SCRIPT_NAME}/%n.log"
  41. DEFAULT_LOG_LEVEL="INFO"
  42. DEFAULT_NODES="celery"
  43. CELERY_DEFAULTS=${CELERY_DEFAULTS:-"/etc/default/${SCRIPT_NAME}"}
  44. test -f "$CELERY_DEFAULTS" && . "$CELERY_DEFAULTS"
  45. # Sets --app argument for CELERY_BIN
  46. CELERY_APP_ARG=""
  47. if [ ! -z "$CELERY_APP" ]; then
  48. CELERY_APP_ARG="--app=$CELERY_APP"
  49. fi
  50. # Set CELERY_CREATE_DIRS to always create log/pid dirs.
  51. CELERY_CREATE_DIRS=${CELERY_CREATE_DIRS:-0}
  52. CELERY_CREATE_RUNDIR=$CELERY_CREATE_DIRS
  53. CELERY_CREATE_LOGDIR=$CELERY_CREATE_DIRS
  54. if [ -z "$CELERYD_PID_FILE" ]; then
  55. CELERYD_PID_FILE="$DEFAULT_PID_FILE"
  56. CELERY_CREATE_RUNDIR=1
  57. fi
  58. if [ -z "$CELERYD_LOG_FILE" ]; then
  59. CELERYD_LOG_FILE="$DEFAULT_LOG_FILE"
  60. CELERY_CREATE_LOGDIR=1
  61. fi
  62. CELERYD_LOG_LEVEL=${CELERYD_LOG_LEVEL:-${CELERYD_LOGLEVEL:-$DEFAULT_LOG_LEVEL}}
  63. CELERY_BIN=${CELERY_BIN:-"celery"}
  64. CELERYD_MULTI=${CELERYD_MULTI:-"$CELERY_BIN multi"}
  65. CELERYD_NODES=${CELERYD_NODES:-$DEFAULT_NODES}
  66. export CELERY_LOADER
  67. if [ -n "$2" ]; then
  68. CELERYD_OPTS="$CELERYD_OPTS $2"
  69. fi
  70. CELERYD_LOG_DIR=`dirname $CELERYD_LOG_FILE`
  71. CELERYD_PID_DIR=`dirname $CELERYD_PID_FILE`
  72. # Extra start-stop-daemon options, like user/group.
  73. if [ -n "$CELERYD_USER" ]; then
  74. DAEMON_OPTS="$DAEMON_OPTS --uid=$CELERYD_USER"
  75. fi
  76. if [ -n "$CELERYD_GROUP" ]; then
  77. DAEMON_OPTS="$DAEMON_OPTS --gid=$CELERYD_GROUP"
  78. fi
  79. if [ -n "$CELERYD_CHDIR" ]; then
  80. DAEMON_OPTS="$DAEMON_OPTS --workdir=$CELERYD_CHDIR"
  81. fi
  82. check_dev_null() {
  83. if [ ! -c /dev/null ]; then
  84. echo "/dev/null is not a character device!"
  85. exit 75 # EX_TEMPFAIL
  86. fi
  87. }
  88. maybe_die() {
  89. if [ $? -ne 0 ]; then
  90. echo "Exiting: $* (errno $?)"
  91. exit 77 # EX_NOPERM
  92. fi
  93. }
  94. create_default_dir() {
  95. if [ ! -d "$1" ]; then
  96. echo "- Creating default directory: '$1'"
  97. mkdir -p "$1"
  98. maybe_die "Couldn't create directory $1"
  99. echo "- Changing permissions of '$1' to 02755"
  100. chmod 02755 "$1"
  101. maybe_die "Couldn't change permissions for $1"
  102. if [ -n "$CELERYD_USER" ]; then
  103. echo "- Changing owner of '$1' to '$CELERYD_USER'"
  104. chown "$CELERYD_USER" "$1"
  105. maybe_die "Couldn't change owner of $1"
  106. fi
  107. if [ -n "$CELERYD_GROUP" ]; then
  108. echo "- Changing group of '$1' to '$CELERYD_GROUP'"
  109. chgrp "$CELERYD_GROUP" "$1"
  110. maybe_die "Couldn't change group of $1"
  111. fi
  112. fi
  113. }
  114. check_paths() {
  115. if [ $CELERY_CREATE_LOGDIR -eq 1 ]; then
  116. create_default_dir "$CELERYD_LOG_DIR"
  117. fi
  118. if [ $CELERY_CREATE_RUNDIR -eq 1 ]; then
  119. create_default_dir "$CELERYD_PID_DIR"
  120. fi
  121. }
  122. create_paths() {
  123. create_default_dir "$CELERYD_LOG_DIR"
  124. create_default_dir "$CELERYD_PID_DIR"
  125. }
  126. export PATH="${PATH:+$PATH:}/usr/sbin:/sbin"
  127. _get_pid_files() {
  128. [ ! -d "$CELERYD_PID_DIR" ] && return
  129. echo `ls -1 "$CELERYD_PID_DIR"/*.pid 2> /dev/null`
  130. }
  131. _get_pids() {
  132. local pid_files=
  133. pid_files=`_get_pid_files`
  134. [ -z "$pid_files" ] && echo "${SCRIPT_NAME} is stopped" && exit 1
  135. for pid_file in $pid_files; do
  136. local pid=`cat "$pid_file"`
  137. local cleaned_pid=`echo "$pid" | sed -e 's/[^0-9]//g'`
  138. if [ -z "$pid" ] || [ "$cleaned_pid" != "$pid" ]; then
  139. echo "bad pid file ($pid_file)"
  140. one_failed=true
  141. else
  142. echo "$pid"
  143. fi
  144. done
  145. }
  146. _get_worker_pids() {
  147. local pids=
  148. pids=`_get_pids`
  149. local worker_pids=
  150. for pid in $pids; do
  151. worker_pids=`ps h --ppid $pid -o pid`
  152. [ "$worker_pids" ] && echo "$worker_pids" || one_failed=true
  153. done
  154. }
  155. stop_workers () {
  156. $CELERYD_MULTI stopwait $CELERYD_NODES --pidfile="$CELERYD_PID_FILE"
  157. sleep $SLEEP_SECONDS
  158. }
  159. start_workers () {
  160. $CELERYD_MULTI start $CELERYD_NODES $DAEMON_OPTS \
  161. --pidfile="$CELERYD_PID_FILE" \
  162. --logfile="$CELERYD_LOG_FILE" \
  163. --loglevel="$CELERYD_LOG_LEVEL" \
  164. $CELERY_APP_ARG \
  165. $CELERYD_OPTS
  166. sleep $SLEEP_SECONDS
  167. }
  168. restart_workers () {
  169. $CELERYD_MULTI restart $CELERYD_NODES $DAEMON_OPTS \
  170. --pidfile="$CELERYD_PID_FILE" \
  171. --logfile="$CELERYD_LOG_FILE" \
  172. --loglevel="$CELERYD_LOG_LEVEL" \
  173. $CELERY_APP_ARG \
  174. $CELERYD_OPTS
  175. sleep $SLEEP_SECONDS
  176. }
  177. restart_workers_graceful () {
  178. local worker_pids=
  179. worker_pids=`_get_worker_pids`
  180. [ "$one_failed" ] && exit 1
  181. for worker_pid in $worker_pids; do
  182. local failed=
  183. kill -HUP $worker_pid 2> /dev/null || failed=true
  184. if [ "$failed" ]; then
  185. echo "${SCRIPT_NAME} worker (pid $worker_pid) could not be restarted"
  186. one_failed=true
  187. else
  188. echo "${SCRIPT_NAME} worker (pid $worker_pid) received SIGHUP"
  189. fi
  190. done
  191. [ "$one_failed" ] && exit 1 || exit 0
  192. }
  193. check_status () {
  194. local pid_files=
  195. pid_files=`_get_pid_files`
  196. [ -z "$pid_files" ] && echo "${SCRIPT_NAME} not running (no pidfile)" && exit 1
  197. local one_failed=
  198. for pid_file in $pid_files; do
  199. local node=`basename "$pid_file" .pid`
  200. local pid=`cat "$pid_file"`
  201. local cleaned_pid=`echo "$pid" | sed -e 's/[^0-9]//g'`
  202. if [ -z "$pid" ] || [ "$cleaned_pid" != "$pid" ]; then
  203. echo "bad pid file ($pid_file)"
  204. else
  205. local failed=
  206. kill -0 $pid 2> /dev/null || failed=true
  207. if [ "$failed" ]; then
  208. echo "${SCRIPT_NAME} (node $node) (pid $pid) is stopped, but pid file exists!"
  209. one_failed=true
  210. else
  211. echo "${SCRIPT_NAME} (node $node) (pid $pid) is running..."
  212. fi
  213. fi
  214. done
  215. [ "$one_failed" ] && exit 1 || exit 0
  216. }
  217. case "$1" in
  218. start)
  219. check_dev_null
  220. check_paths
  221. start_workers
  222. ;;
  223. stop)
  224. check_dev_null
  225. check_paths
  226. stop_workers
  227. ;;
  228. reload|force-reload)
  229. echo "Use restart"
  230. ;;
  231. status)
  232. check_status
  233. ;;
  234. restart)
  235. check_dev_null
  236. check_paths
  237. restart_workers
  238. ;;
  239. restart-workers-graceful)
  240. check_dev_null
  241. restart_workers_graceful
  242. ;;
  243. try-restart)
  244. check_dev_null
  245. check_paths
  246. restart_workers
  247. ;;
  248. create-paths)
  249. check_dev_null
  250. create_paths
  251. ;;
  252. check-paths)
  253. check_dev_null
  254. check_paths
  255. ;;
  256. *)
  257. echo "Usage: /etc/init.d/${SCRIPT_NAME} {start|stop|restart|restart-workers-graceful|kill|create-paths}"
  258. exit 64 # EX_USAGE
  259. ;;
  260. esac
  261. exit 0