#!/bin/bash
# CloudRunner Dispatcher Service
# chkconfig: 345 20 80
# description: CloudRunner Dispatcher service
# processname: /usr/bin/cloudrunner-dsp

NAME=cloudrunner-dsp
DESC="CloudRunner Dispatcher service"
PIDFILE=/var/run/$NAME.pid
SCRIPTNAME=/etc/init.d/cloudrunner-dsp

DAEMON=/usr/bin/cloudrunner-dsp
DAEMONOPTS="--pidfile=$PIDFILE"

case "$1" in
start)
    printf "%-50s" "Starting $NAME..."
    $DAEMON start $DAEMONOPTS 2>&1
    PID=`cat $PIDFILE`
    if [ -z $PID ]; then
        printf "%s\n" "Fail"
    else
        printf "%s\n" "Ok"
    fi
;;
status)
    printf "%-50s" "Checking $NAME..."
    if [ -f $PIDFILE ]; then
        PID=`cat $PIDFILE`
        if [ -z "`ps axf | grep ${PID} | grep -v grep`" ]; then
            printf "%s\n" "Process dead but pidfile exists"
        else
            echo "Running"
        fi
    else
        printf "%s\n" "Service not running"
    fi
;;
stop)
    printf "%-50s" "Stopping $NAME"
    $DAEMON stop $DAEMONOPTS
;;

restart)
    printf "%-50s" "Restarting $NAME"
    $DAEMON restart $DAEMONOPTS
;;

stats)
    printf "%-50s" "Tenant/Node statistics"
    PID=`cat $PIDFILE`
    kill -HUP $PID
;;

*)
    echo "Usage: $0 {status|start|stop|restart}"
    exit 1
esac
