IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
May 4, 2010, 4:10:59 PM (16 years ago)
Author:
watersc1
Message:

Fix typos in pstamptool database queries.
Add pstampDependent.outdir entry to changes.txt.

Final tested code for detectability server.
Works on test cases that involve updates.

r

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/pstamp/scripts/dqueryparse.pl

    r27578 r27859  
    22#
    33# 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 file
    6 # and adds a completed job to the database
    74#
    85
    96use strict;
    107use warnings;
    11 
     8use Carp;
    129use Getopt::Long qw( GetOptions );
    1310use Pod::Usage qw( pod2usage );
    1411use IPC::Cmd 0.36 qw( can_run run );
    1512
     13use PS::IPP::PStamp::RequestFile qw( :standard );
     14use PS::IPP::PStamp::Job qw( :standard );
    1615use PS::IPP::Config qw($PS_EXIT_SUCCESS
    1716                       $PS_EXIT_UNKNOWN_ERROR
     
    2625                       );
    2726
     27my ($no_update, $imagedb, $label);
    2828my ($req_file, $req_id, $out_dir, $product, $mode, $dbname, $dbserver, $verbose, $save_temps);
    29 
     29my ($job_id,$rownum); # stuff from the post-update world
    3030#
    3131# parse args
     
    3535        'file=s'          =>      \$req_file,
    3636        'req_id=s'        =>      \$req_id,
     37        'job_id=s'        =>      \$job_id,
     38        'rownum=s'        =>      \$rownum,
    3739        'out_dir=s'       =>      \$out_dir,
     40        'label=s'         =>      \$label,
    3841        'product=s'       =>      \$product,
    3942        'mode=s'          =>      \$mode,
     
    4447) or pod2usage(2);
    4548
    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 
     49die "invalid mode '$mode'" unless ($mode eq "list_uri") or ($mode eq "queue_job");
     50die "--file or --job_id is required" if !$req_file;
     51
     52if ($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}
    6457my $missing_tools;
    6558my $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);
     59my $detectresponse = can_run('detectability_respond.pl') or
     60    (warn "Can't find detectability_respond.pl" and $missing_tools = 1);
    6761my $fields = can_run('fields') or (warn "Can't find fields" and $missing_tools =1);
    6862
     
    7771}
    7872
     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.
     81if (!$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
    7988# get the query id and check the extname and version from the header
    80 my $fields_output;
     89my $fields_output; 
    8190{
    8291    my $command = "echo $req_file | $fields -x 0 EXTNAME EXTVER QUERY_ID";
     
    8493        run(command => $command, verbose => $verbose);
    8594   
    86 #   fields doesn't return zero when it succeeds
    87 #    unless ($success) {
    88 #        print STDERR @$stderr_buf;
    89 #    }
    9095    $fields_output = join "", @$stdout_buf;
    9196}
    9297my (undef, $extname, $extver, $req_name) = split " ", $fields_output;
    9398
    94 die "$req_file is missing one of EXTNAME EXTVER or QUERY_ID"
     99my_die("$req_file is missing one of EXTNAME EXTVER or QUERY_ID", $PS_EXIT_PROG_ERROR)
    95100    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 }
     101my_die("$req_file has EXTNAME $extname not MOPS_DETECTABILITY_QUERY table", $PS_EXIT_PROG_ERROR)
     102    if $extname ne "MOPS_DETECTABILITY_QUERY";
     103my_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.
    104107if (! -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);
    106109} 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.
     115my $response_file = "$out_dir/${req_name}.dresponse.${req_id}.fits";
    120116my $fault;
     117my $data_to_update = '';
    121118{
    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";
    123120    $command .= " --save-temps" if $save_temps;
    124121    $command .= " --verbose" if $verbose;
     
    127124        run(command => $command, verbose => $verbose);
    128125    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.
    131129    $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
    135135my $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.
     138if ($fault == 0) {
    137139    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";
    139141    $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);
    146145    if ($success) {
    147146        $job_id = join "", @$stdout_buf;
     
    156155    }
    157156}
    158 
    159 
     157else {
     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.
    160187{
    161188    my $command = "$pstamptool -updatereq -req_id $req_id -name $req_name -outProduct $product";
    162189    $command .= " -fault $result" if $result;
    163     $command .= " -dbname $dbname" if $dbname;
    164     $command .= " -dbserver $dbserver" if $dbserver;
    165190
    166191    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
    167192        run(command => $command, verbose => $verbose);
    168193    unless ($success) {
    169         die "$command failed";
     194        my_die("$command failed",$PS_EXIT_UNKNOWN_ERROR);
    170195    }
    171196}
    172197 
    173198exit 0;
     199
     200
     201# If we have to queue an update run, do so and create a new dependent
     202sub 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
     241sub 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.