#!/bin/sh
#
# rc.d script for the vrobbler Celery worker.
#
# PROVIDE: vrobbler_celery_charts
# REQUIRE: NETWORKING postgresql redis
# KEYWORD: shutdown
#
# Installation (on the FreeBSD jail):
#
#   install -m 0555 rc.d/vrobbler_celery_charts /usr/local/etc/rc.d/vrobbler_celery_charts
#   sysrc vrobbler_celery_charts_enable="YES"
#   service vrobbler_celery_charts start
#
# Configuration (via sysrc or /etc/rc.conf):
#
#   vrobbler_celery_charts_queues   - queues to consume      (default: default,charts)
#   vrobbler_celery_charts_bin      - path to celery         (default: /usr/local/bin/celery)
#   vrobbler_celery_charts_user     - user to run as         (default: root)
#   vrobbler_celery_charts_flags    - extra daemon(8) flags
#
# NOTE: do not name the celery path knob "${name}_program".  FreeBSD's
# rc.subr(8) overrides $command with ${name}_program whenever it is set,
# which would replace the daemon(8) wrapper with celery itself.
#
# The command is run under daemon(8) with a supervisor pidfile (-P) and
# restart-on-crash (-R), so the worker is automatically restarted if it dies,
# in the same way immortal used to.  Sending SIGTERM to the supervisor (which
# is what `service vrobbler_celery_charts stop` does) forwards the signal to celery,
# which shuts the worker down gracefully.
#
# The default,charts worker handles on-demand tasks plus per-scrobble chart
# updates.  Companion workers (vrobbler_celery_priority, vrobbler_celery_background)
# consume the priority and background queues with distinct nodenames so they can
# run side by side on one host.
#
. /etc/rc.subr

name="vrobbler_celery_charts"
rcvar="vrobbler_celery_charts_enable"

load_rc_config ${name}

: ${vrobbler_celery_charts_enable:="NO"}
: ${vrobbler_celery_charts_queues:="default,charts"}
: ${vrobbler_celery_charts_bin:="/usr/local/bin/celery"}
: ${vrobbler_celery_charts_autoscale:="2,6"}
: ${vrobbler_log_dir:="/var/log"}

pidfile="/var/run/${name}.pid"

command="/usr/sbin/daemon"
command_args="-f -P ${pidfile} -R 5 -t ${name} ${vrobbler_celery_charts_bin} -A vrobbler worker -Q ${vrobbler_celery_charts_queues} -n default@%h -l info --logfile ${vrobbler_log_dir}/${name}.log"

run_rc_command "$1"
