#!/bin/sh
#
# start/stop the submit job processor daemon.
#
### BEGIN INIT INFO
# Provides:          job_processor
# Required-Start:    $syslog $time
# Required-Stop:     $syslog $time
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Starts/stops the submit job processor daemon.
# Description:       Starts and stops the submit job processor daemon so that
#                    the submit server knows what remote job status is.
### END INIT INFO

# Source function library.
if [ -f /etc/init.d/functions ]; then
   . /etc/init.d/functions
fi

MONITORPATH=/opt/submit/jobProcessor.py

if ! [ -x ${MONITORPATH} ]; then
   exit 1
fi

case "$1" in
   start)
      if [ -f /sbin/start-stop-daemon ]; then
         echo -n "Starting submit job monitor:"
         echo -n " jobpro" ; pgrep -fl 'python ${MONITORPATH}' > /dev/null 2>&1 || /sbin/start-stop-daemon --start --quiet --chdir /opt/submit --chuid gridman --exec ${MONITORPATH}
      else
         echo -n " jobpro" ; daemon --check jobProcessor --user=gridman ${MONITORPATH}
      fi
      echo "."
      ;;
    stop)
      if [ -f /sbin/start-stop-daemon ]; then
         echo -n "Stopping submit job monitor:"
      fi
      echo -n " jobpro" ; pkill -TERM -u gridman -f "${MONITORPATH}"
      echo "."
      ;;
    restart|force-reload)
      if [ -f /sbin/start-stop-daemon ]; then
         echo -n "Restarting submit job monitor:"
      fi
      echo -n " jobpro"
      pkill -TERM -u gridman -f "${MONITORPATH}"
      if [ -f /sbin/start-stop-daemon ]; then
         /sbin/start-stop-daemon --start --quiet --chdir /opt/submit --chuid gridman --exec ${MONITORPATH}
      else
         daemon --check jobProcessor --user=gridman ${MONITORPATH}
      fi
      echo "."
      ;;
       *)
      echo "Usage: /etc/init.d/jobpro {start|stop|restart|force-reload}"
      exit 1
      ;;
esac

exit 0
