#! /bin/bash
# Copyright (c) 2009 SUSE Linux AG, Nuernberg, Germany.
# All rights reserved.
#
# Author: Adrian Schroeter
# Please send feedback to http://www.suse.de/feedback/
#
# /etc/init.d/obsapidelayed
#   and its symbolic link
# /(usr/)sbin/rcobsapidelayed
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program; if not, write to the Free Software
#    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
### BEGIN INIT INFO
# Provides:          obsapidelayed
# Required-Start:    $network $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Should-Start:      obsapisetup
# Should-Stop:       $none
# Default-Start:     3 5
# Default-Stop:      0 1 2 6
# Short-Description: OBS api delayed jobs
# Description:       Start the delayed job handler for OBS api
### END INIT INFO

. /etc/rc.status

API_ROOT=/srv/www/obs/api
CLOCKWORKD=/usr/bin/clockworkd.ruby2.5

# If you are using newrelic_rpm you need this
# to fix detection of our delay_job.api daemons
export NEW_RELIC_DISPATCHER=delayed_job

function run_in_api
{
  export RAILS_ENV="production"
  # startproc only works for classical daemons
  chroot --userspec=wwwrun:www / /bin/bash -c "cd $API_ROOT && /usr/bin/bundle.ruby2.5 exec $*"
}

# number of parallel delayed jobs
NUM=3

# Reset status of this service
rc_reset

case "$1" in
    start)
        echo -n "Starting OBS api delayed job handler "
        run_in_api script/delayed_job.api.rb --queue=quick start -n $NUM
        run_in_api script/delayed_job.api.rb --queue=releasetracking start -i 1000
        run_in_api script/delayed_job.api.rb --queue=issuetracking start -i 1010
        run_in_api script/delayed_job.api.rb --queue=mailers start -i 1020
        # The default queue used by ActiveJob (jobs scheduled with .perform_later)
        run_in_api script/delayed_job.api.rb --queue=default start -i 1030
        run_in_api script/delayed_job.api.rb --queue=project_log_rotate start -i 1040
        run_in_api script/delayed_job.api.rb --queue=consistency_check start -i 1050
        rc_status -v
        echo -n "Starting OBS searchd daemon "
        FILE_SIZE=`stat -c '%s' /srv/www/obs/api/config/production.sphinx.conf`
        if [ $FILE_SIZE -eq 0 ];then
          run_in_api rails.ruby2.5 ts:rebuild
        else
          run_in_api rails.ruby2.5 ts:start
        fi
        rc_status -v
        echo -n "Starting OBS api clock daemon "
        run_in_api $CLOCKWORKD --log-dir=log -l -c config/clock.rb start
        rc_status -v
        ;;
    stop)
        echo -n "Shutting down OBS api delayed job handler "
        run_in_api script/delayed_job.api.rb --queue=quick stop -n $NUM
        run_in_api script/delayed_job.api.rb --queue=releasetracking stop -i 1000
        run_in_api script/delayed_job.api.rb --queue=issuetracking stop -i 1010
        run_in_api script/delayed_job.api.rb --queue=mailers stop -i 1020
        run_in_api script/delayed_job.api.rb --queue=default stop -i 1030
        run_in_api script/delayed_job.api.rb --queue=project_log_rotate stop -i 1040
        run_in_api script/delayed_job.api.rb --queue=consistency_check stop -i 1050
        rc_status -v
        echo -n "Shutting down OBS api clock daemon "
        run_in_api $CLOCKWORKD -l -c config/clock.rb stop
        rc_status -v
        echo -n "Shutting down OBS searchd daemon "
        run_in_api rails.ruby2.5 ts:stop
        rc_status -v
        ;;
    try-restart|condrestart)
        if test "$1" = "condrestart"; then
            echo "${attn} Use try-restart ${done}(LSB)${attn} rather than condrestart ${warn}(RH)${norm}"
        fi
        $0 status
        if test $? = 0; then
            $0 restart
        else
            rc_reset # Not running is not a failure.
        fi
        rc_status
        ;;
    clean-restart)
        $0 stop
        rm -f $API_ROOT/db/sphinx/production/*
        $0 start
        run_in_api rails.ruby2.5 ts:index

        rc_status
        ;;
    restart)
        $0 stop
        $0 start

        rc_status
        ;;
    force-reload)
        echo -n "Reload service OBS api delayed jobs "
        $0 try-restart
        rc_status
        ;;
    reload)
        ## Not supported
        rc_failed 3
        rc_status -v
        ;;
    status)
        echo -n "Checking for service delayed OBS api jobs "
        checkproc delayed_job.0
        [ $? == $NUM ]
        rc_status -v
        ;;
    *)
        echo "Usage: $0 {start|stop|status|try-restart|restart|force-reload|reload}"
        exit 1
        ;;
esac
rc_exit
