Commit 18990d74fdb48089c9bcc1596bb1d5695ffd14bf

Authored by Gust
Committed by Sergio Oliveira
1 parent 7c84af70

Add celery service

Signed-off-by: Gustavo Jaruga <darksshades@gmail.com>
Signed-off-by: Luciano Prestes Cavalcanti <lucianopcbr@gmail.com>
vagrant/misc/etc/default/celeryd 0 → 100644
... ... @@ -0,0 +1,28 @@
  1 +# Names of nodes to start
  2 +CELERYD_NODES="worker1"
  3 +
  4 +# Absolute or relative path to the 'celery' command:
  5 +CELERY_BIN="/home/vagrant/.virtualenvs/colab/bin/celery"
  6 +
  7 +# comment out this line if you don't use an app
  8 +CELERY_APP="colab"
  9 +
  10 +# Where to chdir at start.
  11 +CELERYD_CHDIR="/vagrant/"
  12 +
  13 +# Extra command-line arguments to the worker
  14 +CELERYD_OPTS="--time-limit=300 --concurrency=8"
  15 +
  16 +# %N will be replaced with the first part of the nodename.
  17 +CELERYD_LOG_FILE="/var/log/celery/%N.log"
  18 +CELERYD_PID_FILE="/var/run/celery/%N.pid"
  19 +
  20 +# Workers should run as an unprivileged user.
  21 +# You need to create this user manually (or you can choose
  22 +# a user/group combination that already exists, e.g. nobody).
  23 +CELERYD_USER="vagrant"
  24 +CELERYD_GROUP="vagrant"
  25 +
  26 +# If enabled pid and log directories will be created if missing,
  27 +# and owned by the userid/group configured.
  28 +CELERY_CREATE_DIRS=1
