Index: /trunk/ippTasks/nightly_stacks.pro
===================================================================
--- /trunk/ippTasks/nightly_stacks.pro	(revision 27180)
+++ /trunk/ippTasks/nightly_stacks.pro	(revision 27181)
@@ -107,4 +107,5 @@
 
 book init nsData
+book init nsBurntool
 #
 # Macros to control the book.
@@ -122,4 +123,5 @@
        echo $date $state
     end
+    book npages nsBurntool -var Npages
 end
 
@@ -265,4 +267,10 @@
     ipptool2book stdout nsData -uniq -key date
 
+    # remove the burntool page if we're done with it.
+    book getword nsData $options:0 nsState -var ns_STATE
+    if ("$ns_STATE" == "QUEUECHIPS") 
+	book delpage nsBurntool $options:0
+    end
+
     if ($VERBOSE > 2)
 	book listbook nsData
@@ -427,5 +435,5 @@
     book delpage nsData $options:0
 # We've queued up stacking. We're done with this date, so don't reload the page.
-#    ipptool2book stdout nsData -uniq -key date
+    ipptool2book stdout nsData -uniq -key date
     if ($VERBOSE > 2)
 	book listbook nsData
@@ -447,5 +455,5 @@
 
 #
-# Generate a list of date ranges that need to be processed with burntool
+# Generate a list of date ranges and chips that need to be processed with burntool
 #
 task              ns.burntool.load
@@ -457,28 +465,31 @@
 
   task.exec
-     stdout NULL
-     stderr $LOGDIR/ns.burntool.log
-
-     book getpage nsData $ns_burnPAGE -var date
-     book getword nsData $date nsState -var ns_STATE
-     book npages nsData -var Npages
-
-    if ($VERBOSE > 5) 
-       echo "ns.burntool.load: " $ns_burnPAGE $date $ns_STATE $Npages
-    end
-
-     $ns_burnPAGE ++
-     if ($ns_burnPAGE >= $Npages) set ns_burnPAGE = 0
-     option $date
-
-     if ("$ns_STATE" != "NEEDSBURNING") break
-     $run = automate_stacks.pl --define_burntool --date $date
-     command $run
-   end
+    stdout NULL
+    stderr $LOGDIR/ns.burntool.log
+
+    book getpage nsData $ns_burnPAGE -var date
+    book getword nsData $date nsState -var ns_STATE
+    book npages nsData -var Npages
+
+    if ($VERBOSE > 5) 
+        echo "ns.burntool.load: " $ns_burnPAGE $date $ns_STATE $Npages
+    end
+
+    $ns_burnPAGE ++
+    if ($ns_burnPAGE >= $Npages) set ns_burnPAGE = 0
+    option $date
+    
+    if ("$ns_STATE" != "NEEDSBURNING") break
+
+    $run = automate_stacks.pl --define_burntool --date $date
+    command $run
+  end
   # success
   task.exit   0
 #    convert 'stdout' to book format
-    book delpage nsData $options:0
-    ipptool2book stdout nsData -uniq -key date
+#    book delpage nsBurntool $options:0
+    ipptool2book stdout nsBurntool -uniq -key date
+    book setword nsData $options:0 nsState "QUEUEBURNING"
+
     if ($VERBOSE > 2)
 	book listbook nsData
@@ -500,5 +511,5 @@
 
 #
-# Magically run burntool on the data, based on the time ranges stored in our book.
+# Magically run burntool on the data, based on the information stored in our book.
 #
 task              ns.burntool.run
@@ -516,52 +527,71 @@
 
     if ($VERBOSE > 5) 
