
## PanTasks scripts for Phase 1

# tasks: p1pending p1astro p1done
#
# queues: 
#   p1pending: (expID) (version) (cameraID) (state)
#
# globals: 

# p1pending : search for images to be processed
task	       p1pending
  command      p1search -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 p1pending -uniq -key 0 "$line new"
    end
  end
end

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

  task.exec
    local expID version camera Npending
    queuesize  p1pending -var Npending
    if ($Npending == 0) break

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

    list tmp -split $line
    $expID  = $tmp:0
    $version = $tmp:1
    $camera = $tmp:2
    queuepush p1pending "$expID $version $camera run"

    host any
    command p1astro $expID $version $camera
  end

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

# p1update : search for images to be processed
task	       p1done
  command      p1search -done
  host         local

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