... ...
vagrant/misc/etc/init.d/celeryd 0 → 100755
... ... @@ -0,0 +1,395 @@
  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 +
  11 +
  12 +### BEGIN INIT INFO
  13 +# Provides: celeryd
  14 +# Required-Start: $network $local_fs $remote_fs
  15 +# Required-Stop: $network $local_fs $remote_fs
  16 +# Default-Start: 2 3 4 5
  17 +# Default-Stop: 0 1 6
  18 +# Short-Description: celery task worker daemon
  19 +### END INIT INFO
  20 +#
  21 +#
  22 +# To implement separate init scripts, copy this script and give it a different
  23 +# name:
  24 +# I.e., if my new application, "little-worker" needs an init, I
  25 +# should just use:
  26 +#
  27 +# cp /etc/init.d/celeryd /etc/init.d/little-worker
  28 +#
  29 +# You can then configure this by manipulating /etc/default/little-worker.
  30 +#
  31 +VERSION=10.1
  32 +echo "celery init v${VERSION}."
  33 +if [ $(id -u) -ne 0 ]; then
  34 + echo "Error: This program can only be used by the root user."
  35 + echo " Unprivileged users must use the 'celery multi' utility, "
  36 + echo " or 'celery worker --detach'."
  37 + exit 1
  38 +fi
  39 +
  40 +
  41 +# Can be a runlevel symlink (e.g. S02celeryd)
  42 +if [ -L "$0" ]; then
  43 + SCRIPT_FILE=$(readlink "$0")
  44 +else
  45 + SCRIPT_FILE="$0"
  46 +fi
  47 +SCRIPT_NAME="$(basename "$SCRIPT_FILE")"
  48 +
  49 +DEFAULT_USER="celery"
  50 +DEFAULT_PID_FILE="/var/run/celery/%n.pid"
  51 +DEFAULT_LOG_FILE="/var/log/celery/%n.log"
  52 +DEFAULT_LOG_LEVEL="INFO"
  53 +DEFAULT_NODES="celery"
  54 +DEFAULT_CELERYD="-m celery worker --detach"
  55 +
  56 +CELERY_DEFAULTS=${CELERY_DEFAULTS:-"/etc/default/${SCRIPT_NAME}"}
  57 +
  58 +# Make sure executable configuration script is owned by root
  59 +_config_sanity() {
  60 + local path="$1"
  61 + local owner=$(ls -ld "$path" | awk '{print $3}')
  62 + local iwgrp=$(ls -ld "$path" | cut -b 6)
  63 + local iwoth=$(ls -ld "$path" | cut -b 9)
  64 +
  65 + if [ "$(id -u $owner)" != "0" ]; then
  66 + echo "Error: Config script '$path' must be owned by root!"
  67 + echo
  68 + echo "Resolution:"
  69 + echo "Review the file carefully and make sure it has not been "
  70 + echo "modified with mailicious intent. When sure the "
  71 + echo "script is safe to execute with superuser privileges "
  72 + echo "you can change ownership of the script:"
  73 + echo " $ sudo chown root '$path'"
  74 + exit 1
  75 + fi
  76 +
  77 + if [ "$iwoth" != "-" ]; then # S_IWOTH
  78 + echo "Error: Config script '$path' cannot be writable by others!"
  79 + echo
  80 + echo "Resolution:"
  81 + echo "Review the file carefully and make sure it has not been "
  82 + echo "modified with malicious intent. When sure the "
  83 + echo "script is safe to execute with superuser privileges "
  84 + echo "you can change the scripts permissions:"
  85 + echo " $ sudo chmod 640 '$path'"
  86 + exit 1
  87 + fi
  88 + if [ "$iwgrp" != "-" ]; then # S_IWGRP
  89 + echo "Error: Config script '$path' cannot be writable by group!"
  90 + echo
  91 + echo "Resolution:"
  92 + echo "Review the file carefully and make sure it has not been "
  93 + echo "modified with malicious intent. When sure the "
  94 + echo "script is safe to execute with superuser privileges "
  95 + echo "you can change the scripts permissions:"
  96 + echo " $ sudo chmod 640 '$path'"
  97 + exit 1
  98 + fi
  99 +}
  100 +
  101 +if [ -f "$CELERY_DEFAULTS" ]; then
  102 + _config_sanity "$CELERY_DEFAULTS"
  103 + echo "Using config script: $CELERY_DEFAULTS"
  104 + . "$CELERY_DEFAULTS"
  105 +fi
  106 +
  107 +# Sets --app argument for CELERY_BIN
  108 +CELERY_APP_ARG=""
  109 +if [ ! -z "$CELERY_APP" ]; then
  110 + CELERY_APP_ARG="--app=$CELERY_APP"
  111 +fi
  112 +
  113 +CELERYD_USER=${CELERYD_USER:-$DEFAULT_USER}
  114 +
  115 +# Set CELERY_CREATE_DIRS to always create log/pid dirs.
  116 +CELERY_CREATE_DIRS=${CELERY_CREATE_DIRS:-0}
  117 +CELERY_CREATE_RUNDIR=$CELERY_CREATE_DIRS
  118 +CELERY_CREATE_LOGDIR=$CELERY_CREATE_DIRS
  119 +if [ -z "$CELERYD_PID_FILE" ]; then
  120 + CELERYD_PID_FILE="$DEFAULT_PID_FILE"
  121 + CELERY_CREATE_RUNDIR=1
  122 +fi
  123 +if [ -z "$CELERYD_LOG_FILE" ]; then
  124 + CELERYD_LOG_FILE="$DEFAULT_LOG_FILE"
  125 + CELERY_CREATE_LOGDIR=1
  126 +fi
  127 +
  128 +CELERYD_LOG_LEVEL=${CELERYD_LOG_LEVEL:-${CELERYD_LOGLEVEL:-$DEFAULT_LOG_LEVEL}}
  129 +CELERY_BIN=${CELERY_BIN:-"celery"}
  130 +CELERYD_MULTI=${CELERYD_MULTI:-"$CELERY_BIN multi"}
  131 +CELERYD_NODES=${CELERYD_NODES:-$DEFAULT_NODES}
  132 +
  133 +export CELERY_LOADER
  134 +
  135 +if [ -n "$2" ]; then
  136 + CELERYD_OPTS="$CELERYD_OPTS $2"
  137 +fi
  138 +
  139 +CELERYD_LOG_DIR=`dirname $CELERYD_LOG_FILE`
  140 +CELERYD_PID_DIR=`dirname $CELERYD_PID_FILE`
  141 +
  142 +# Extra start-stop-daemon options, like user/group.
  143 +if [ -n "$CELERYD_CHDIR" ]; then
  144 + DAEMON_OPTS="$DAEMON_OPTS --workdir=$CELERYD_CHDIR"
  145 +fi
  146 +
  147 +
  148 +check_dev_null() {
  149 + if [ ! -c /dev/null ]; then
  150 + echo "/dev/null is not a character device!"
  151 + exit 75 # EX_TEMPFAIL
  152 + fi
  153 +}
  154 +
  155 +
  156 +maybe_die() {
  157 + if [ $? -ne 0 ]; then
  158 + echo "Exiting: $* (errno $?)"
  159 + exit 77 # EX_NOPERM
  160 + fi
  161 +}
  162 +
  163 +create_default_dir() {
  164 + if [ ! -d "$1" ]; then
  165 + echo "- Creating default directory: '$1'"
  166 + mkdir -p "$1"
  167 + maybe_die "Couldn't create directory $1"
  168 + echo "- Changing permissions of '$1' to 02755"
  169 + chmod 02755 "$1"
  170 + maybe_die "Couldn't change permissions for $1"
  171 + if [ -n "$CELERYD_USER" ]; then
  172 + echo "- Changing owner of '$1' to '$CELERYD_USER'"
  173 + chown "$CELERYD_USER" "$1"
  174 + maybe_die "Couldn't change owner of $1"
  175 + fi
  176 + if [ -n "$CELERYD_GROUP" ]; then
  177 + echo "- Changing group of '$1' to '$CELERYD_GROUP'"
  178 + chgrp "$CELERYD_GROUP" "$1"
  179 + maybe_die "Couldn't change group of $1"
  180 + fi
  181 + fi
  182 +}
  183 +
  184 +
  185 +check_paths() {
  186 + if [ $CELERY_CREATE_LOGDIR -eq 1 ]; then
  187 + create_default_dir "$CELERYD_LOG_DIR"
  188 + fi
  189 + if [ $CELERY_CREATE_RUNDIR -eq 1 ]; then
  190 + create_default_dir "$CELERYD_PID_DIR"
  191 + fi
  192 +}
  193 +
  194 +create_paths() {
  195 + create_default_dir "$CELERYD_LOG_DIR"
  196 + create_default_dir "$CELERYD_PID_DIR"
  197 +}
  198 +
  199 +export PATH="${PATH:+$PATH:}/usr/sbin:/sbin"
  200 +
  201 +
  202 +_get_pidfiles () {
  203 + # note: multi < 3.1.14 output to stderr, not stdout, hence the redirect.
  204 + ${CELERYD_MULTI} expand "${CELERYD_PID_FILE}" ${CELERYD_NODES} 2>&1
  205 +}
  206 +
  207 +
  208 +_get_pids() {
  209 + found_pids=0
  210 + my_exitcode=0
  211 +
  212 + for pidfile in $(_get_pidfiles); do
  213 + local pid=`cat "$pidfile"`
  214 + local cleaned_pid=`echo "$pid" | sed -e 's/[^0-9]//g'`
  215 + if [ -z "$pid" ] || [ "$cleaned_pid" != "$pid" ]; then
  216 + echo "bad pid file ($pidfile)"
  217 + one_failed=true
  218 + my_exitcode=1
  219 + else
  220 + found_pids=1
  221 + echo "$pid"
  222 + fi
  223 +
  224 + if [ $found_pids -eq 0 ]; then
  225 + echo "${SCRIPT_NAME}: All nodes down"
  226 + exit $my_exitcode
  227 + fi
  228 + done
  229 +}
  230 +
  231 +
  232 +_chuid () {
  233 + su "$CELERYD_USER" -c "$CELERYD_MULTI $*"
  234 +}
  235 +
  236 +
  237 +start_workers () {
  238 + if [ ! -z "$CELERYD_ULIMIT" ]; then
  239 + ulimit $CELERYD_ULIMIT
  240 + fi
  241 + _chuid $* start $CELERYD_NODES $DAEMON_OPTS \
  242 + --pidfile="$CELERYD_PID_FILE" \
  243 + --logfile="$CELERYD_LOG_FILE" \
  244 + --loglevel="$CELERYD_LOG_LEVEL" \
  245 + $CELERY_APP_ARG \
  246 + $CELERYD_OPTS
  247 +}
  248 +
  249 +
  250 +dryrun () {
  251 + (C_FAKEFORK=1 start_workers --verbose)
  252 +}
  253 +
  254 +
  255 +stop_workers () {
  256 + _chuid stopwait $CELERYD_NODES --pidfile="$CELERYD_PID_FILE"
  257 +}
  258 +
  259 +
  260 +restart_workers () {
  261 + _chuid restart $CELERYD_NODES $DAEMON_OPTS \
  262 + --pidfile="$CELERYD_PID_FILE" \
  263 + --logfile="$CELERYD_LOG_FILE" \
  264 + --loglevel="$CELERYD_LOG_LEVEL" \
  265 + $CELERY_APP_ARG \
  266 + $CELERYD_OPTS
  267 +}
  268 +
  269 +
  270 +kill_workers() {
  271 + _chuid kill $CELERYD_NODES --pidfile="$CELERYD_PID_FILE"
  272 +}
  273 +
  274 +
  275 +restart_workers_graceful () {
  276 + echo "WARNING: Use with caution in production"
  277 + echo "The workers will attempt to restart, but they may not be able to."
  278 + local worker_pids=
  279 + worker_pids=`_get_pids`
  280 + [ "$one_failed" ] && exit 1
  281 +
  282 + for worker_pid in $worker_pids; do
  283 + local failed=
  284 + kill -HUP $worker_pid 2> /dev/null || failed=true
  285 + if [ "$failed" ]; then
  286 + echo "${SCRIPT_NAME} worker (pid $worker_pid) could not be restarted"
  287 + one_failed=true
  288 + else
  289 + echo "${SCRIPT_NAME} worker (pid $worker_pid) received SIGHUP"
  290 + fi
  291 + done
  292 +
  293 + [ "$one_failed" ] && exit 1 || exit 0
  294 +}
  295 +
  296 +
  297 +check_status () {
  298 + my_exitcode=0
  299 + found_pids=0
  300 +
  301 + local one_failed=
  302 + for pidfile in $(_get_pidfiles); do
  303 + if [ ! -r $pidfile ]; then
  304 + echo "${SCRIPT_NAME} down: no pidfiles found"
  305 + one_failed=true
  306 + break
  307 + fi
  308 +
  309 + local node=`basename "$pidfile" .pid`
  310 + local pid=`cat "$pidfile"`
  311 + local cleaned_pid=`echo "$pid" | sed -e 's/[^0-9]//g'`
  312 + if [ -z "$pid" ] || [ "$cleaned_pid" != "$pid" ]; then
  313 + echo "bad pid file ($pidfile)"
  314 + one_failed=true
  315 + else
  316 + local failed=
  317 + kill -0 $pid 2> /dev/null || failed=true
  318 + if [ "$failed" ]; then
  319 + echo "${SCRIPT_NAME} (node $node) (pid $pid) is down, but pidfile exists!"
  320 + one_failed=true
  321 + else
  322 + echo "${SCRIPT_NAME} (node $node) (pid $pid) is up..."
  323 + fi
  324 + fi
  325 + done
  326 +
  327 + [ "$one_failed" ] && exit 1 || exit 0
  328 +}
  329 +
  330 +
  331 +case "$1" in
  332 + start)
  333 + check_dev_null
  334 + check_paths
  335 + start_workers
  336 + ;;
  337 +
  338 + stop)
  339 + check_dev_null
  340 + check_paths
  341 + stop_workers
  342 + ;;
  343 +
  344 + reload|force-reload)
  345 + echo "Use restart"
  346 + ;;
  347 +
  348 + status)
  349 + check_status
  350 + ;;
  351 +
  352 + restart)
  353 + check_dev_null
  354 + check_paths
  355 + restart_workers
  356 + ;;
  357 +
  358 + graceful)
  359 + check_dev_null
  360 + restart_workers_graceful
  361 + ;;
  362 +
  363 + kill)
  364 + check_dev_null
  365 + kill_workers
  366 + ;;
  367 +
  368 + dryrun)
  369 + check_dev_null
  370 + dryrun
  371 + ;;
  372 +
  373 + try-restart)
  374 + check_dev_null
  375 + check_paths
  376 + restart_workers
  377 + ;;
  378 +
  379 + create-paths)
  380 + check_dev_null
  381 + create_paths
  382 + ;;
  383 +
  384 + check-paths)
  385 + check_dev_null
  386 + check_paths
  387 + ;;
  388 +
  389 + *)
  390 + echo "Usage: /etc/init.d/${SCRIPT_NAME} {start|stop|restart|graceful|kill|dryrun|create-paths}"
  391 + exit 64 # EX_USAGE
  392 + ;;
  393 +esac
  394 +
  395 +exit 0
... ...
vagrant/provision.sh
... ... @@ -36,3 +36,7 @@ fi
36 36 colab-admin migrate
37 37 colab-admin loaddata /vagrant/tests/test_data.json
38 38  
  39 +# Init.d Celery files
  40 +sudo cp $basedir/vagrant/misc/etc/init.d/celeryd /etc/init.d/
  41 +sudo cp $basedir/vagrant/misc/etc/default/celeryd /etc/default/
  42 +sudo systemctl start celeryd
... ...