
## PanTasks scripts for Phase 3

# tasks: p3pending p3astro p3done
#
# queues: 
#   p3pending: (expID) (version) (cameraID) (state)
#
# globals: 

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

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

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

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

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

    host any
    command p3astro $expID $version $camera
  end

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

# p3update : search for images to be processed
task	       p3done
  command      p3search -done
  host         local

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