
data types:
  @data : queue
  $data : system variable
  
commands
  MESSAGE : send message to system log
  STDOUT  : push task stdout on queue or file
  STDERR  : push task stderr on queue or file


TASK properties:
  EXEC_PERIOD : seconds between attempts
  POLL_PERIOD : seconds between checks of task status
  TIMEOUT     : seconds before task must complete
  HOST        : machine for controller execution
  
  ARG.N       : command and arguments  

  STDIN       : QUEUE source for stdin
  STDOUT      : QUEUE sink for stdout

  PRE_EXEC    : conditions for execution
END  

QUEUE 3 new.images

TASK	       new.images
  COMMAND      new.images
  HOST         -
  STDERR       /data/logfiles/new.images.log
  EXEC_PERIOD  30
  POLL_PERIOD  1

  # success
  EXIT         0
    STDOUT     @new.images
    ZERO       new.image.failure
  END

  # locked list
  EXIT         1
    MESSAGE    "new.images: exec failure"
    INCR       new.image.failure
  END

  # default exit status
  EXIT         -
    MESSAGE    "new.images: unknown exit status: $EXIT"
    INCR       new.image.failure
  END

  # operation times out?
  TIMEOUT      2
    MESSAGE    "new.images: timeout"
    INCR       new.image.failure
  END
END

TASK           copy.images
  COMMAND      copy.images $RemoteName $FileID $Host
  HOST         $Host
  STDERR       /data/logfiles/copy/$file.ID.log
  EXEC_PERIOD  2

  # pre-exec condition
  EXEC_TEST
    REQUIRE @new.images > 0
    REQUIRE $network    == 1
    REQUIRE $filesystem == 1
    PERFORM $RemoteName = @new.images[0]
    PERFORM $FileID     = @new.images[1]
    PERFORM $Host       = @new.images[2]
  END

  # success
  EXIT         0
    STDOUT     @new.images
    $new.image.failure = 0
  END

  # locked list
  EXIT         1
    MESSAGE    "new.images: exec failure"
    $new.image.failure ++
  END

  TIMEOUT      30
    MESSAGE    "new.images: timeout"
    $new.image.failure ++
  END
END

TASK      phase1
  INPUT   @science.mosaics  # (fileID)
  ARG.0   phase1
  ARG.1   INPUT[0]
  STDIN   @NULL
  STDOUT  @NULL
  STDERR  $log/phase1/%s.log INPUT[0]
  TIMEOUT 10
  PERIOD  10
  FAILURE @foo
END

TASK      test.phase2
  ARG.0   test.phase2 
  STDOUT  @science.chips # (hostname) (filename) (mosaicID)
END

TASK      run.phase2
  INPUT   @science.chips
  ARG.0   phase2
  ARG.1   $INPUT[1]
  ARG.2   $INPUT[2]
  HOST    $INPUT[0]
  TIMEOUT 40
  PERIOD  20
END

TASK      test.phase3
  ARG.0   test.phase3 
  STDOUT  @phase3.mosaics # (mosaicID)
END

TASK      run.phase3
  INPUT   @phase3.mosaics
  ARG.0   phase3
  ARG.1   $INPUT[1]
  ARG.2   $INPUT[2]
  HOST    $INPUT[0]
  TIMEOUT 40
  PERIOD  20
END



# example IPP functions

# steps to find and copy a single image from the summit

  - identify image to be copied
    - local resource (table or file) contains a list of 
      (remote.filename) (file.ID)
    - local process generates list from OTIS sources   
    - (remote.filename) : user@hostname:/full/path/filename.fits
    - (file.ID) : GPC-01.34.20041112-200110o.raw
    - (chip) : 34
    * pop entry from list?
    * read entry from list & flag?

    - file.ID options:
      - CAMERA.CHIP.SEQUENCE.TYPE.VERSION
      * GPC-01.34.20041112-200110o.raw
      * GPC-01.34.20041112-200110b.raw
      * MC.13.670123o.raw
      * MC.13.670123o.flt
      * MC.MF.670123o.flt

  - determine file.ID for remote file
    - provided with input list

  - determine primary host for image
    - based on chip
    - provided with input list

  - create new storage object with instance on target host
    - instance = new.object (file.ID) -host (host)
  - on target host, copy remote.file to instance
    @host: cp (remote.name) instance

  - if copy fails:	    
    - put back in queue?

  - add new chip to metadata database raw chips
  - add new mosaic to metadata database science frames



exec @host copy.image (remote.name) (file.ID) (host)

