Index: trunk/pstamp/scripts/pstamp_job_run.pl
===================================================================
--- trunk/pstamp/scripts/pstamp_job_run.pl	(revision 24962)
+++ trunk/pstamp/scripts/pstamp_job_run.pl	(revision 24963)
@@ -14,25 +14,29 @@
 use Digest::MD5::File qw( file_md5_hex );
 use PS::IPP::PStamp::RequestFile qw( :standard );
-
-my $verbose;
-my $dbname;
-my $dbserver;
-my $job_id;
-my $redirect_output;
-my $output_base;
-
-my $host = hostname();
-
+use IPC::Cmd 0.36 qw( can_run run );
+
+use PS::IPP::Metadata::Config;
+#use PS::IPP::Metadata::Stats;
+#use PS::IPP::Metadata::List qw( parse_md_list );
+
+use PS::IPP::Config qw( :standard );
+
+my ($job_id, $redirect_output, $outputBase, $rownum, $jobType); 
+my ($verbose, $dbname, $dbserver, $no_update);
 
 GetOptions(
     'job_id=s'          =>  \$job_id,
-    'output_base=s'     =>  \$output_base,
-    'redirect-output'   => \$redirect_output,
+    'job_type=s'        =>  \$jobType,
+    'rownum=s'          =>  \$rownum,
+    'output_base=s'     =>  \$outputBase,
+    'redirect-output'   =>  \$redirect_output,
     'dbname=s'          =>  \$dbname,
     'dbserver=s'        =>  \$dbserver,
     'verbose'           =>  \$verbose,
+    'no-update'         =>  \$no_update,
 );
 
 
