## chip.pro : globals and support macros : -*- sh -*-
## this file contains the tasks for running the phase 0 stage
## these tasks use the book chipPendingImfile

## XXX the cleanup stage probably needs to mimic the chip processing stages, with an exposure and imfile layer
## can we define the cleanup stages in a separate file derived from this file? 

# test for required global variables
check.globals

book init chipPendingImfile
book init chipPendingCleanup

macro chip.status
  book listbook chipPendingImfile
  book listbook chipPendingCleanup
end

macro chip.reset
  book init chipPendingImfile
  book init chipPendingCleanup
end

macro chip.on
  task chip.imfile.load
    active true
  end
  task chip.imfile.run
    active true
  end
  task chip.cleanup.load
    active true
  end
  task chip.cleanup.run
    active true
  end
end

macro chip.off
  task chip.imfile.load
    active false
  end
  task chip.imfile.run
    active false
  end
  task chip.cleanup.load
    active false
  end
  task chip.cleanup.run
    active false
  end
end

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

# select images ready for chip analysis
# new entries are added to chipPendingImfile
# skip already-present entries
task	       chip.imfile.load
  host         local

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

  stdout NULL
  stderr $LOGDIR/chip.imfile.log

  task.exec
    if ($DB:n == 0)
      option DEFAULT
      command chiptool -pendingimfile -limit 20
    else
      # save the DB name for the exit tasks
      option $DB:$chip_DB
      command chiptool -pendingimfile -limit 20 -dbname $DB:$chip_DB
      $chip_DB ++
      if ($chip_DB >= $DB:n) set chip_DB = 0
    end
  end

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

    # delete existing entries in the appropriate pantaskStates
    process_cleanup chipPendingImfile
  end

  # locked list
  task.exit    default
    showcommand failure
  end

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

# run the chip_imfile.pl script on pending images
task	       chip.imfile.run
  periods      -poll $RUNPOLL
  periods      -exec $RUNEXEC
  periods      -timeout 60

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

    book setword chipPendingImfile $pageName pantaskState RUN
    book getword chipPendingImfile $pageName camera -var CAMERA
    book getword chipPendingImfile $pageName exp_id -var EXP_ID
    book getword chipPendingImfile $pageName exp_tag -var EXP_TAG
    book getword chipPendingImfile $pageName chip_id -var CHIP_ID
    book getword chipPendingImfile $pageName workdir -var WORKDIR_TEMPLATE
    book getword chipPendingImfile $pageName class_id -var CLASS_ID
    book getword chipPendingImfile $pageName uri -var URI
    book getword chipPendingImfile $pageName dbname -var DBNAME
    book getword chipPendingImfile $pageName reduction -var REDUCTION

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

    # set the WORKDIR variable
    set.workdir.by.camera $CAMERA $CLASS_ID $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.ch.%s" $WORKDIR $EXP_TAG $EXP_TAG $CHIP_ID

    stdout $LOGDIR/chip.imfile.log
    stderr $LOGDIR/chip.imfile.log

    $run = chip_imfile.pl --exp_id $EXP_ID --chip_id $CHIP_ID --class_id $CLASS_ID --uri $URI --camera $CAMERA --outroot $outroot --redirect-output
    if ("$REDUCTION" != "NULL")
      $run = $run --reduction $REDUCTION
    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

  # default exit status
  task.exit    default
    process_exit chipPendingImfile $options:0 $JOB_STATUS
  end

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

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

# select images ready for chip analysis
# new entries are added to chipPendingImfile
# skip already-present entries
task	       chip.cleanup.load
  host         local

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

  stdout NULL
  stderr $LOGDIR/chip.cleanup.log

  task.exec
    if ($DB:n == 0)
      option DEFAULT
      command chiptool -pendingcleanuprun -limit 20
    else
      # save the DB name for the exit tasks
      option $DB:$chip_cleanup_DB
      command chiptool -pendingcleanuprun -limit 20 -dbname $DB:$chip_cleanup_DB
      $chip_cleanup_DB ++
      if ($chip_cleanup_DB >= $DB:n) set chip_cleanup_DB = 0
    end
  end

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

    # delete existing entries in the appropriate pantaskStates
    process_cleanup chipPendingCleanup
  end

  # locked list
  task.exit    default
    showcommand failure
  end

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

# run the ipp_cleanup.pl script on pending images
task	       chip.cleanup.run
  periods      -poll $RUNPOLL
  periods      -exec $RUNEXEC
  periods      -timeout 60
  active       true

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

    book setword chipPendingCleanup $pageName pantaskState RUN
    book getword chipPendingCleanup $pageName camera -var CAMERA
    book getword chipPendingCleanup $pageName state -var CLEANUP_MODE
    book getword chipPendingCleanup $pageName chip_id -var CHIP_ID
    book getword chipPendingCleanup $pageName dbname -var DBNAME

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

    stdout $LOGDIR/chip.cleanup.log
    stderr $LOGDIR/chip.cleanup.log

    # XXX is everything listed here needed?
    $run = ipp_cleanup.pl --stage chip --stage_id $CHIP_ID --camera $CAMERA --mode $CLEANUP_MODE
    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

  # default exit status
  task.exit    default
    process_exit chipPendingCleanup $options:0 $JOB_STATUS
  end

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