
## PanTasks scripts for Phase 0

# tasks: p0ready p0update p0cleanup
#
# queues: 
#   p0pending: (expID) (cameraID) (state)
#
# globals: 

# p0pending : search for images to be processed
task	       p0pending
  command      p0search -pending
  host         local

  # timeout shorter than exec so jobs do not build up
  periods      -exec     5
  periods      -poll     1
  periods      -timeout  4

  # success
  task.exit    0
    local i Nstdout
    # keep only new, unique entries (URL is key)
    queuesize stdout -var Nstdout
    for i 0 $Nstdout
      queuepop stdout -var line
      queuepush p0pending -uniq -key 0 "$line new"
    end
  end
end

# p0update : submit an exposure to be updated
task	       p0update
  periods      -exec 1
  periods      -timeout 10

  task.exec
    local Npending
    queuesize  p0pending -var Npending
    if ($Npending == 0) break

    queuepop p0pending -var line -key 2 new
    if ("$line" == "NULL") break

    list tmp -split $line
    $expID  = $tmp:0
    $camera = $tmp:1
    queuepush p0pending "$expID $camera run"

    host any
    command p0search -update $expID $camera
  end

  # success
  task.exit    0
    queueinit stdout
    queuepush p0pending $taskargs:0 $taskargs:1 done
  end
end

# p0update : search for images to be processed
task	       p0cleanup
  command      p0search -cleanup
  host         local

  # timeout shorter than exec so jobs do not build up
  periods      -exec     30
  periods      -poll     1
  periods      -timeout  10
end
