Index: trunk/pstamp/scripts/pstamp_checkdependent.pl
===================================================================
--- trunk/pstamp/scripts/pstamp_checkdependent.pl	(revision 28046)
+++ trunk/pstamp/scripts/pstamp_checkdependent.pl	(revision 28047)
@@ -135,30 +135,22 @@
         print "skipping $command\n";
     }
-} elsif (($it->{state} eq 'cleaned') or ($it->{state} eq 'update') and 
-        ($whole_run or ($it->{data_state} eq 'cleaned'))) {
-
-    # Update has not been queued for this component yet.  
-
-    # XXX: if $whole_run this is going to repeatedly call -setimfiletoupdate. This will be a no-op
-    # but is wasteful.
-    # Maybe we should deal with having three states for pstampDependent.state. 'new', 'run', 'full'
-    # need to queue the updates
+} elsif (($it->{state} eq 'cleaned') or ($it->{state} eq 'update')) {
+    #       For warp and diff stages we need to call the 'queue_update' subroutines even if the 
+    #       data_state is update in order to check the earlier stages in the pipeline
+    #       For example if warpSkyfile is in update state but the chip run that it depends on hasn't
+    #       been updated we need to go and queue it.
+
     if ($stage eq 'chip') {
-        # queue_update_chip takes an array so that queue_update_warp can pass it set of chips
+        # check_states_chip takes an array so that check_states_warp can pass it set of chips
         my $chips = [$it];
-        $status = queue_update_chip($it->{chip_id}, $whole_run, $chips, $rlabel, $need_magic);
+        $status = check_states_chip($it->{chip_id}, $whole_run, $chips, $rlabel, $need_magic);
     } elsif ($stage eq 'warp') {
-        $status = queue_update_warp($it, $whole_run, $rlabel, $need_magic);
+        $status = check_states_warp($it, $whole_run, $rlabel, $need_magic);
     } elsif ($stage eq 'diff') {
-        $status = queue_update_diff($it, $whole_run, $rlabel, $need_magic);
+        $status = check_states_diff($it, $whole_run, $rlabel, $need_magic);
     } else {
         my_die("Unexpected stage found $stage", $PS_EXIT_PROG_ERROR);
     }
 } else {
-    # update for this component has been queued by hasn't completed yet or the state is an error state
-    # XXX: in this state we aren't checking the dependents anymore. If something goes wrong this
-    # will never complete. Consider calling the 'queue_update' functions again just to check. They are relatively cheap
-
-
     print "${stage}Run $stage_id state is $it->{state} $component data_state is $it->{data_state}\n";
 
@@ -170,13 +162,16 @@
     my $job_fault = 0;
 
-    # temporary hack to deal with data with old burntool tables. This check is now done by pstampparse.pl
-    # so runs in this state should not get queued
+if (0) {
     if ($stage eq 'chip') {
+        # XXX: There is no need to check this anymore. All magicked chipRuns have abs(burntool_state) >= 13
+        # If something changes that causes an error, we will figure that out from the chip failure
         my $burntool_state = $it->{burntool_state};
-        if ($burntool_state and (abs($burntool_state) < 14)) {
+        # XXX: get the value of 13 from the ppImage recipe
+        if ($burntool_state and (abs($burntool_state) < 13)) {
             print STDERR "chip $it->{chip_id} $it->{class_id} has burntool_state $burntool_state. Not avaiable.\n";
             $job_fault = $PSTAMP_NOT_AVAILABLE;
         }
     }
+}
     if ($state eq 'error_cleaned') {
         $job_fault = $PSTAMP_NOT_AVAILABLE;
@@ -191,5 +186,5 @@
     if (!$job_fault and ($stage eq 'chip')) {
         # chip processing is done, start destreaking.
-        $job_fault = queue_update_magicDSRun($stage, $stage_id, $rlabel, $need_magic, $it->{raw_magicked}, $it->{dsRun_state});
+        $job_fault = check_states_magicDSRun($stage, $stage_id, $rlabel, $need_magic, $it->{raw_magicked}, $it->{dsRun_state});
     }
     if ($job_fault) {
@@ -201,5 +196,5 @@
 
 
-sub queue_update_chip {
+sub check_states_chip {
     my $chip_id = shift;
     my $whole_run = shift;  # if true queue entire run for update
@@ -216,6 +211,8 @@
             $raw_all_magicked &= ($chip->{raw_magicked} > 0);
 
-            if ($chip->{state} =~ /error/) {
+            if (($chip->{state} =~ /error/) or ($chip->{state} =~ /purged/) or ($chip->{state} =~ /scrubbed/)) {
+                print "chipRun state is $chip->{chip_id} is in state $chip->{state} cannot update\n";
                 faultJobs('stop', undef, undef, $PSTAMP_GONE);
+                return;
             } elsif (($chip->{data_state} ne 'update') and ($chip->{data_state} ne 'full')) {
                 my $command = "$chiptool -setimfiletoupdate -chip_id $chip_id -class_id $chip->{class_id}";
@@ -237,4 +234,10 @@
         $dsRun_state = $run->{dsRun_state};
         $raw_all_magicked = ($run->{raw_magicked} > 0);
+        my $state = $run->{state};
+        if (($state =~ /error/) or ($state =~ /purged/) or ($state =~ /scrubbed/)) {
+            print "chipRun state is $run->{chip_id} is in state $state cannot update\n";
+            faultJobs('stop', undef, undef, $PSTAMP_GONE);
+            return;
+        }
 
         # providing no -class_id arguments changes all imfiles with data_state = 'cleaned' to 'update'
@@ -253,13 +256,13 @@
     }
 
-    my $status = queue_update_magicDSRun('chip', $chip_id, $rlabel, $need_magic, $raw_all_magicked, $dsRun_state);
+    my $status = check_states_magicDSRun('chip', $chip_id, $rlabel, $need_magic, $raw_all_magicked, $dsRun_state);
 
     return $status;
 }
 
-sub queue_update_warp {
+sub check_states_warp {
     # check status of input chips. If state is not updatable set error code for job
 
-    # if chipProcessedImfile.state is cleaned call queue_update_chip
+    # if chipProcessedImfile.state is cleaned call check_states_chip
 
     # need to code warptool -setskyfiletoupdate
@@ -301,5 +304,5 @@
         }
 
-        if ($chips_ready) {
+        if ($chips_ready and $skycell->{data_state} ne 'update') {
             # the reason we defer setting the warp to update is so that we can handle error conditions at previous
             # stages more easily.
@@ -317,5 +320,5 @@
             }
         } elsif (scalar @chipsToUpdate > 0) {
-            return queue_update_chip($chip_id, 0, \@chipsToUpdate, $rlabel, $need_magic);
+            return check_states_chip($chip_id, 0, \@chipsToUpdate, $rlabel, $need_magic);
         }
     } else {
@@ -354,5 +357,5 @@
             (($chipRun->{state} eq 'full') and ($need_magic and ($chipRun->{magicked} < 0) and ($chipRun->{dsRun_state} ne 'new')))) {
             my $data = [$chipRun];
-            return queue_update_chip($chipRun->{chip_id}, 1, $data, $rlabel, $need_magic);
+            return check_states_chip($chipRun->{chip_id}, 1, $data, $rlabel, $need_magic);
         }
     }
@@ -361,5 +364,5 @@
     return 0;
 }
-sub queue_update_diff {
+sub check_states_diff {
     my $metadata = shift;
     my $whole_run = shift;  # if true queue entire run for update
@@ -392,5 +395,5 @@
 
             if ($warp->{data_state} ne 'full') {
-                return queue_update_warp($warp, 0, $rlabel, $need_magic);
+                return check_states_warp($warp, 0, $rlabel, $need_magic);
             }
             # warps are ready fall through and queue the diff update
@@ -404,5 +407,5 @@
             if ($warp1->{data_state} ne 'full') {
                 $warps_ready = 0;
-                $status = queue_update_warp($warp1, 0, $rlabel, $need_magic);
+                $status = check_states_warp($warp1, 0, $rlabel, $need_magic);
                 if ($status) {
                     return $status;
@@ -415,5 +418,5 @@
             if ($warp2->{data_state} ne 'full') {
                 $warps_ready = 0;
-                $status = queue_update_warp($warp2, 0, $rlabel, $need_magic);
+                $status = check_states_warp($warp2, 0, $rlabel, $need_magic);
             }
 
@@ -467,5 +470,5 @@
 
             if ($warp->{data_state} ne 'full') {
-                return queue_update_warp($warp, 0, $rlabel, $need_magic);
+                return check_states_warp($warp, 0, $rlabel, $need_magic);
             }
             # warps are ready fall through and queue the diff update
@@ -474,15 +477,17 @@
         }
 
-        my $command = "$difftool -setskyfiletoupdate -diff_id $diff_id -skycell_id $skycell_id";
-        $command .= " -set_label $rlabel" if $rlabel;
-
-        if (!$no_update) {
-            my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
-                        run(command => $command, verbose => $verbose);
-            unless ($success) {
-                my_die("failed to queue ${stage}Run $stage_id $component for update", $PS_EXIT_UNKNOWN_ERROR);
-            }
-        } else {
-            print "skipping $command\n";
+        if ($skycell->{data_state} ne 'update') {
+            my $command = "$difftool -setskyfiletoupdate -diff_id $diff_id -skycell_id $skycell_id";
+            $command .= " -set_label $rlabel" if $rlabel;
+
+            if (!$no_update) {
+                my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
+                            run(command => $command, verbose => $verbose);
+                unless ($success) {
+                    my_die("failed to queue ${stage}Run $stage_id $component for update", $PS_EXIT_UNKNOWN_ERROR);
+                }
+            } else {
+                print "skipping $command\n";
+            }
         }
     } else {
@@ -599,5 +604,5 @@
 }
 
-sub queue_update_magicDSRun {
+sub check_states_magicDSRun {
     my $stage = shift;
     my $stage_id = shift;
@@ -608,7 +613,7 @@
 
     # XXX: this code assumes that destreaking is handled at the chip stage
-    my_die ("queue_update_magicDSRun only works for stage chip", $PS_EXIT_PROG_ERROR) if $stage ne 'chip';
-
-    # if called from queue_update_warp dsRun_state is unknown. Go find it.
+    my_die ("check_states_magicDSRun only works for stage chip", $PS_EXIT_PROG_ERROR) if $stage ne 'chip';
+
+    # if called from check_states_warp dsRun_state is unknown. Go find it.
     if (!$dsRun_state) {
         my $command = "$chiptool -listrun -chip_id $stage_id";
