File gitorious-poller.init of Package gitorious

#! /bin/sh
#
# Author: Duncan Mac-Vicar P. <dmacvicar@suse.de>
#
### BEGIN INIT INFO
# Provides: gitorious-poller
# Required-Start: $network $syslog
# Required-Stop: $network $syslog
# Default-Start: 3 5
# Default-Stop: 0 1 2 4 6
# Description: Gitorious async queue processor
# Short-Description: Gitorious processor
### END INIT INFO

# Check for existence of needed config file and read it
GITORIOUS_SYSCONFIG=/etc/sysconfig/gitorious
test -r $GITORIOUS_SYSCONFIG || { echo "$GITORIOUS_SYSCONFIG not existing";
        if [ "$1" = "stop" ]; then exit 0;
        else exit 6; fi; }

# Read config
. $GITORIOUS_SYSCONFIG

# Check for missing binaries (stale symlinks should not happen)
# Note: Special treatment of stop for LSB conformance
POLLER_BIN=${GITORIOUS_DIR}/script/poller
test -x $POLLER_BIN || { echo "$POLLER_BIN not installed";
        if [ "$1" = "stop" ]; then exit 0;
        else exit 5; fi; }

START_CMD="cd ${GITORIOUS_DIR} && RAILS_ENV=production script/poller start"
STOP_CMD="cd ${GITORIOUS_DIR} && RAILS_ENV=production script/poller stop"
RESTART_CMD="cd ${GITORIOUS_DIR} && RAILS_ENV=production script/poller restart"

# Shell functions sourced from /etc/rc.status:
#      rc_check         check and set local and overall rc status
#      rc_status        check and set local and overall rc status
#      rc_status -v     ditto but be verbose in local rc status
#      rc_status -v -r  ditto and clear the local rc status
#      rc_failed        set local and overall rc status to failed
#      rc_reset         clear local rc status (overall remains)
#      rc_exit          exit appropriate to overall rc status
. /etc/rc.status

# First reset status of this service
rc_reset

# Return values acc. to LSB for all commands but status:
# 0 - success
# 1 - misc error
# 2 - invalid or excess args
# 3 - unimplemented feature (e.g. reload)
# 4 - insufficient privilege
# 5 - program not installed
# 6 - program not configured
# 7 - program is not running
#
# Note that starting an already running service, stopping
# or restarting a not-running service as well as the restart
# with force-reload (in case signalling is not supported) are
# considered a success.

case "$1" in
    start)
        echo "Starting gitorious poller..."
        /bin/su - $GITORIOUS_USER -c "$START_CMD"
        rc_failed 0
        rc_status -v
        ;;
    stop)
        echo -n "Shutting down gitorious poller"
        /bin/su - $GITORIOUS_USER -c "$STOP_CMD"
        rc_failed 0
        rc_status -v
        ;;
    reload) # no reloading possible, do a restart
        ## Stop the service and regardless of whether it was
        ## running or not, start it again.
        $0 stop
        sleep 2
        $0 start
        # Remember status and be quiet
        rc_status
        ;;
    restart)
        ## Stop the service and regardless of whether it was
        ## running or not, start it again.
        $0 stop
        sleep 2
        $0 start
        # Remember status and be quiet
        rc_status
        ;;
    status)
        echo -n "Checking for git poller"
        if [ `ps -fC ruby | grep poller | grep ${GITORIOUS_USER} | wc -l` -ge 1 ]
        then
           rc_failed 0
          # rc_status -v 0
        else
           rc_failed 3
          # rc_status -v 3
        fi
        rc_status -v
        ;;
    *)
        echo "Usage: $0 {start|stop|restart|reload}"
        exit 1
        ;;
esac
rc_exit