## addstar.pro : globals and support macros : -*- sh -*-
## this file contains the tasks for running the addstar analysis stage
## these tasks use the book addPendingExp

# test for required global variables
check.globals

book init addPendingExp

macro addstar.status
  book listbook addPendingExp
end

macro addstar.reset
  book init addPendingExp
end

macro addstar.on
  task addstar.exp.load
    active true
  end
  task addstar.exp.run
    active true
  end
end

macro addstar.off
  task addstar.exp.load
    active false
  end
  task addstar.exp.run
    active false
  end
end

macro addstar.revert.off
  task addstar.revert
    active false
  end
end
macro addstar.revert.on
  task addstar.revert
    active true
  end
end

# this variable will cycle through the known database names
$addstar_DB = 0
$addstar_revert_DB = 0

# select images ready for addstar analysis
# new entries are added to addPendingExp
# skip already-present entries
task	       addstar.exp.load
  host         local

  periods      -poll $LOADPOLL
  periods      -exec $LOADEXEC
  periods      -timeout 30
  npending     1

  stdout NULL
  stderr $LOGDIR/addstar.exp.log

  task.exec
    if ($LABEL:n == 0) break
    $run = addtool -pendingexp
    if ($DB:n == 0)
      option DEFAULT
    else
      # save the DB name for the exit tasks
      option $DB:$addstar_DB
      $run = $run -dbname $DB:$addstar_DB
      $addstar_DB ++
      if ($addstar_DB >= $DB:n) set addstar_DB = 0
    end
    add_poll_args run
    add_poll_labels run
    command $run
  end

  # success
  task.exit    0
    # convert 'stdout' to book format
    ipptool2book stdout addPendingExp -key add_id -uniq -setword dbname $options:0 -setword pantaskState INIT
    if ($VERBOSE > 2)
      book listbook addPendingExp
    end

    # delete existing entries in the appropriate pantaskStates
    process_cleanup addPendingExp
  end

  # default exit status
  task.exit    default
    showcommand failure
  end

  task.exit    crash
    showcommand crash
  end

  # operation times out?
  task.exit    timeout
    showcommand timeout
  end
end

# run the addstar script on pending exposures
task	       addstar.exp.run
  periods      -poll $RUNPOLL
  periods      -exec $RUNEXEC
  periods      -timeout 1200

  ## we want only a single outstanding addstar job.  
  host         local
  npending     1

  task.exec
    book npages addPendingExp -var N
    if ($N == 0) break
    if ($NETWORK == 0) break
    
    # look for new images in addPendingExp (pantaskState == INIT)
    book getpage addPendingExp 0 -var pageName -key pantaskState INIT
    if ("$pageName" == "NULL") break

    book setword addPendingExp $pageName pantaskState RUN
    book getword addPendingExp $pageName camera -var CAMERA
    book getword addPendingExp $pageName exp_tag -var EXP_TAG
    book getword addPendingExp $pageName add_id -var ADD_ID
    book getword addPendingExp $pageName camroot -var CAMROOT
    book getword addPendingExp $pageName workdir -var WORKDIR_TEMPLATE
    book getword addPendingExp $pageName reduction -var REDUCTION
    book getword addPendingExp $pageName dvodb  -var DVODB
    book getword addPendingExp $pageName image_only -var IMAGE_ONLY
    book getword addPendingExp $pageName dbname -var DBNAME

    # specify choice of remote host based on camera and chip (class_id)
    # set.host.for.camera $CAMERA FPA

    # set the WORKDIR variable
    set.workdir.by.camera $CAMERA FPA $WORKDIR_TEMPLATE $default_host WORKDIR

    # notes on how this works:
    # -- raw workdir examples:
    # file://data/@HOST@.0/gpc1/20080130
    # neb:///@HOST@-vol0/gpc1/20080130 (need to supply volname?, or are we re-defining this each time?)
    # -- out workdir examples:
    # file://data/ipp004.0/gpc1/20080130
    # neb:///ipp004-vol0/gpc1/20080130

    ## generate outroot specific to this exposure (& chip)
    sprintf outroot "%s/%s/%s.add.%s" $WORKDIR $EXP_TAG $EXP_TAG $ADD_ID

    stdout $LOGDIR/addstar.exp.log
    stderr $LOGDIR/addstar.exp.log

    $run = addstar_run.pl --exp_tag $EXP_TAG --add_id $ADD_ID --camera $CAMERA --dvodb $DVODB --camroot $CAMROOT --outroot $outroot --redirect-output
    if ("$REDUCTION" != "NULL")
      $run = $run --reduction $REDUCTION
    end
    if ("$IMAGE_ONLY" == "T")
      $run = $run --image-only
    end
    
    add_standard_args run

    # save the pageName for future reference below
    options $pageName

    # create the command line
    if ($VERBOSE > 1)
      echo command $run
    end
    command $run
  end

  # success
  task.exit default
    process_exit addPendingExp $options:0 $JOB_STATUS
  end

  # locked list
  task.exit    crash
    showcommand crash
    echo "hostname: $JOB_HOSTNAME"
    book setword addPendingExp $options:0 pantaskState CRASH
  end

  # operation times out?
  task.exit    timeout
    showcommand timeout
    book setword addPendingExp $options:0 pantaskState TIMEOUT
  end
end

task addstar.revert
  host         local

  periods      -poll 5.0
  periods      -exec 60.0
  periods      -timeout 1200
  npending     1
  active        false

  stdout NULL
  stderr $LOGDIR/revert.log

  task.exec
    if ($LABEL:n == 0) break
    $run = addtool -revertprocessedexp
    if ($DB:n == 0)
      option DEFAULT
    else
      # save the DB name for the exit tasks
      option $DB:$addstar_revert_DB
      $run = $run -dbname $DB:$addstar_revert_DB
      $addstar_revert_DB ++
      if ($addstar_revert_DB >= $DB:n) set addstar_revert_DB = 0
    end
    add_poll_labels run
    command $run
  end

  # success
  task.exit    0
  end

  # locked list
  task.exit    default
    showcommand failure
  end

  task.exit    crash
    showcommand crash
  end

  # operation times out?
  task.exit    timeout
    showcommand timeout
  end
end
