celeryd.init 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. #!/bin/sh
  2. ### BEGIN INIT INFO
  3. # Provides: celeryd
  4. # Required-Start: $network $local_fs $remote_fs
  5. # Required-Stop: $network $local_fs $remote_fs
  6. # Default-Start: 2 3 4 5
  7. # Default-Stop: 0 1 6
  8. # Short-Description: celery task worker daemon
  9. ### END INIT INFO
  10. #
  11. # ============================================
  12. # celeryd - Starts the Celery worker daemon.
  13. # ============================================
  14. #
  15. # :Usage: /etc/init.d/${basename $0} {start|stop|restart|status}
  16. # :Configuration file: /etc/sysconfig/celeryd
  17. #
  18. # To implement separate init scripts, do NOT copy this script. Instead,
  19. # symlink it. I.e., if my new application, "little-worker" needs an init, I
  20. # should just use:
  21. #
  22. # ln -s /etc/init.d/celeryd /etc/init.d/little-worker
  23. #
  24. # You can then configure this by manipulating /etc/sysconfig/little-worker.
  25. #
  26. # Setting `prog` here allows you to symlink this init script, making it easy
  27. # to run multiple processes on the system.
  28. prog="$(basename $0)"
  29. # Source the centos stuff
  30. . /etc/init.d/functions
  31. # Also look at sysconfig; this is where environmental variables should be set
  32. # on RHEL systems.
  33. [ -f "/etc/sysconfig/$prog" ] && . /etc/sysconfig/$prog
  34. CELERYD=${CELERYD:-"-m celery worker --detach"}
  35. CELERY=${CELERY:-"/usr/bin/celery"}
  36. CELERYD_PID_FILE=${CELERYD_PID_FILE:-"/var/run/celery/$prog.pid"}
  37. CELERYD_LOG_FILE=${CELERYD_LOG_FILE:-"/var/log/celery/$prog.log"}
  38. CELERYD_LOG_LEVEL=${CELERYD_LOG_LEVEL:-"INFO"}
  39. # This is used to change how Celery loads in the configs. It does not need to
  40. # be set to be run.
  41. export CELERY_LOADER
  42. start_workers () {
  43. CELERYD_LOG_DIR=$(dirname $CELERYD_LOG_FILE)
  44. CELERYD_PID_DIR=$(dirname $CELERYD_PID_FILE)
  45. # Ensure that the directories exist.
  46. mkdir -p $CELERYD_LOG_DIR $CELERYD_PID_DIR
  47. # If we specified a user, and/or a group, chown as needed
  48. if [ -n "$CELERYD_USER" ]; then
  49. CHOWN_UG="${CELERYD_USER}"
  50. # If the group is specified, also use that in the chown.
  51. [ -n "$CELERYD_GROUP" ] && CHOWN_UG="$CHOWN_UG:$CELERYD_GROUP"
  52. # Execute the chown on the directory only
  53. chown $CHOWN_UG $CELERYD_LOG_DIR $CELERYD_PID_DIR
  54. CELERYD_OPTS="$CELERYD_OPTS --uid=$CELERYD_USER"
  55. fi
  56. # If we need to be run from a specific location, cd to it before launch
  57. if [ -n "$CELERYD_CHDIR" ]; then
  58. cd $CELERYD_CHDIR
  59. fi
  60. log_message "Starting $prog"
  61. $CELERY multi start $prog \
  62. --pidfile=$CELERYD_PID_FILE \
  63. --logfile=$CELERYD_LOG_FILE \
  64. --loglevel=$CELERYD_LOG_LEVEL \
  65. --cmd="$CELERYD" \
  66. --quiet \
  67. $CELERYD_OPTS
  68. RETVAL=$?
  69. if [ "$RETVAL" == "0" ]; then
  70. touch /var/lock/subsys/$prog
  71. success
  72. else
  73. failure
  74. fi
  75. echo
  76. }
  77. stop_workers () {
  78. echo -n $"Stopping $prog: "
  79. # If we haven't ended, explicitly kill it!
  80. if [ ! -f $CELERYD_PID_FILE ] || [ ! -e /proc/$(cat $CELERYD_PID_FILE) ]; then
  81. failure
  82. echo
  83. return
  84. fi
  85. # According to the latest Celery workers guide (3.0.x+),
  86. # stopping workers should be via TERM signals
  87. celerypids=$(cat $CELERYD_PID_FILE | xargs echo)
  88. echo "Killing PIDs $celerypids"
  89. log_message "Killing PIDs $celerypids ..."
  90. kill -s TERM $celerypids
  91. RETVAL=$?
  92. # SLeep a few seconds to make sure the process really is dead
  93. sleep 3
  94. # Now make sure that the process really does not exist
  95. RETVAL=$(ps h -p $celerypids)
  96. if [ "$RETVAL" == "" ]; then
  97. rm -f /var/lock/subsys/$prog
  98. log_message "SUCCESS killing PIDs $celerypids"
  99. success
  100. else
  101. log_message "FAILED killing PIDs $celerypids"
  102. failure
  103. fi
  104. echo
  105. }
  106. log_message() {
  107. now=$(/bin/date -u "+%Y-%m-%d %H:%M:%S")
  108. echo "[$now:] $1" >> $CELERYD_LOG_FILE
  109. }
  110. case "$1" in
  111. start)
  112. start_workers ;;
  113. stop)
  114. stop_workers ;;
  115. status)
  116. status -p $CELERYD_PID_FILE $prog ;;
  117. restart)
  118. stop_workers
  119. start_workers ;;
  120. *)
  121. echo "Usage: /etc/init.d/$prog {start|stop|restart|status}"
  122. exit 1
  123. ;;
  124. esac
  125. exit 0