+my $host = hostname();
 if ($verbose) {
     print "\n\n";
@@ -41,29 +45,17 @@
 
 die "job_id is required" if !$job_id;
-die "output_base is required" if !$output_base;
-
-use IPC::Cmd 0.36 qw( can_run run );
-
-use PS::IPP::Metadata::Config;
-use PS::IPP::Metadata::Stats;
-use PS::IPP::Metadata::List qw( parse_md_list );
-
-use PS::IPP::Config qw($PS_EXIT_SUCCESS
-		       $PS_EXIT_UNKNOWN_ERROR
-		       $PS_EXIT_SYS_ERROR
-		       $PS_EXIT_CONFIG_ERROR
-		       $PS_EXIT_PROG_ERROR
-		       $PS_EXIT_DATA_ERROR
-		       $PS_EXIT_TIMEOUT_ERROR
-		       metadataLookupStr
-		       metadataLookupBool
-		       caturi
-		       );
+
+my_die( "job_type is required", $job_id, $PS_EXIT_PROG_ERROR) if !$jobType;
+my_die("rownum is required", $job_id, $PS_EXIT_PROG_ERROR) if !$rownum;
+my_die("output_base is required", $job_id, $PS_EXIT_PROG_ERROR) if !$outputBase;
+
+
 my $ipprc = PS::IPP::Config->new(); # IPP Configuration
-
 if ($redirect_output) {
-    my $logDest = "$output_base.log";
-    $ipprc->redirect_output($logDest);
-}
+    my $logDest = "$outputBase.log";
+    $ipprc->redirect_output($logDest)
+        or my_die ("unable to redirect output to $logDest", $job_id, $PS_EXIT_UNKNOWN_ERROR);
+}
+
 
 if (!$dbserver) {
@@ -78,50 +70,13 @@
 
 if ($missing_tools) {
-    warn("Can't find required tools.");
-    exit ($PS_EXIT_CONFIG_ERROR);
+    my_die("Can't find required tools", $job_id, $PS_EXIT_CONFIG_ERROR);
 }
 
 my $mdcParser = PS::IPP::Metadata::Config->new; # Parser for metadata config files
-
-my $psjob;
-#Look up the uri for the given job
-{
-    my $command = "$pstamptool -pendingjob -job_id $job_id";
-    $command .= " -dbname $dbname" if $dbname;
-    $command .= " -dbserver $dbserver" if $dbserver;
-    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
-        run(command => $command, verbose => $verbose);
-    unless ($success) {
-        die("Unable to perform pstamptool -pendingjob: $error_code");
-    }
-
-    if (@$stdout_buf == 0) {
-        print STDERR "pending pstamp job id $job_id not found\n";
-        exit 0;
-    }
-    my $metadata = $mdcParser->parse(join "", @$stdout_buf) or
-        die("Unable to parse metdata config doc");
-
-    my $jobs = parse_md_list($metadata);
-    my $num = @$jobs;
-    die "unexpected number of jobs $num found for job: $job_id" if $num > 1;
-
-    $psjob = $jobs->[0];
-}
-
-if (!$psjob) {
-    print STDERR "postage stamp job $job_id not found\n";
-    exit 1;
-}
-
-my $rownum = $psjob->{rownum};
-my $uri = $psjob->{uri};
-my $outputBase = $psjob->{outputBase};
-my $jobType = $psjob->{jobType};
-#my $argString = $psjob->{args};
 
 my $jobStatus;
 if ($jobType eq "stamp") {
-    open ARGSLIST, "<$uri" or die "failed to open argslist file $uri";
+    my $argslist = "$outputBase.args";
+    open ARGSLIST, "<$argslist" or my_die("failed to open argslist file $argslist", $job_id, $PS_EXIT_UNKNOWN_ERROR);
     my $argString = <ARGSLIST>;
     close ARGSLIST;
@@ -129,5 +84,5 @@
 
     # XXX: should we do any other sanity checking?
-    die "arglist file $uri is empty" if !$argString;
+    my_die("arglist file $argslist is empty", $job_id, $PS_EXIT_DATA_ERROR) if !$argString;
 
     my $command = "$ppstamp $outputBase $argString";
@@ -138,35 +93,31 @@
 
     if ($success) {
-        # XXX shouldn't need to do this, review schema`
         my $dir = dirname($outputBase);
 
         my $reglist = "$dir/reglist$job_id";
 
-        open F, ">$reglist" or die "can't open $reglist for output";
-
-        # figure out what output images we should expect
-
-        # Note: we are assuming the contents of the filerules here.
-        my @extensions = ( "fits", "mk.fits", "wt.fits");
-
-        # we always require an image
+        open F, ">$reglist" or my_die( "can't open $reglist for output", $job_id, $PS_EXIT_UNKNOWN_ERROR);
+
+        # Figure out what output images were produced
+
+        # Note: we are assuming the contents of the PSTAMP filerules here.
+        my %extensions = ( $PSTAMP_SELECT_IMAGE  => "fits", 
+                           $PSTAMP_SELECT_MASK   => "mk.fits",
+                           $PSTAMP_SELECT_WEIGHT => "wt.fits");
+
+        # we always create a stamp of the image
         my $output_mask = $PSTAMP_SELECT_IMAGE;
 
-        # we search the argString for -mask and -weight. 
+        # we search the argString for -mask and -variance. 
         # searching the arg string allows us to avoid adding a column in pstampJob
-        # XXX yeah but does this make us susceptible to user input fouling us up?
-        # For example, what if somebody sets the output base to file-mask-weight
-        # TODO: clean this up
-        $output_mask |= $PSTAMP_SELECT_MASK   if ($argString =~ /-mask/);
-        $output_mask |= $PSTAMP_SELECT_WEIGHT if ($argString =~ /-weight/);
-
-        # XXX we're getting a bit intimate with the bit field definitions here.
-        # do better use a hash
-
-        my $m = 1;
-        foreach my $extension (@extensions) {
-            my $do_this_one = $m & $output_mask;
-            $m = $m << 1;
+        $output_mask   |= $PSTAMP_SELECT_MASK   if ($argString =~ /-mask/);
+        $output_mask   |= $PSTAMP_SELECT_WEIGHT if ($argString =~ /-variance/);
+
+        foreach my $key (keys (%extensions)) {
+            my $do_this_one = $key & $output_mask;
+
             next if (! $do_this_one);
+
+            my $extension = $extensions{$key};
 
             my $basename = basename($outputBase);
@@ -183,7 +134,10 @@
     } else {
         $jobStatus = $error_code >> 8;
-        print STDERR "ppstamp failed with error code: $jobStatus\n";
+        my_die( "ppstamp failed with error code: $jobStatus", $job_id, $jobStatus);
     }
 } elsif ($jobType eq "get_image") {
+    my_die( "get_image jobs not working right now", $job_id, $PS_EXIT_CONFIG_ERROR);
+
+    my $uri = "";
     my $command = "$pstamp_get_image_job --job_id $job_id --uri $uri --out_dir $outputBase --rownum $rownum";
     $command .= " --dbname $dbname" if $dbname;
@@ -196,25 +150,29 @@
     } else {
         $jobStatus = $error_code >> 8;
-        print STDERR "ppstamp failed with error code: $jobStatus\n";
+        my_die( "pstamp_get_image_job failed with error code: $jobStatus", $job_id, $jobStatus);
     }
 } elsif ($jobType eq "detect_query") {
-    die("multiple detect_query jobs not supported yet");
+    my_die("detect_query jobs not supported yet", $job_id,$PS_EXIT_CONFIG_ERROR);
 } else {
-    die("unknown jobType $jobType found");
-}
-
-# stop the job and set the result value
+    my_die("unknown jobType $jobType found", $job_id, $PS_EXIT_PROG_ERROR);
+}
+
+# mark the job stopped in the database
 {
-    my $command = "$pstamptool -updatejob -job_id $job_id -state stop -fault $jobStatus";
+    my $command = "$pstamptool -updatejob -job_id $job_id -state stop"; 
     $command .= " -dbname $dbname" if $dbname;
     $command .= " -dbserver $dbserver" if $dbserver;
-    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
-        run(command => $command, verbose => $verbose);
-    unless ($success) {
-        die("Unable to perform $command: $error_code");
-    }
-}
-
-exit $jobStatus;
+    if (!$no_update) {
+        my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
+            run(command => $command, verbose => $verbose);
+        unless ($success) {
+            die("Unable to perform $command: $error_code");
+        }
+    } else {
+        print STDERR "skipping command: $command\n"
+    }
+}
+
+exit 0;
 
 # create a string to be passed as input to dsreg when registering this file in a fileset