-       echo "ns.burntool.run: " $ns_RburnPAGE $date $ns_STATE $Npages
-    end
-
-     $ns_RburnPAGE ++
-     if ($ns_RburnPAGE >= $Npages) set ns_RburnPAGE = 0
-
-     if ("$ns_STATE" != "QUEUEBURNING") break
-	book getword nsData $date btN -var btN
-	book getword nsData $date btNCounter -var btNcounter
-
-	if ($btNcounter > $btN) 
-	    book setword nsData $date nsState "BURNING"
-	    break
-	end
-
-	sprintf start_date_label "btBegin%d"   $btNcounter
-	sprintf end_date_label   "btEnd%d"     $btNcounter
-	sprintf counter_label    "btCounter%d" $btNcounter
-
-	book getword nsData $date $start_date_label -var start_date
-	book getword nsData $date $end_date_label -var end_date
-	book getword nsData $date $counter_label -var chip_counter
+        echo "ns.burntool.run: " $ns_RburnPAGE $date $ns_STATE $Npages
+    end
+
+    $ns_RburnPAGE ++
+    if ($ns_RburnPAGE >= $Npages) set ns_RburnPAGE = 0
+    if (("$ns_STATE" != "QUEUEBURNING")&&("$ns_STATE" != "BURNING")) break
+    # Find out where in the list of jobs we are
+    book getword nsBurntool $date btN -var btN
+    book getword nsBurntool $date btNCounter -var btNcounter
+
+    if ($VERBOSE > 5) 
+	echo "ns.burntool.run: Status: " $btNcounter $date
+    end
+
+    if ("$ns_STATE" == "QUEUEBURNING")
+	$new_state = "QUEUEBURNING"
+    end
+    if ("$ns_STATE" == "BURNING")
+	$new_state = "BURNING"
+    end
+    if ($btNcounter > $btN)
+	$new_state = "BURNING"
+	$btNcounter = 0
+    end
+    if ($VERBOSE > 5) 
+	echo "ns.burntool.run: Status: " $btNcounter $new_state
+    end
+    
+    # Increment the counter in the book for the next job.
+    $counter_update = $btNcounter + 1
+    book setword nsBurntool $date btNCounter $counter_update
+
+    # Get the current status of this job, and skip if it doesn't need to process.
+    sprintf status_label     "bt%dStatus"  $btNcounter
+    book getword nsBurntool $date $status_label   -var status
+
+    if ($VERBOSE > 5) 
+	echo "ns.burntool.run: Status: " $btNcounter $status $date $status_label
+    end
+    if (("$status" == "FINISHED")||("$status" == "RUN")) break	
+    
+    # Continue loading information to process this job
+    sprintf start_date_label "bt%dBegin"   $btNcounter
+    sprintf end_date_label   "bt%dEnd"     $btNcounter
+    sprintf class_count_label "bt%dClass"   $btNcounter
+
+    book getword nsBurntool $date $start_date_label -var start_date
+    book getword nsBurntool $date $end_date_label -var end_date
+    book getword nsBurntool $date $class_count_label -var chip_counter
+
+    # Lookup class_id/host pairs
+    list word -split $hostmatch:$chip_counter
+    $class_id = $word:0
+    $host = $word:1
+    host $host
+#    set.host.for.camera GPC1 $class_id
+    $logfile = "burntool_logs/$class_id.$start_date.log"
+
+    $run = ipp_apply_burntool.pl --class_id $class_id --dateobs_begin $start_date --dateobs_end $end_date --dbname gpc1 --logfile $logfile
 	
-	list word -split $hostmatch:$chip_counter
-	$class_id = $word:0
-	$host = $word:1
-	host $host
-	$logfile = "burntool_logs/$class_id.$start_date.log"
-
-	$run = ipp_apply_burntool.pl --class_id $class_id --dateobs_begin $start_date --dateobs_end $end_date --dbname gpc1 --logfile $logfile
-	
-	echo "ns.burntool.run: " $date $btN $btNcounter $start_date $end_date $chip_counter $class_id $host $logfile $run
-	$chip_counter ++
-	if ($chip_counter >= $hostmatch:n) 
-	   $chip_counter = 0
-	   $btNcounter ++
-	end
-	$new_state = "QUEUEBURNING"
-	if ($btNcounter > $btN)
-	    $new_state = "BURNING"
-	end
-
-	echo "ns.burntool.run: " $date $btN $btNcounter $chip_counter $new_state
-	book setword nsData $date $counter_label $chip_counter
-	book setword nsData $date btNCounter $btNcounter
-	option $date $new_state
-	
-	command $run
+    echo "ns.burntool.run: " $date $btN $btNcounter $start_date $end_date $chip_counter $class_id $host $logfile $run
+    echo "ns.burntool.run: " $date $btN $btNcounter $chip_counter $new_state
+
+    book setword nsBurntool $date $status_label RUN
+    option $date $new_state $status_label
+
+    command $run
 #     command /bin/true
    end
@@ -569,8 +599,8 @@
   task.exit   0
 #    convert 'stdout' to book format
-#    book delpage nsData $options:0
-#    ipptool2book stdout nsData -uniq -key date
+    # Set data state based on if we're queueing or waiting
     book setword nsData $options:0 nsState $options:1
-
+    # Set the job state for success.
+    book setword nsBurntool $options:0 $options:2 FINISHED
     if ($VERBOSE > 2)
 	book listbook nsData
@@ -580,11 +610,14 @@
   # locked list
   task.exit    default
+    book setword nsBurntool $options:0 $options:2 FAIL
     showcommand failure
   end
   task.exit    crash
+    book setword nsBurntool $options:0 $options:2 FAIL
     showcommand crash
   end
   #operation times out?
   task.exit    timeout
+    book setword nsBurntool $options:0 $options:2 FAIL
     showcommand timeout
   end
