Changeset 527


Ignore:
Timestamp:
Jun 17, 2010, 3:58:14 PM (10 years ago)
Author:
zerodeux
Message:

Refactor the --daemon/start-stop-daemon stuff a bit, hopefully making some sense out of it - see #18

File:
1 edited

Legend:

Unmodified
Added
Removed
  • jobq/jobq

    r525 r527  
    5757  echo "$argv" >.$id && mv .$id $id
    5858 
    59   # Automatically start queue runner
    60   cd - >/dev/null && run_daemon
     59  # Automatically start queue runner in backgorund. Make sure to restore
     60  # caller's cwd so referring to $0 still works.
     61  cd - >/dev/null && queue_runner_daemon
    6162}
    6263
    6364queue_runner() {
     65  echo "queue runner started for user/queue '$LOGNAME/$queue' (pid $$)" | logger -t $PROGRAM
     66
    6467  cd $JOBPATH/$queue
    65 
    66   if [ -z "$debug" ]; then
    67     trap ":" INT HUP
    68     exec </dev/null 2>/dev/null >/dev/null
    69   fi
    7068
    7169  while true; do
     
    106104}
    107105
    108 run_daemon() {
     106queue_runner_daemon() {
     107  # Make sure the queue path exists so we can write our pidfile
     108  mkdir -m 0700 -p $JOBPATH/$queue
    109109  pidfile=$JOBPATH/$queue/.runner
    110   jobqpath="$(cd "$(dirname "$0")"; pwd)/$(basename $0)"
    111110
    112   mkdir -m 0700 -p $JOBPATH/$queue
    113 
    114   /sbin/start-stop-daemon --quiet --make-pidfile --pidfile $pidfile --startas $jobqpath --background --start -- -q $queue -s $debug
    115   if [ $? -eq 1 ]; then
    116     if [ -n "$daemon" ]; then
    117       echo 'Jobq is already running !'
    118     fi
    119   else
    120     echo "queue runner started for user/queue '$LOGNAME/$queue' (pid $pid)" | logger -t $PROGRAM
     111  # - s-s-d does the pidfile mess and hopefully proper locking for us
     112  # - --chdir PWD is needed to invoke $0 because s-s-d chdir to / as a default
     113  /sbin/start-stop-daemon --quiet \
     114    --make-pidfile --pidfile $pidfile \
     115    --chdir "$PWD" \
     116    --background --start --startas "$0" -- \
     117    --runner --queue "$queue"
     118  if [ $? = 1 ]; then
     119    # Daemon is already up, wake it up to process new job immediatly
     120    #kill -USR1 $(cat "$pidfile")  FIXME: to implement, interrupt sleep 5 above with signal
     121    true
     122  fi
     123  if [ $? != 0 ]; then
     124    echo "Error while trying to start queue runner for user/queue '$LOGNAME/$queue'" >&2
    121125  fi
    122126}
     
    137141
    138142Options:
    139   -d, --daemon       Explicitly start queue runner daemon
    140143  -h, --help         Show this help
    141144  -q, --queue QUEUE  Select queue (default name is 'default')
     145  -r, --runner       Explicitly start queue runner (in foreground)
    142146  -v, --version      Show this software revision
    143147EOF
     
    151155
    152156queue=default
    153 daemon=
    154 debug=
     157runner=
    155158
    156159parse_opt=run
    157160while [ $parse_opt != "done"  ] ; do
    158161  case "$1" in
    159     -d|--daemon)  shift; daemon=yes;;
    160     -s)           shift; startqueue=yes;; # internal usage only
    161162    -q|--queue)   shift; queue="$1"; shift;;
     163    -r|--runner)  shift; runner=yes;;
    162164    -h|--help)    help;;
    163165    -v|--version) version;;
    164     -x)           shift; debug="-x";;
    165166
    166167    --) parse_opt=done;;
     
    170171done
    171172
    172 if [ -n "$startqueue" ]; then
     173if [ -n "$runner" ]; then
    173174  queue_runner
    174   exit 0
    175 fi
    176 if [ -n "$daemon" ]; then
    177   run_daemon
    178175  exit 0
    179176fi
Note: See TracChangeset for help on using the changeset viewer.