@@ -227,5 +185,5 @@
     if (-e $path) {
         my @finfo = stat($path);
-        die "failed to stat $path" unless (@finfo);    # XXX clean up
+        my_die("failed to stat $path", $job_id, $PS_EXIT_UNKNOWN_ERROR) unless (@finfo); 
         my $bytes = $finfo[7];
         my $md5sum = file_md5_hex($path);
@@ -233,5 +191,26 @@
         return "$filename|$bytes|$md5sum|$filetype|";
     } else {
-        die "$filename not found at $path";
-    }
-}
+        my_die("$filename not found at $path", $job_id, $PS_EXIT_UNKNOWN_ERROR);
+    }
+}
+
+sub my_die
+{
+    my $msg = shift;            # Warning message on die
+    my $job_id = shift;         # job identifier
+    my $exit_code = shift;      # Exit code to add
+
+    $exit_code = $PS_EXIT_PROG_ERROR unless defined $exit_code;
+
+    carp($msg);
+    if (defined $job_id and not $no_update) {
+        my $command = "$pstamptool -updatejob";
+        $command .= " -job_id $job_id";
+        $command .= " -fault $exit_code";
+        $command .= " -dbname $dbname" if defined $dbname;
+        $command .= " -dbserver $dbserver" if defined $dbserver;
+        system($command);
+    }
+    exit $exit_code;
+}
+
