IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 26151 for trunk/pstamp


Ignore:
Timestamp:
Nov 15, 2009, 1:53:33 PM (17 years ago)
Author:
bills
Message:

implementation for "get_image" jobs

Location:
trunk/pstamp/scripts
Files:
4 edited

Legend:

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

    r20220 r26151  
    1616use File::Basename;
    1717use Digest::MD5::File qw( file_md5_hex );
     18use IPC::Cmd 0.36 qw( can_run run );
    1819
    19 use PS::IPP::Config qw($PS_EXIT_SUCCESS
    20                        $PS_EXIT_UNKNOWN_ERROR
    21                        $PS_EXIT_SYS_ERROR
    22                        $PS_EXIT_CONFIG_ERROR
    23                        $PS_EXIT_PROG_ERROR
    24                        $PS_EXIT_DATA_ERROR
    25                        $PS_EXIT_TIMEOUT_ERROR
    26                        metadataLookupStr
    27                        metadataLookupBool
    28                        caturi
    29                        );
     20
     21use PS::IPP::Config qw( :standard );
     22
    3023my $product;
    3124my $fileset;
    3225
    33 my $uri;
    34 my $out_dir;
     26my $output_base;
    3527
    36 my $verbose = 1;
     28my $verbose;
    3729my $ipprc;
    3830my $dbname;
     31my $dbserver;
    3932my $job_id;
    4033my $rownum;
     
    4740        'job_id=s'        =>      \$job_id,
    4841        'rownum=s'        =>      \$rownum,
    49         'uri=s'           =>      \$uri,
    50         'out_dir=s'       =>      \$out_dir,
     42        'output_base=s'   =>      \$output_base,
    5143        'dbname=s'        =>      \$dbname,
     44        'dbserver=s'      =>      \$dbserver,
     45        'verbose'         =>      \$verbose,
    5246) or pod2usage(2);
    5347
     
    5549$err .= "--job_id is required\n" if (!$job_id);
    5650$err .= "--rownum is required\n" if (!$rownum);
    57 $err .= "--uri is required to specify the file list\n" if (!$uri);
    58 $err .= "--out_dir is required to specify the output fileset\n" if (!$out_dir);
     51$err .= "--output_base is required to specify the output fileset\n" if (!$output_base);
    5952
    6053die $err if $err;
    6154
    62 # collapse any multiple slashes in output_dir
    63 $out_dir = File::Spec->canonpath($out_dir);
     55my $params_file = $output_base . ".params";
     56if (! open(INPUT, "<$params_file") ) {
     57    die("failed to open params file: $params_file");
     58}
    6459
    65 my @path = split(/\//, $out_dir);
     60my $params = join "", (<INPUT>);
    6661
    67 my $nelem = @path;
    68 if ($nelem < 2) {
    69     die("$out_dir is not a valid output fileset directory");
     62print STDERR "\nparams is $params\n";
     63
     64my ($stage, $stage_id, $component, $path_base, $camera) = split " ", $params;
     65if (!$camera or !$path_base or !$component or !$stage_id or !$stage) {
     66   die "failed to parse params: $params";
    7067}
    71 $fileset = $path[$nelem-1];
    72 $product = $path[$nelem-2];
    7368
    74 $_ = $out_dir;
    75 my ($ds_dir) = m%(.*)/$product/$fileset%;
     69print STDERR "\nCAMERA is $camera\n";
    7670
    77 if ($verbose) {
    78     print STDERR "DS root:  $ds_dir\n";
    79     print STDERR "Product:  $product\n";
    80     print STDERR "Fileset:  $fileset\n";
    81     print STDERR "Filelist: $uri\n";
     71my $out_dir = dirname($output_base);
     72my $prefix = basename($output_base);
     73my $results_file = $output_base . ".bundle_results";
     74
     75# Look for programs we need
     76my $missing_tools;
     77my $dist_bundle   = can_run('dist_bundle.pl') or (warn "Can't find dist_bundle.pl" and $missing_tools = 1);
     78if ($missing_tools) {
     79    warn("Can't find required tools.");
     80    exit($PS_EXIT_CONFIG_ERROR);
    8281}
    8382
    8483
    85 if (!stat("$ds_dir/index.txt")) {
    86     $err .= "Data Store not found at '$ds_dir'.\n"
     84{
     85    my $command = "$dist_bundle --camera $camera --stage $stage --stage_id $stage_id --component $component";
     86    $command .= " --path_base $path_base --outdir $out_dir --results_file $results_file";
     87    $command .= " --prefix $prefix";
     88    # ignore magic for now
     89    $command .= " --no_magic";
     90
     91    $command .= " --dbname $dbname" if $dbname;
     92    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
     93        run(command => $command, verbose => $verbose);
     94    unless ($success) {
     95        die("Unable to perform $command: $error_code");
     96    }
    8797}
    8898
    89 show_usage("Invalid product '$product'.")
    90     unless defined stat("$ds_dir/$product/index.txt");
    91 
    92 if (! open(INPUT, "<$uri") ) {
    93     die("failed to open file list: $uri");
     99if (! open(RESULTS, "<$results_file")) {
     100    die("failed to open bundle results file: $results_file");
    94101}
    95102
    96 my $reglist = "$out_dir/reglist";
    97 if (! open(REGLIST, ">$reglist$job_id") ) {
     103my $file_name;
     104my $bytes;
     105my $md5sum;
     106foreach my $line (<RESULTS>) {
     107    chomp $line;
     108    next if !$line;
     109    next if $line =~ /bundleResults/;
     110    last if $line =~ /END/;
     111
     112    my ($tag, $type, $val) = split " ", $line;
     113    if ($tag eq "name") {
     114        $file_name = $val;
     115    } elsif ($tag eq "bytes") {
     116        $bytes = $val;
     117    } elsif ($tag eq "md5sum") {
     118        $md5sum = $val;
     119    } else {
     120        die("unexpected tag: $tag  found in results file: $results_file");
     121    }
     122}
     123
     124my $reglist = "$out_dir/reglist$job_id";
     125if (! open(REGLIST, ">$reglist") ) {
    98126    die("failed to open registration list: $reglist");
    99127}
    100128
    101 my @files;
    102 my $num = 0;
    103 while (<INPUT>) {
    104     chomp;
    105     my @fields = split /\|/;
    106     my $pathname = shift @fields;
    107     my $filetype = shift @fields;
    108     my $class_id = shift @fields;
    109 
    110     ($pathname , my $filename) = resolvepath($pathname);
    111 
    112     if ($verbose) {
    113         print STDERR "$pathname @fields\n";
    114     }
    115 
    116     $num++;
    117     $filename = "${rownum}_${num}_$filename";
    118     my $dest_path = "$out_dir/$filename";
    119     if (! copy $pathname, "$dest_path" ) {
    120         die("failed trying to copy $pathname to $out_dir");
    121     }
    122 
    123     # XXX is pstamp always the right file type, if not where can we get the right one?
    124     print REGLIST file_registration_line($filename, $dest_path, $filetype, $class_id);
    125 
    126     foreach my $f (@fields) {
    127         print REGLIST "$f|";
    128     }
    129     print REGLIST "\n";
    130 }
     129print REGLIST "$file_name|$bytes|$md5sum|tgz|\n";
    131130
    132131close(REGLIST);
    133132
    134133exit 0;
    135 
    136 sub resolvepath {
    137     my $pathname = $_[0];
    138 
    139     # use the basename of the unresolved file as the name of the file
    140     # in order to avoid nebulous name mangled paths in the datastore
    141     my $file = basename($pathname);
    142 
    143     my $slash = index($pathname, "/");
    144     if ($slash != 0) {
    145         if (!$ipprc) {
    146             $ipprc = PS::IPP::Config->new();
    147         }
    148         $pathname = $ipprc->file_resolve($pathname);
    149     }
    150 
    151 
    152     return ($pathname, $file);
    153 }
    154 
    155 # XXX move this to a module so the code can be shared
    156 sub file_registration_line {
    157     my $filename = shift;
    158     my $path     = shift;
    159     my $filetype = shift;
    160     my $chipname = shift;
    161     $chipname = $chipname ? "$chipname|" : "";
    162 
    163     if (-e $path) {
    164         my @finfo = stat($path);
    165         die "failed to stat $path" unless (@finfo);    # XXX clean up
    166         my $bytes = $finfo[7];
    167         my $md5sum = file_md5_hex($path);
    168 
    169         return "$filename|$bytes|$md5sum|$filetype|$chipname";
    170     } else {
    171         die "$filename not found at $path";
    172     }
    173 }
  • trunk/pstamp/scripts/pstamp_job_run.pl

    r25712 r26151  
    148148    }
    149149} elsif ($jobType eq "get_image") {
    150     my_die( "get_image jobs not working right now", $job_id, $PS_EXIT_PROG_ERROR);
    151150
    152151    my $uri = "";
    153     my $command = "$pstamp_get_image_job --job_id $job_id --uri $uri --out_dir $outputBase --rownum $rownum";
     152    my $command = "$pstamp_get_image_job --job_id $job_id --output_base $outputBase --rownum $rownum";
    154153    $command .= " --dbname $dbname" if $dbname;
    155154    $command .= " --dbserver $dbserver" if $dbserver;
  • trunk/pstamp/scripts/pstamp_parser_run.pl

    r25319 r26151  
    1313use Getopt::Long qw( GetOptions );
    1414use File::Basename qw( basename dirname);
     15use File::Copy;
    1516use POSIX qw( strftime );
    1617use Carp;
     
    119120        unlink $new_uri or my_die("failed to unlink $new_uri", $req_id, $PS_EXIT_UNKNOWN_ERROR);
    120121    }
    121     if (! symlink $uri, $new_uri) {
    122         my_die ("failed to link request file $uri to workdir $workdir", $req_id, $PS_EXIT_UNKNOWN_ERROR);
     122    if (! copy $uri, $new_uri) {
     123        my_die ("failed to copy request file $uri to workdir $workdir", $req_id, $PS_EXIT_UNKNOWN_ERROR);
    123124    }
    124125}
  • trunk/pstamp/scripts/pstampparse.pl

    r26083 r26151  
    9898
    9999# make sure the file contains what we are expecting
    100 
    101 # we shouldn't get here if this is the case so just die. No need to notify the client
     100# This program shouldn't have been run if the request file is bogus.
     101# No need to notify the client
    102102my_die("$request_file_name is not a PS1_PS_REQEST", $PS_EXIT_PROG_ERROR) if $extname ne "PS1_PS_REQUEST";
    103103my_die("REQ_NAME not found in $request_file_name", $PS_EXIT_PROG_ERROR)  if (!$req_name);
     
    165165    # XXX: TODO: sanity check all parameters
    166166
    167     # XXX: TODO: We shouldn't really just die in this loop.
    168     # If we encounter an error for a particular row
    169     # add a job with the proper fault code. If there is a db or config error we should probably just
    170     # trash the request.
     167    # If we encounter an error for a particular row add a job with the proper fault code.
     168    # If we encounter an error in this loop we shouldn't really just die.
     169    # We only do that now in the case of I/O or DB errors or the like.
    171170
    172171    my $rownum   = $row->{ROWNUM};
     
    236235    }
    237236
    238     # collect rows with the same images of interest in a list so that they
    239     # can be looked up together
     237    # For "stamp" and "list_uri" jobs collect rows with the same images of interest in a list so that they
     238    # can be looked up together.
    240239    if (@rowList) {
    241240        my $firstRow = $rowList[0];
    242         # note order of these parameters matters
    243         if (same_images_of_interest($firstRow, $row)) {
     241        if (($firstRow->{JOB_TYPE} ne "get_image") and same_images_of_interest($firstRow, $row)) {
    244242
    245243            # add this row to the list and move on
     
    305303    my $have_skycells = shift;
    306304    my $need_magic = shift;
     305    my $mode = shift;
    307306
    308307    my $num_jobs = 0;
     
    476475        }
    477476    } elsif ($job_type eq "get_image") {
    478         print STDERR "get_image jobs not implemented yet" if $verbose;
    479         foreach my $row (@$rowList) {
    480             insertFakeJobForRow($row, 1, $PSTAMP_NOT_IMPLEMENTED);
    481             $num_jobs++;
    482         }
     477        my $n = scalar @$rowList;
     478
     479        my_die( "error: unexpected number of rows for get_image request: $n", $PS_EXIT_PROG_ERROR) if $n != 1;
     480
     481        $num_jobs = queueGetImageJobs($firstRow, $imageList, $stage, $need_magic, $mode);
     482
    483483    } else {
    484484        if (!$imageList or (scalar @$imageList eq 0)) {
     
    582582           
    583583            foreach my $row (@$rowList) {
    584                 $num_jobs += queueJobsForRow($row, $stage, $thisRun, $have_skycells, $need_magic);
     584                $num_jobs += queueJobsForRow($row, $stage, $thisRun, $have_skycells, $need_magic, $mode);
    585585            }
    586586        }
     587    }
     588    return $num_jobs;
     589}
     590
     591#        $num_jobs = queueGetImageJobs($firstRow, $imageList, $stage, $need_magic, $mode);
     592sub queueGetImageJobs
     593{
     594    my $row = shift;
     595    my $imageList = shift;
     596    my $stage = shift;
     597    my $need_magic = shift;
     598    my $mode = shift;
     599
     600    my $num_jobs = 0;
     601    my $rownum = $row->{ROWNUM};
     602    my $option_mask = $row->{OPTION_MASK};
     603
     604    # For dist_bundle we need
     605    #  --camera from $image
     606    #  --stage
     607    #  --stage_id from $image
     608    #  --component from $image
     609    #  --path_base
     610    #  --outdir global to this script
     611
     612    # loop over images
     613    my $job_num = 0;
     614    foreach my $image (@$imageList) {
     615        my $stage_id = $image->{stage_id};
     616        my $component = $image->{component};
     617
     618        # skip faulted components for now. Should we even be here?
     619        if ($image->{fault} > 0) {
     620            printf STDERR "skipping faulted component for $stage $stage_id $component\n" if $verbose;
     621            next;
     622        }
     623
     624        $job_num++;
     625
     626        my $imagefile = $image->{image};
     627        if (($stage ne "stack") and ($need_magic and !$image->{magicked})) {
     628            # we only get here if req_type is (byid or byexp). For other types the test for magicked is performed
     629            # in locate_images because it's much more efficient to do the test in the database.
     630            # For these two modes we fall through to here in order to give feedback to the requestor as
     631            # to why the request failed to queue jobs.
     632            print STDERR "skipping non-magicked image $imagefile\n" if $verbose;
     633            insertFakeJobForRow($row, $job_num, $PSTAMP_NOT_DESTREAKED);
     634            $num_jobs++;
     635
     636            next;
     637        }
     638        my $exp_id = $image->{exp_id};
     639           
     640        my $output_base = "$out_dir/${rownum}_${job_num}_";
     641        my $params = "${output_base}.params";
     642
     643        # copy the argument list to a file
     644        open PARAMS, ">$params" or my_die("failed to open $params", $PS_EXIT_UNKNOWN_ERROR);
     645        print PARAMS "$stage $image->{stage_id} $image->{component} $image->{path_base} $image->{camera}\n";
     646        close PARAMS or my_die("failed to close $params", $PS_EXIT_UNKNOWN_ERROR);
     647
     648        my $newState = "run";
     649        my $fault = 0;
     650        my $dep_id;
     651
     652        if ($stage ne 'raw') {
     653            my $run_state = $image->{state};
     654            my $data_state = $image->{data_state};
     655            $data_state = $run_state if $stage eq "stack";
     656            if (($run_state eq 'goto_purged') or ($data_state eq 'purged') or
     657                ($run_state eq 'goto_scrubbed') or ($data_state eq 'scrubbed')) {
     658                # image is gone and it's not coming back
     659                $newState = 'stop';
     660                $fault = $PSTAMP_GONE;
     661            } elsif (($data_state ne 'full') or ($run_state ne 'full' )) {
     662                # don't wait for update unless the caller asks us to
     663                if (!($option_mask & $PSTAMP_WAIT_FOR_UPDATE)) {
     664                    $newState = 'stop';
     665                    $fault = $PSTAMP_NOT_AVAILABLE;
     666                } else {
     667                    # cause the image to be re-made
     668                    # set up to queue an update run
     669                    queue_update_run(\$newState, \$fault, \$dep_id, $image->{image_db},
     670                        $run_state, $stage, $image->{stage_id}, $need_magic, $image->{label});
     671                }
     672            }
     673        }
     674
     675        $num_jobs++;
     676        my $command = "$pstamptool -addjob  -req_id $req_id -job_type $row->{JOB_TYPE}"
     677                        . " -outputBase $output_base -rownum $rownum -state $newState -options $option_mask";
     678        $command .= " -fault $fault" if $fault;
     679        $command .= " -exp_id $exp_id" if $exp_id;
     680        $command .= " -dep_id $dep_id" if $dep_id;
     681
     682        if ($mode eq "list_job") {
     683            # this is a debugging mode, just print the pstamptool that would have run
     684            # this is sort of like the mode -noupdate that some other tools support
     685            print "$command\n";
     686        } elsif (!$no_update) {
     687            # mode eq "queue_job"
     688            my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
     689                run(command => $command, verbose => $verbose);
     690            unless ($success) {
     691                print STDERR @$stderr_buf;
     692                # XXX TODO: now what? Should we mark the error state for the request?
     693                # should we keep going for other uris? If so how do we report that some
     694                # of the work that the request wanted isn't going to get done
     695                my_die("failed to queue job for request $req_id", $PS_EXIT_UNKNOWN_ERROR);
     696            }
     697        } else {
     698            print "skipping command: $command\n";
     699        }
     700    }
     701    if ( $num_jobs == 0 ) {
     702        print STDERR "no jobs for row $rownum\n" if $verbose;
     703        insertFakeJobForRow($row, 1, $PSTAMP_NO_JOBS_QUEUED);
     704        $num_jobs = 1;
    587705    }
    588706    return $num_jobs;
     
    677795    return 0 if ($r1->{REQ_TYPE} ne $r2->{REQ_TYPE});
    678796    return 0 if ($r1->{IMG_TYPE} ne $r2->{IMG_TYPE});
    679     return 0 if ($r1->{ID} ne $r2->{ID});
    680     return 0 if ($r1->{TESS_ID} ne $r2->{TESS_ID});
    681     return 0 if ($r1->{REQFILT} ne $r2->{REQFILT});
    682     return 0 if ($r1->{LABEL} ne $r2->{LABEL});
    683     return 0 if ($r1->{MJD_MIN} ne $r2->{MJD_MAX});
    684     return 0 if ($r1->{MJD_MAX} ne $r2->{MJD_MAX});
    685     return 0 if ($r1->{inverse} ne $r2->{inverse});
     797    return 0 if ($r1->{ID}       ne $r2->{ID});
     798    return 0 if ($r1->{TESS_ID}  ne $r2->{TESS_ID});
     799    return 0 if ($r1->{REQFILT}  ne $r2->{REQFILT});
     800    return 0 if ($r1->{LABEL}    ne $r2->{LABEL});
     801    return 0 if ($r1->{MJD_MIN}  ne $r2->{MJD_MAX});
     802    return 0 if ($r1->{MJD_MAX}  ne $r2->{MJD_MAX});
     803    return 0 if ($r1->{inverse}  ne $r2->{inverse});
    686804
    687805    if (defined($r1->{COMPONENT})) {
Note: See TracChangeset for help on using the changeset viewer.