Commit 4a4ddcb1903c924f1634081e7bec703dc102ebe4

Authored by Sergio Oliveira
1 parent 13321ea3

Added init script for celery beat

Signed-off-by: Sergio Oliveira <sergio@tracy.com.br>
vagrant/misc/etc/default/celerybeat 0 → 100644
... ... @@ -0,0 +1,22 @@
  1 +# Absolute or relative path to the 'celery' command:
  2 +CELERY_BIN="/home/vagrant/.virtualenvs/colab/bin/celery"
  3 +
  4 +# App instance to use
  5 +# comment out this line if you don't use an app
  6 +CELERY_APP="colab.celery:app"
  7 +
  8 +# Where to chdir at start.
  9 +CELERYBEAT_CHDIR="/vagrant/"
  10 +
  11 +# Extra arguments to celerybeat
  12 +CELERYBEAT_OPTS="--schedule=/var/run/celery/celerybeat-schedule"
  13 +
  14 +CELERTBEAT_LOG_FILE="/var/log/celery/beat.log"
  15 +CELERYBEAT_PID_FILE="/var/run/celery/beat.pid"
  16 +
  17 +CELERYBEAT_USER="vagrant"
  18 +CELERYBEAT_GROUP="CELERYBEAT_GROUP"
  19 +
  20 +# If enabled pid and log directories will be created if missing,
  21 +# and owned by the userid/group configured.
  22 +CELERY_CREATE_DIRS=1
... ...
vagrant/misc/etc/default/celeryd
... ... @@ -5,7 +5,7 @@ CELERYD_NODES=&quot;worker1&quot;
5 5 CELERY_BIN="/home/vagrant/.virtualenvs/colab/bin/celery"
6 6  
7 7 # comment out this line if you don't use an app
8   -CELERY_APP="colab"
  8 +CELERY_APP="colab.celery:app"
