Index: trunk/ippScripts/scripts/magic_destreak_cleanup.pl
===================================================================
--- trunk/ippScripts/scripts/magic_destreak_cleanup.pl	(revision 29097)
+++ trunk/ippScripts/scripts/magic_destreak_cleanup.pl	(revision 29561)
@@ -89,5 +89,5 @@
 $q1 .= " WHERE magic_ds_id = $magic_ds_id";
 
-my $q2 = "SELECT * from magicDSFile where magic_ds_id = $magic_ds_id";
+my $q2 = "SELECT * from magicDSFile WHERE (data_state = 'full' OR data_state = 'update') AND magic_ds_id = $magic_ds_id";
 
 my $stmt1 = $dbh->prepare($q1);
@@ -245,21 +245,17 @@
             delete_files($rimage, $rmask, $rweight, $rsources, undef, $bimage, $bmask, $bweight, $bsources);
         }
-}
-
-$dbh = DBI->connect($dsn, $dbuser, $dbpassword) or die "Cannot connect to mysql server\n";
-
-if (!$no_update and ($num_components > 0)) {
-    my $result = $dbh->do("DELETE FROM magicDSFile WHERE magic_ds_id = ?", undef, $magic_ds_id);
-    # my $result = $stmt3->do($magic_ds_id);
-    my_die("attempt to delete magicDSFiles failed", $magic_ds_id, $PS_EXIT_UNKNOWN_ERROR) if $result eq "0E0";
-}
-
-if (!$no_update) {
-    my $result = $dbh->do("UPDATE magicDSRun SET state = 'cleaned' WHERE magic_ds_id = ?", undef, $magic_ds_id);
-    my_die("attempt to update magicDSRun.state failed", $magic_ds_id, $PS_EXIT_UNKNOWN_ERROR) if $result eq "0E0";
-} else {
-    print STDERR "skipping update of magicDSRun\n";
-}
-
+        my $command = "$magicdstool -tocleanedfile -magic_ds_id $magic_ds_id -component $component";
+        $command   .= " -dbname $dbname" if defined $dbname;
+
+        unless ($no_update) {
+            my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
+                run(command => $command, verbose => $verbose);
+            unless ($success) {
+                carp("failed to update database for $magic_ds_id");
+            }
+        } else {
+            print "Skipping command: $command\n";
+        }
+}
 
 ### Pau.
@@ -292,5 +288,5 @@
     $exit_code = $PS_EXIT_PROG_ERROR unless defined $exit_code;
 
-    my $command = "$magicdstool -updaterun -set_state failed_cleanup";
+    my $command = "$magicdstool -updaterun -set_state error_cleaned";
     $command   .= " -magic_ds_id $magic_ds_id";
     $command   .= " -dbname $dbname" if defined $dbname;
