Changeset 27859 for trunk/pstamp/scripts/dqueryparse.pl
- Timestamp:
- May 4, 2010, 4:10:59 PM (16 years ago)
- File:
-
- 1 edited
-
trunk/pstamp/scripts/dqueryparse.pl (modified) (8 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/pstamp/scripts/dqueryparse.pl
r27578 r27859 2 2 # 3 3 # parse a MOPS_DETCTABILITY_QUERY table and create a results file 4 #5 # Note: this file is currently only a placeholder which creates a fake response file6 # and adds a completed job to the database7 4 # 8 5 9 6 use strict; 10 7 use warnings; 11 8 use Carp; 12 9 use Getopt::Long qw( GetOptions ); 13 10 use Pod::Usage qw( pod2usage ); 14 11 use IPC::Cmd 0.36 qw( can_run run ); 15 12 13 use PS::IPP::PStamp::RequestFile qw( :standard ); 14 use PS::IPP::PStamp::Job qw( :standard ); 16 15 use PS::IPP::Config qw($PS_EXIT_SUCCESS 17 16 $PS_EXIT_UNKNOWN_ERROR … … 26 25 ); 27 26 27 my ($no_update, $imagedb, $label); 28 28 my ($req_file, $req_id, $out_dir, $product, $mode, $dbname, $dbserver, $verbose, $save_temps); 29 29 my ($job_id,$rownum); # stuff from the post-update world 30 30 # 31 31 # parse args … … 35 35 'file=s' => \$req_file, 36 36 'req_id=s' => \$req_id, 37 'job_id=s' => \$job_id, 38 'rownum=s' => \$rownum, 37 39 'out_dir=s' => \$out_dir, 40 'label=s' => \$label, 38 41 'product=s' => \$product, 39 42 'mode=s' => \$mode, … … 44 47 ) or pod2usage(2); 45 48 46 my $err = ""; 47 48 if (!$req_file) { 49 $err .= "--file is required\n"; 50 } 51 if (!$req_id) { 52 $err .= "--req_id is required\n"; 53 } 54 if (!$out_dir) { 55 $err .="--out_dir is required\n"; 56 } 57 if (!$product) { 58 $err .="--product is required\n"; 59 } 60 61 62 die $err if ($err); 63 49 die "invalid mode '$mode'" unless ($mode eq "list_uri") or ($mode eq "queue_job"); 50 die "--file or --job_id is required" if !$req_file; 51 52 if ($mode ne "list_uri") { 53 die "req_id is required" if !$req_id; 54 die "out_dir is required" if !$out_dir; 55 die "product is required" if !$product; 56 } 64 57 my $missing_tools; 65 58 my $pstamptool = can_run('pstamptool') or (warn "Can't find pstamptool" and $missing_tools =1); 66 my $fakedresponse = can_run('fakedresponse.pl') or (warn "Can't find fakedresponse.pl" and $missing_tools =1); 59 my $detectresponse = can_run('detectability_respond.pl') or 60 (warn "Can't find detectability_respond.pl" and $missing_tools = 1); 67 61 my $fields = can_run('fields') or (warn "Can't find fields" and $missing_tools =1); 68 62 … … 77 71 } 78 72 73 # just deal with these arguments once and for all 74 $pstamptool .= " -dbname $dbname" if $dbname; 75 $pstamptool .= " -dbserver $dbserver" if $dbserver; 76 $detectresponse .= " --dbname $dbname" if $dbname; 77 78 $no_update = 1 if $mode eq "list_job"; 79 80 # Unless we're running as a job, write the parse arguments in case we need to rerun this parsing. 81 if (!$job_id) { 82 my $argslist = "$out_dir/parse.args"; 83 open ARGSLIST, ">$argslist" or my_die("failed to open argslist file $argslist", $job_id, $PS_EXIT_UNKNOWN_ERROR); 84 print ARGSLIST "--label $label --mode $mode --req_id $req_id --product $product --out_dir $out_dir --file $req_file\n"; 85 close ARGSLIST; 86 } 87 79 88 # get the query id and check the extname and version from the header 80 my $fields_output; 89 my $fields_output; 81 90 { 82 91 my $command = "echo $req_file | $fields -x 0 EXTNAME EXTVER QUERY_ID"; … … 84 93 run(command => $command, verbose => $verbose); 85 94 86 # fields doesn't return zero when it succeeds87 # unless ($success) {88 # print STDERR @$stderr_buf;89 # }90 95 $fields_output = join "", @$stdout_buf; 91 96 } 92 97 my (undef, $extname, $extver, $req_name) = split " ", $fields_output; 93 98 94 die "$req_file is missing one of EXTNAME EXTVER or QUERY_ID" 99 my_die("$req_file is missing one of EXTNAME EXTVER or QUERY_ID", $PS_EXIT_PROG_ERROR) 95 100 if !(defined($extname) and defined($extver) and defined($req_name)); 96 97 die "$req_file has EXTNAME $extname not MOPS_DETECTABILITY_QUERY table" 98 if $extname ne "MOPS_DETECTABILITY_QUERY"; 99 die "$req_file is version $extver expecting 1" if $extver ne 1; 100 101 if (0) { 102 $out_dir .= "/$req_name"; 103 } 101 my_die("$req_file has EXTNAME $extname not MOPS_DETECTABILITY_QUERY table", $PS_EXIT_PROG_ERROR) 102 if $extname ne "MOPS_DETECTABILITY_QUERY"; 103 my_die("$req_file is version $extver expecting 1", $PS_EXIT_PROG_ERROR) 104 if $extver ne 1; 105 106 # Set up the workdir for this query. 104 107 if (! -e $out_dir ) { 105 mkdir $out_dir or die "cannot create output directory $out_dir";108 mkdir $out_dir or my_die("cannot create output directory $out_dir", $PS_EXIT_PROG_ERROR); 106 109 } elsif (! -d $out_dir ) { 107 die "output fileset directory $out_dir exists but is not a directory"; 108 } 109 110 # 111 # XXX Eventually we will parse the file here, to look up the list of input images to be processed 112 # and queue a job for each image. 113 # The request file will be one of the arguments. Each job will look at all rows and create entries 114 # in the response file the coordinates that overlap the image. 115 # 116 # In the meantime we don't parse the file here, pass it to the program fakedresponse. 117 # It creates a response file with a line for each row in the request file 118 119 my $response_file = "$out_dir/response.fits"; 110 my_die ("output fileset directory $out_dir exists but is not a directory", $PS_EXIT_PROG_ERROR); 111 } 112 113 114 # Pass along the request file to the response generator. 115 my $response_file = "$out_dir/${req_name}.dresponse.${req_id}.fits"; 120 116 my $fault; 117 my $data_to_update = ''; 121 118 { 122 my $command = "$ fakedresponse --input $req_file --output $response_file --workdir $out_dir";119 my $command = "$detectresponse --input $req_file --output $response_file --workdir $out_dir"; 123 120 $command .= " --save-temps" if $save_temps; 124 121 $command .= " --verbose" if $verbose; … … 127 124 run(command => $command, verbose => $verbose); 128 125 unless ($success) { 129 warn("Unable to perform $command error code: $error_code"); 130 } 126 warn("Warn! Unable to perform $command error code: $error_code"); 127 } 128 # Use the fault code to see if we can regenerate the missing data. 131 129 $fault = $error_code >> 8; 132 } 133 134 my $job_id; 130 if ($fault == $PSTAMP_NOT_AVAILABLE) { 131 $data_to_update = (split /\n/, (join "", @$stdout_buf))[-1]; 132 } 133 } 134 135 135 my $result; 136 { 136 # If we returned correctly with a valid response file, get a job ID 137 # for the completed work, and move the response to a standardized name. 138 if ($fault == 0) { 137 139 my $command = "$pstamptool -addjob -req_id $req_id -outputBase $out_dir"; 138 $command .= " -job_type detect_query -state stop -fault $fault";140 $command .= " -job_type detect_query -state stop -fault 0"; 139 141 $command .= " -rownum 1"; 140 $command .= " -dbname $dbname" if $dbname; 141 $command .= " -dbserver $dbserver" if $dbserver; 142 143 my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) = 144 run(command => $command, verbose => $verbose); 145 142 143 my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) = 144 run(command => $command, verbose => $verbose); 146 145 if ($success) { 147 146 $job_id = join "", @$stdout_buf; … … 156 155 } 157 156 } 158 159 157 else { 158 # Failed to run correctly, which means that we need to queue a job and flag data for updating. 159 # Get the dependency id for the data we're requesting be updated. 160 my $dep_id = queue_update_run($req_id,$job_id,$out_dir,$label,$data_to_update); 161 162 # Link this request to a job and link that job to any dependency 163 my $command = "$pstamptool -addjob -req_id $req_id -outputBase $out_dir"; 164 $command .= " -job_type detect_query -state run -fault 0"; 165 $command .= " -rownum 1"; 166 $command .= " -dep_id $dep_id" if $dep_id; 167 168 my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) = 169 run(command => $command, verbose => $verbose); 170 171 if ($success) { 172 $job_id = join "", @$stdout_buf; 173 chomp $job_id; 174 if ($job_id && -e $response_file) { 175 # We shouldn't have a response file at this stage. 176 rename $response_file, "$out_dir/response${job_id}.fits"; 177 } 178 $result = 0; 179 } else { 180 warn("Unable to perform $command error code: $error_code"); 181 $result = $error_code >> 8; 182 } 183 } 184 185 # This does not set the request state to stop. That will happen with the request_finish.pl script, 186 # which will notice that we've inserted the stopped job and decide we're finished. Easy enough. 160 187 { 161 188 my $command = "$pstamptool -updatereq -req_id $req_id -name $req_name -outProduct $product"; 162 189 $command .= " -fault $result" if $result; 163 $command .= " -dbname $dbname" if $dbname;164 $command .= " -dbserver $dbserver" if $dbserver;165 190 166 191 my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) = 167 192 run(command => $command, verbose => $verbose); 168 193 unless ($success) { 169 die "$command failed";194 my_die("$command failed",$PS_EXIT_UNKNOWN_ERROR); 170 195 } 171 196 } 172 197 173 198 exit 0; 199 200 201 # If we have to queue an update run, do so and create a new dependent 202 sub queue_update_run { 203 my ($req_id, $job_id, $out_dir, $label, $data_to_update) = @_; 204 205 my ($state, $stage, $stage_id, $component, $need_magic, $imagedb) = split /\s+/, $data_to_update; 206 207 if (($state ne 'cleaned') and ($state ne 'update') and ($state ne 'goto_cleaned')) { 208 # We should have received one of these states, so if not, signal that we have a problem. 209 my_die("$stage $stage_id is in unexpected state $state", $PS_EXIT_PROG_ERROR); 210 } 211 my $dep_id; 212 my $command = "$pstamptool -getdependent -stage $stage -stage_id $stage_id -imagedb $imagedb -component $component "; 213 $command .= " -outdir $out_dir"; 214 $command .= " -need_magic" if $need_magic; 215 216 my $rlabel = "dq_ud_" . $label if $label; 217 $command .= " -rlabel $rlabel" if $rlabel; 218 219 if (!$no_update) { 220 my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) = 221 run(command => $command, verbose => $verbose); 222 unless ($success) { 223 my_die("$command failed", $PS_EXIT_UNKNOWN_ERROR); 224 } 225 my $output = join "", @$stdout_buf; 226 chomp $output; 227 $dep_id = $output; 228 229 my_die("pstamptool -getdependent returned invalid dep_id", $PS_EXIT_PROG_ERROR) if !$dep_id; 230 } 231 else { 232 print STDERR "skipping $command\n"; 233 $dep_id = 42; 234 } 235 236 return($dep_id); 237 } 238 239 240 241 sub my_die { 242 my $msg = shift; 243 my $fault = shift; 244 245 carp $msg; 246 247 # we don't fault the request here pstamp_parser_run.pl handles that if necessary 248 exit $fault; 249 }
Note:
See TracChangeset
for help on using the changeset viewer.