9 9  
10 10 # Where to chdir at start.
11 11 CELERYD_CHDIR="/vagrant/"
... ...
vagrant/misc/etc/init.d/celerybeat 0 → 100755
... ... @@ -0,0 +1,318 @@
  1 +#!/bin/sh -e
  2 +# =========================================================
  3 +# celerybeat - Starts the Celery periodic task scheduler.
  4 +# =========================================================
  5 +#
  6 +# :Usage: /etc/init.d/celerybeat {start|stop|force-reload|restart|try-restart|status}
  7 +# :Configuration file: /etc/default/celerybeat or /etc/default/celeryd
  8 +#
  9 +# See http://docs.celeryproject.org/en/latest/tutorials/daemonizing.html#generic-init-scripts
  10 +
  11 +### BEGIN INIT INFO
  12 +# Provides: celerybeat
  13 +# Required-Start: $network $local_fs $remote_fs
  14 +# Required-Stop: $network $local_fs $remote_fs
  15 +# Default-Start: 2 3 4 5
  16 +# Default-Stop: 0 1 6
  17 +# Short-Description: celery periodic task scheduler
  18 +### END INIT INFO
  19 +
  20 +# Cannot use set -e/bash -e since the kill -0 command will abort
  21 +# abnormally in the absence of a valid process ID.
  22 +#set -e
  23 +VERSION=10.1
  24 +echo "celery init v${VERSION}."
  25 +
  26 +if [ $(id -u) -ne 0 ]; then
  27 + echo "Error: This program can only be used by the root user."
  28 + echo " Unpriviliged users must use 'celery beat --detach'"
  29 + exit 1
  30 +fi
  31 +
  32 +
  33 +# May be a runlevel symlink (e.g. S02celeryd)
  34 +if [ -L "$0" ]; then
  35 + SCRIPT_FILE=$(readlink "$0")
  36 +else
  37 + SCRIPT_FILE="$0"
  38 +fi
  39 +SCRIPT_NAME="$(basename "$SCRIPT_FILE")"
  40 +
  41 +# /etc/init.d/celerybeat: start and stop the celery periodic task scheduler daemon.
  42 +
  43 +# Make sure executable configuration script is owned by root
  44 +_config_sanity() {
  45 + local path="$1"
  46 + local owner=$(ls -ld "$path" | awk '{print $3}')
  47 + local iwgrp=$(ls -ld "$path" | cut -b 6)
  48 + local iwoth=$(ls -ld "$path" | cut -b 9)
  49 +
  50 + if [ "$(id -u $owner)" != "0" ]; then
  51 + echo "Error: Config script '$path' must be owned by root!"
  52 + echo
  53 + echo "Resolution:"
  54 + echo "Review the file carefully and make sure it has not been "
  55 + echo "modified with mailicious intent. When sure the "
  56 + echo "script is safe to execute with superuser privileges "
  57 + echo "you can change ownership of the script:"
  58 + echo " $ sudo chown root '$path'"
  59 + exit 1
  60 + fi
  61 +
  62 + if [ "$iwoth" != "-" ]; then # S_IWOTH
  63 + echo "Error: Config script '$path' cannot be writable by others!"
  64 + echo
  65 + echo "Resolution:"
  66 + echo "Review the file carefully and make sure it has not been "
  67 + echo "modified with malicious intent. When sure the "
  68 + echo "script is safe to execute with superuser privileges "
  69 + echo "you can change the scripts permissions:"
  70 + echo " $ sudo chmod 640 '$path'"
  71 + exit 1
  72 + fi
  73 + if [ "$iwgrp" != "-" ]; then # S_IWGRP
  74 + echo "Error: Config script '$path' cannot be writable by group!"
  75 + echo
  76 + echo "Resolution:"
  77 + echo "Review the file carefully and make sure it has not been "
  78 + echo "modified with malicious intent. When sure the "
  79 + echo "script is safe to execute with superuser privileges "
  80 + echo "you can change the scripts permissions:"
  81 + echo " $ sudo chmod 640 '$path'"
  82 + exit 1
  83 + fi
  84 +}
  85 +
  86 +scripts=""
  87 +
  88 +if test -f /etc/default/celeryd; then
  89 + scripts="/etc/default/celeryd"
  90 + _config_sanity /etc/default/celeryd
  91 + . /etc/default/celeryd
  92 +fi
  93 +
  94 +EXTRA_CONFIG="/etc/default/${SCRIPT_NAME}"
  95 +if test -f "$EXTRA_CONFIG"; then
  96 + scripts="$scripts, $EXTRA_CONFIG"
  97 + _config_sanity "$EXTRA_CONFIG"
  98 + . "$EXTRA_CONFIG"
  99 +fi
  100 +
  101 +echo "Using configuration: $scripts"
  102 +
  103 +CELERY_BIN=${CELERY_BIN:-"celery"}
  104 +DEFAULT_USER="celery"
  105 +DEFAULT_PID_FILE="/var/run/celery/beat.pid"
  106 +DEFAULT_LOG_FILE="/var/log/celery/beat.log"
  107 +DEFAULT_LOG_LEVEL="INFO"
  108 +DEFAULT_CELERYBEAT="$CELERY_BIN beat"
  109 +
  110 +CELERYBEAT=${CELERYBEAT:-$DEFAULT_CELERYBEAT}
  111 +CELERYBEAT_LOG_LEVEL=${CELERYBEAT_LOG_LEVEL:-${CELERYBEAT_LOGLEVEL:-$DEFAULT_LOG_LEVEL}}
  112 +
  113 +# Sets --app argument for CELERY_BIN
  114 +CELERY_APP_ARG=""
  115 +if [ ! -z "$CELERY_APP" ]; then
  116 + CELERY_APP_ARG="--app=$CELERY_APP"
  117 +fi
  118 +
  119 +CELERYBEAT_USER=${CELERYBEAT_USER:-${CELERYD_USER:-$DEFAULT_USER}}
  120 +
  121 +# Set CELERY_CREATE_DIRS to always create log/pid dirs.
  122 +CELERY_CREATE_DIRS=${CELERY_CREATE_DIRS:-0}
  123 +CELERY_CREATE_RUNDIR=$CELERY_CREATE_DIRS
  124 +CELERY_CREATE_LOGDIR=$CELERY_CREATE_DIRS
  125 +if [ -z "$CELERYBEAT_PID_FILE" ]; then
  126 + CELERYBEAT_PID_FILE="$DEFAULT_PID_FILE"
  127 + CELERY_CREATE_RUNDIR=1
  128 +fi
  129 +if [ -z "$CELERYBEAT_LOG_FILE" ]; then
  130 + CELERYBEAT_LOG_FILE="$DEFAULT_LOG_FILE"
  131 + CELERY_CREATE_LOGDIR=1
  132 +fi
  133 +
  134 +export CELERY_LOADER
  135 +
  136 +CELERYBEAT_OPTS="$CELERYBEAT_OPTS -f $CELERYBEAT_LOG_FILE -l $CELERYBEAT_LOG_LEVEL"
  137 +
  138 +if [ -n "$2" ]; then
  139 + CELERYBEAT_OPTS="$CELERYBEAT_OPTS $2"
  140 +fi
  141 +
  142 +CELERYBEAT_LOG_DIR=`dirname $CELERYBEAT_LOG_FILE`
  143 +CELERYBEAT_PID_DIR=`dirname $CELERYBEAT_PID_FILE`
  144 +
  145 +# Extra start-stop-daemon options, like user/group.
  146 +
  147 +CELERYBEAT_CHDIR=${CELERYBEAT_CHDIR:-$CELERYD_CHDIR}
  148 +if [ -n "$CELERYBEAT_CHDIR" ]; then
  149 + DAEMON_OPTS="$DAEMON_OPTS --workdir=$CELERYBEAT_CHDIR"
  150 +fi
  151 +
  152 +
  153 +export PATH="${PATH:+$PATH:}/usr/sbin:/sbin"
  154 +
  155 +check_dev_null() {
  156 + if [ ! -c /dev/null ]; then
  157 + echo "/dev/null is not a character device!"
  158 + exit 75 # EX_TEMPFAIL
  159 + fi
  160 +}
  161 +
  162 +maybe_die() {
  163 + if [ $? -ne 0 ]; then
  164 + echo "Exiting: $*"
  165 + exit 77 # EX_NOPERM
  166 + fi
  167 +}
  168 +
  169 +create_default_dir() {
  170 + if [ ! -d "$1" ]; then
  171 + echo "- Creating default directory: '$1'"
  172 + mkdir -p "$1"
  173 + maybe_die "Couldn't create directory $1"
  174 + echo "- Changing permissions of '$1' to 02755"
  175 + chmod 02755 "$1"
  176 + maybe_die "Couldn't change permissions for $1"
  177 + if [ -n "$CELERYBEAT_USER" ]; then
  178 + echo "- Changing owner of '$1' to '$CELERYBEAT_USER'"
  179 + chown "$CELERYBEAT_USER" "$1"
  180 + maybe_die "Couldn't change owner of $1"
  181 + fi
  182 + if [ -n "$CELERYBEAT_GROUP" ]; then
  183 + echo "- Changing group of '$1' to '$CELERYBEAT_GROUP'"
  184 + chgrp "$CELERYBEAT_GROUP" "$1"
  185 + maybe_die "Couldn't change group of $1"
  186 + fi
  187 + fi
  188 +}
  189 +
  190 +check_paths() {
  191 + if [ $CELERY_CREATE_LOGDIR -eq 1 ]; then
  192 + create_default_dir "$CELERYBEAT_LOG_DIR"
  193 + fi
  194 + if [ $CELERY_CREATE_RUNDIR -eq 1 ]; then
  195 + create_default_dir "$CELERYBEAT_PID_DIR"
  196 + fi
  197 +}
  198 +
  199 +
  200 +create_paths () {
  201 + create_default_dir "$CELERYBEAT_LOG_DIR"
  202 + create_default_dir "$CELERYBEAT_PID_DIR"
  203 +}
  204 +
  205 +
  206 +wait_pid () {
  207 + pid=$1
  208 + forever=1
  209 + i=0
  210 + while [ $forever -gt 0 ]; do
  211 + kill -0 $pid 1>/dev/null 2>&1
  212 + if [ $? -eq 1 ]; then
  213 + echo "OK"
  214 + forever=0
  215 + else
  216 + kill -TERM "$pid"
  217 + i=$((i + 1))
  218 + if [ $i -gt 60 ]; then
  219 + echo "ERROR"
  220 + echo "Timed out while stopping (30s)"
  221 + forever=0
  222 + else
  223 + sleep 0.5
  224 + fi
  225 + fi
  226 + done
  227 +}
  228 +
  229 +
  230 +stop_beat () {
  231 + echo -n "Stopping ${SCRIPT_NAME}... "
  232 + if [ -f "$CELERYBEAT_PID_FILE" ]; then
  233 + wait_pid $(cat "$CELERYBEAT_PID_FILE")
  234 + else
  235 + echo "NOT RUNNING"
  236 + fi
  237 +}
  238 +
  239 +_chuid () {
  240 + su "$CELERYBEAT_USER" -c "$CELERYBEAT $*"
  241 +}
  242 +
  243 +start_beat () {
  244 + echo "Starting ${SCRIPT_NAME}..."
  245 + _chuid $CELERY_APP_ARG $CELERYBEAT_OPTS $DAEMON_OPTS --detach \
  246 + --pidfile="$CELERYBEAT_PID_FILE"
  247 +}
  248 +
  249 +
  250 +check_status () {
  251 + local failed=
  252 + local pid_file=$CELERYBEAT_PID_FILE
  253 + if [ ! -e $pid_file ]; then
  254 + echo "${SCRIPT_NAME} is up: no pid file found"
  255 + failed=true
  256 + elif [ ! -r $pid_file ]; then
  257 + echo "${SCRIPT_NAME} is in unknown state, user cannot read pid file."
  258 + failed=true
  259 + else
  260 + local pid=`cat "$pid_file"`
  261 + local cleaned_pid=`echo "$pid" | sed -e 's/[^0-9]//g'`
  262 + if [ -z "$pid" ] || [ "$cleaned_pid" != "$pid" ]; then
  263 + echo "${SCRIPT_NAME}: bad pid file ($pid_file)"
  264 + failed=true
  265 + else
  266 + local failed=
  267 + kill -0 $pid 2> /dev/null || failed=true
  268 + if [ "$failed" ]; then
  269 + echo "${SCRIPT_NAME} (pid $pid) is down, but pid file exists!"
  270 + failed=true
  271 + else
  272 + echo "${SCRIPT_NAME} (pid $pid) is up..."
  273 + fi
  274 + fi
  275 + fi
  276 +
  277 + [ "$failed" ] && exit 1 || exit 0
  278 +}
  279 +
  280 +
  281 +case "$1" in
  282 + start)
  283 + check_dev_null
  284 + check_paths
  285 + start_beat
  286 + ;;
  287 + stop)
  288 + check_paths
  289 + stop_beat
  290 + ;;
  291 + reload|force-reload)
  292 + echo "Use start+stop"
  293 + ;;
  294 + status)
  295 + check_status
  296 + ;;
  297 + restart)
  298 + echo "Restarting celery periodic task scheduler"
  299 + check_paths
  300 + stop_beat
  301 + check_dev_null
  302 + start_beat
  303 + ;;
  304 + create-paths)
  305 + check_dev_null
  306 + create_paths
  307 + ;;
  308 + check-paths)
  309 + check_dev_null
  310 + check_paths
  311 + ;;
  312 + *)
  313 + echo "Usage: /etc/init.d/${SCRIPT_NAME} {start|stop|restart|create-paths|status}"
  314 + exit 64 # EX_USAGE
  315 + ;;
  316 +esac
  317 +
  318 +exit 0
... ...
vagrant/provision.sh
... ... @@ -43,8 +43,9 @@ colab-admin migrate
43 43 colab-admin loaddata /vagrant/tests/test_data.json
44 44  
45 45 # Init.d Celery files
46   -sudo cp $basedir/vagrant/misc/etc/init.d/celeryd /etc/init.d/
47   -sudo cp $basedir/vagrant/misc/etc/default/celeryd /etc/default/
  46 +sudo cp $basedir/vagrant/misc/etc/init.d/celery* /etc/init.d/
  47 +sudo cp $basedir/vagrant/misc/etc/default/celery* /etc/default/
48 48 sudo service celeryd start
  49 +sudo service celerybeat start
49 50  
50 51 colab-admin rebuild_index --noinput
... ...