IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
May 3, 2010, 8:45:22 AM (16 years ago)
Author:
eugene
Message:

updates from trunk

Location:
branches/simmosaic_branches
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/simmosaic_branches

  • branches/simmosaic_branches/pstamp/scripts/pstamp_job_run.pl

    r24831 r27839  
    1111use Sys::Hostname;
    1212use Getopt::Long qw( GetOptions :config auto_help auto_version gnu_getopt );
     13use Carp;
    1314use File::Basename;
     15use File::Copy;
    1416use Digest::MD5::File qw( file_md5_hex );
    1517use PS::IPP::PStamp::RequestFile qw( :standard );
    16 
    17 my $verbose;
    18 my $dbname;
    19 my $dbserver;
    20 my $job_id;
    21 my $redirect_output;
    22 my $output_base;
    23 
    24 my $host = hostname();
    25 
     18use IPC::Cmd 0.36 qw( can_run run );
     19use POSIX;
     20
     21use PS::IPP::Metadata::Config;
     22#use PS::IPP::Metadata::Stats;
     23use PS::IPP::Metadata::List qw( parse_md_list );
     24
     25use PS::IPP::Config qw( :standard );
     26
     27my ($job_id, $redirect_output, $outputBase, $rownum, $jobType, $options);
     28my ($verbose, $dbname, $dbserver, $no_update);
    2629
    2730GetOptions(
    2831    'job_id=s'          =>  \$job_id,
    29     'output_base=s'     =>  \$output_base,
    30     'redirect-output'   => \$redirect_output,
     32    'job_type=s'        =>  \$jobType,
     33    'rownum=s'          =>  \$rownum,
     34    'output_base=s'     =>  \$outputBase,
     35    'options=s'         =>  \$options,
     36    'redirect-output'   =>  \$redirect_output,
    3137    'dbname=s'          =>  \$dbname,
    3238    'dbserver=s'        =>  \$dbserver,
    3339    'verbose'           =>  \$verbose,
     40    'no-update'         =>  \$no_update,
    3441);
    3542
    3643
     44my $host = hostname();
    3745if ($verbose) {
    3846    print "\n\n";
     
    4149
    4250die "job_id is required" if !$job_id;
    43 die "output_base is required" if !$output_base;
    44 
    45 use IPC::Cmd 0.36 qw( can_run run );
    46 
    47 use PS::IPP::Metadata::Config;
    48 use PS::IPP::Metadata::Stats;
    49 use PS::IPP::Metadata::List qw( parse_md_list );
    50 
    51 use PS::IPP::Config qw($PS_EXIT_SUCCESS
    52                        $PS_EXIT_UNKNOWN_ERROR
    53                        $PS_EXIT_SYS_ERROR
    54                        $PS_EXIT_CONFIG_ERROR
    55                        $PS_EXIT_PROG_ERROR
    56                        $PS_EXIT_DATA_ERROR
    57                        $PS_EXIT_TIMEOUT_ERROR
    58                        metadataLookupStr
    59                        metadataLookupBool
    60                        caturi
    61                        );
     51
     52my_die( "job_type is required", $job_id, $PS_EXIT_PROG_ERROR) if !$jobType;
     53my_die("rownum is required", $job_id, $PS_EXIT_PROG_ERROR) if !$rownum;
     54my_die("output_base is required", $job_id, $PS_EXIT_PROG_ERROR) if !$outputBase;
     55
     56$options = 1 if !$options;
     57
     58
    6259my $ipprc = PS::IPP::Config->new(); # IPP Configuration
    63 
    6460if ($redirect_output) {
    65     my $logDest = "$output_base.log";
    66     $ipprc->redirect_output($logDest);
    67 }
     61    my $logDest = "$outputBase.log";
     62    $ipprc->redirect_output($logDest)
     63        or my_die ("unable to redirect output to $logDest", $job_id, $PS_EXIT_UNKNOWN_ERROR);
     64}
     65
    6866
    6967if (!$dbserver) {
     
    7876
    7977if ($missing_tools) {
    80     warn("Can't find required tools.");
    81     exit ($PS_EXIT_CONFIG_ERROR);
     78    my_die("Can't find required tools", $job_id, $PS_EXIT_CONFIG_ERROR);
    8279}
    8380
    8481my $mdcParser = PS::IPP::Metadata::Config->new; # Parser for metadata config files
    8582
    86 my $psjob;
    87 #Look up the uri for the given job
    88 {
    89     my $command = "$pstamptool -pendingjob -job_id $job_id";
     83my $jobStatus;
     84if ($jobType eq "stamp") {
     85    my $argslist = "$outputBase.args";
     86    open ARGSLIST, "<$argslist" or my_die("failed to open argslist file $argslist", $job_id, $PS_EXIT_UNKNOWN_ERROR);
     87    my $argString = <ARGSLIST>;
     88    close ARGSLIST;
     89    chomp $argString;
     90
     91    # XXX: should we do any other sanity checking?
     92    my_die("arglist file $argslist is empty", $job_id, $PS_EXIT_DATA_ERROR) if !$argString;
     93
     94    my $command = "$ppstamp $outputBase $argString";
    9095    $command .= " -dbname $dbname" if $dbname;
    9196    $command .= " -dbserver $dbserver" if $dbserver;
    9297    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
    9398        run(command => $command, verbose => $verbose);
    94     unless ($success) {
    95         die("Unable to perform pstamptool -pendingjob: $error_code");
    96     }
    97 
    98     if (@$stdout_buf == 0) {
    99         print STDERR "pending pstamp job id $job_id not found\n";
    100         exit 0;
    101     }
    102     my $metadata = $mdcParser->parse(join "", @$stdout_buf) or
    103         die("Unable to parse metdata config doc");
    104 
    105     my $jobs = parse_md_list($metadata);
    106     my $num = @$jobs;
    107     die "unexpected number of jobs $num found for job: $job_id" if $num > 1;
    108 
    109     $psjob = $jobs->[0];
    110 }
    111 
    112 if (!$psjob) {
    113     print STDERR "postage stamp job $job_id not found\n";
    114     exit 1;
    115 }
    116 
    117 my $rownum = $psjob->{rownum};
    118 my $uri = $psjob->{uri};
    119 my $outputBase = $psjob->{outputBase};
    120 my $argString = $psjob->{args};
    121 my $jobType = $psjob->{jobType};
    122 
    123 my $jobStatus;
    124 if ($jobType eq "stamp") {
    125     my $command = "$ppstamp -file $uri $outputBase $argString";
     99
     100    my $exitStatus;
     101    if (WIFEXITED($error_code)) {
     102        $exitStatus = WEXITSTATUS($error_code);
     103    } else {
     104        print STDERR "ppstamp failed error_code: $error_code\n";
     105        $exitStatus = $PS_EXIT_SYS_ERROR;
     106    }
     107
     108    if ($exitStatus == 0) {
     109        my $dir = dirname($outputBase);
     110
     111        my $reglist = "$dir/reglist$job_id";
     112
     113        my $F;
     114        open $F, ">$reglist" or my_die( "can't open $reglist for output", $job_id, $PS_EXIT_UNKNOWN_ERROR);
     115
     116        # Figure out what output images were produced
     117
     118        # Note: we are assuming the contents of the PSTAMP filerules here.
     119        my %extensions = ( $PSTAMP_SELECT_IMAGE  => "fits",
     120                           $PSTAMP_SELECT_MASK   => "mk.fits",
     121                           $PSTAMP_SELECT_WEIGHT => "wt.fits");
     122
     123        my $output_mask = $options & ($PSTAMP_SELECT_IMAGE | $PSTAMP_SELECT_MASK | $PSTAMP_SELECT_WEIGHT);
     124
     125        foreach my $key (keys (%extensions)) {
     126            my $do_this_one = $key & $output_mask;
     127
     128            next if (! $do_this_one);
     129
     130            my $extension = $extensions{$key};
     131
     132            my $basename = basename($outputBase);
     133
     134            my $filename = "${basename}.${extension}";
     135            my $path  = "${outputBase}.${extension}";
     136
     137            # XXX is pstamp always the right file type, if not where do we get the right one?
     138            print $F file_registration_line($filename, $path, "pstamp") . "\n";
     139        }
     140
     141        get_other_outputs($F, $outputBase, $options);
     142
     143        close $F;
     144        $jobStatus = $PS_EXIT_SUCCESS;
     145    } elsif ($exitStatus == $PSTAMP_NO_OVERLAP) {
     146        $jobStatus = $PSTAMP_NO_OVERLAP;
     147    } else {
     148        my_die( "ppstamp failed with error code: $exitStatus", $job_id, $exitStatus);
     149    }
     150} elsif ($jobType eq "get_image") {
     151
     152    my $uri = "";
     153    my $command = "$pstamp_get_image_job --job_id $job_id --output_base $outputBase --rownum $rownum";
     154    $command .= " --dbname $dbname" if $dbname;
     155    $command .= " --dbserver $dbserver" if $dbserver;
     156    $command .= " --verbose" if $verbose;
     157    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
     158        run(command => $command, verbose => $verbose);
     159
     160    if ($success) {
     161        $jobStatus = $PS_EXIT_SUCCESS;
     162    } else {
     163        $jobStatus = $error_code >> 8;
     164        my_die( "pstamp_get_image_job failed with error code: $jobStatus", $job_id, $jobStatus);
     165    }
     166} elsif ($jobType eq "detect_query") {
     167    my_die("detect_query jobs not supported yet", $job_id,$PS_EXIT_CONFIG_ERROR);
     168} else {
     169    my_die("unknown jobType $jobType found", $job_id, $PS_EXIT_PROG_ERROR);
     170}
     171
     172# mark the job stopped in the database
     173{
     174    my $command = "$pstamptool -updatejob -job_id $job_id -state stop";
     175    $command .= " -fault $jobStatus" if $jobStatus;
    126176    $command .= " -dbname $dbname" if $dbname;
    127177    $command .= " -dbserver $dbserver" if $dbserver;
    128     my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
    129         run(command => $command, verbose => $verbose);
    130 
    131     if ($success) {
    132         # XXX shouldn't need to do this, review schema`
    133         my $dir = dirname($outputBase);
    134 
    135         my $reglist = "$dir/reglist$job_id";
    136 
    137         open F, ">$reglist" or die "can't open $reglist for output";
    138 
    139         # figure out what output images we should expect
    140 
    141         # Note: we are assuming the contents of the filerules here.
    142         my @extensions = ( "fits", "mk.fits", "wt.fits");
    143 
    144         # we always require an image
    145         my $output_mask = $PSTAMP_SELECT_IMAGE;
    146 
    147         # we search the argString for -mask and -weight.
    148         # searching the arg string allows us to avoid adding a column in pstampJob
    149         # XXX yeah but does this make us susceptible to user input fouling us up?
    150         # For example, what if somebody sets the output base to file-mask-weight
    151         # TODO: clean this up
    152         $output_mask |= $PSTAMP_SELECT_MASK   if ($argString =~ /-mask/);
    153         $output_mask |= $PSTAMP_SELECT_WEIGHT if ($argString =~ /-weight/);
    154 
    155         # XXX we're getting a bit intimate with the bit field definitions here.
    156         # do better use a hash
    157 
    158         my $m = 1;
    159         foreach my $extension (@extensions) {
    160             my $do_this_one = $m & $output_mask;
    161             $m = $m << 1;
    162             next if (! $do_this_one);
    163 
    164             my $basename = basename($outputBase);
    165 
    166             my $filename = "${basename}.${extension}";
    167             my $path  = "${outputBase}.${extension}";
    168 
    169             # XXX is pstamp always the right file type, if not where do we get the right one?
    170             print F file_registration_line($filename, $path, "pstamp") . "\n";
    171         }
    172 
    173         close F;
    174         $jobStatus = $PS_EXIT_SUCCESS;
    175     } else {
    176         $jobStatus = $error_code >> 8;
    177         print STDERR "ppstamp failed with error code: $jobStatus\n";
    178     }
    179 } elsif ($jobType eq "get_image") {
    180     my $command = "$pstamp_get_image_job --job_id $job_id --uri $uri --out_dir $outputBase --rownum $rownum";
    181     $command .= " --dbname $dbname" if $dbname;
    182     $command .= " --dbserver $dbserver" if $dbserver;
    183     my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
    184         run(command => $command, verbose => $verbose);
    185 
    186     if ($success) {
    187         $jobStatus = $PS_EXIT_SUCCESS;
    188     } else {
    189         $jobStatus = $error_code >> 8;
    190         print STDERR "ppstamp failed with error code: $jobStatus\n";
    191     }
    192 } elsif ($jobType eq "detect_query") {
    193     die("multiple detect_query jobs not supported yet");
    194 } else {
    195     die("unknown jobType $jobType found");
    196 }
    197 
    198 # stop the job and set the result value
    199 {
    200     my $command = "$pstamptool -updatejob -job_id $job_id -state stop -fault $jobStatus";
    201     $command .= " -dbname $dbname" if $dbname;
    202     $command .= " -dbserver $dbserver" if $dbserver;
    203     my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
    204         run(command => $command, verbose => $verbose);
    205     unless ($success) {
    206         die("Unable to perform $command: $error_code");
    207     }
    208 }
    209 
    210 exit $jobStatus;
     178    if (!$no_update) {
     179        my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
     180            run(command => $command, verbose => $verbose);
     181        unless ($success) {
     182            die("Unable to perform $command: $error_code");
     183        }
     184    } else {
     185        print STDERR "skipping command: $command\n"
     186    }
     187}
     188
     189exit 0;
    211190
    212191# create a string to be passed as input to dsreg when registering this file in a fileset
     
    219198    if (-e $path) {
    220199        my @finfo = stat($path);
    221         die "failed to stat $path" unless (@finfo);    # XXX clean up
     200        my_die("failed to stat $path", $job_id, $PS_EXIT_UNKNOWN_ERROR) unless (@finfo);
    222201        my $bytes = $finfo[7];
    223202        my $md5sum = file_md5_hex($path);
     
    225204        return "$filename|$bytes|$md5sum|$filetype|";
    226205    } else {
    227         die "$filename not found at $path";
    228     }
    229 }
     206        my_die("$filename not found at $path", $job_id, $PS_EXIT_UNKNOWN_ERROR);
     207    }
     208}
     209       
     210sub get_other_outputs {
     211    my $f = shift;
     212    my $output_base = shift;
     213    my $options = shift;
     214
     215    if ($options & ( $PSTAMP_SELECT_CMF | $PSTAMP_SELECT_PSF | $PSTAMP_SELECT_BACKMDL)) {
     216        my $comp = read_params_file($output_base);
     217
     218        my $stage = $comp->{stage};
     219
     220        # raw files don't have any other data products
     221        return 1 if $stage eq "raw";
     222
     223        # add the other data products if they are selected and exist.
     224        # silently skip them if they don't exist. Perhaps this should be
     225        # detected in pstampparse so that the user can be notified with
     226        # a message in parse_error.txt ("warp do not have a background model")
     227        my $cmf_file = $comp->{cmf} if ($options & $PSTAMP_SELECT_CMF);
     228        my $psf_file = $comp->{psf} if ($options & $PSTAMP_SELECT_PSF);
     229        my $backmdl_file = $comp->{backmdl} if ($options & $PSTAMP_SELECT_BACKMDL);
     230        my $pattern_file = $comp->{pattern} if ($options & $PSTAMP_SELECT_BACKMDL);
     231
     232        my $outdir = dirname($output_base);
     233        my $basename = basename($output_base);
     234        my ($rownum, $jobnum, $therest) = split /_/, $basename;
     235        &my_die("failed to split basename: $basename", $job_id, $PS_EXIT_CONFIG_ERROR)
     236            if (!$therest or !$rownum or !$jobnum);
     237
     238        my $prefix = "${rownum}_${jobnum}_";
     239
     240        if ($cmf_file) {
     241            print "cmf file is $cmf_file\n";
     242            copy_and_register_file($f, $cmf_file, $outdir, $prefix);
     243        }
     244        if ($psf_file) {
     245            print "psf_file is $psf_file\n";
     246            copy_and_register_file($f, $psf_file, $outdir, $prefix);
     247        }
     248        if ($backmdl_file) {
     249            print "backmdl_file is $backmdl_file\n";;
     250            copy_and_register_file($f, $backmdl_file, $outdir, $prefix);
     251        }
     252       if (0) {
     253        # don't enable this yet
     254        if ($pattern_file) {
     255            print "pattern_file is $pattern_file\n";;
     256            copy_and_register_file($f, $pattern_file, $outdir, $prefix);
     257        }
     258       }
     259    }
     260}
     261
     262sub read_params_file {
     263    my $output_base = shift;
     264
     265    my $params_file = $output_base . ".mdc";
     266    open (IN, "<$params_file")
     267        or my_die("failed to open params file: $params_file", $job_id, $PS_EXIT_UNKNOWN_ERROR);
     268
     269    my $data = $mdcParser->parse(join "", (<IN>))
     270        or my_die("failed to parse params file: $params_file", $job_id, $PS_EXIT_UNKNOWN_ERROR);
     271
     272    my $components = parse_md_list($data);
     273
     274    my $n = scalar @$components;
     275    if ($n != 1) {
     276        my_die("params file $params_file contains unexpected number of components: $n",
     277                $job_id, $PS_EXIT_PROG_ERROR);
     278    }
     279    return $components->[0];
     280}
     281
     282# copy_and_register_file ($f, $src, $destdir, $prefix);
     283sub copy_and_register_file {
     284    my $F = shift;
     285    my $src = shift;
     286    my $destdir = shift;
     287    my $prefix = shift;
     288
     289    my $fn = $prefix . basename($src);
     290    my $dst = "$destdir/$fn";
     291
     292    my $resolved = $ipprc->file_resolve($src);
     293
     294    my_die("failed to resolve $src", $job_id, $PS_EXIT_UNKNOWN_ERROR) if !$resolved;
     295
     296    if (!-e $resolved) {
     297        print STDERR "$src does not exist, skipping\n";
     298        return;
     299    }
     300
     301
     302    copy($resolved, $dst) or my_die("failed to copy $resolved to $dst", $job_id, $PS_EXIT_UNKNOWN_ERROR);
     303
     304    print $F file_registration_line($fn, $dst, "fits") . "\n";
     305}
     306
     307sub my_die
     308{
     309    my $msg = shift;            # Warning message on die
     310    my $job_id = shift;         # job identifier
     311    my $exit_code = shift;      # Exit code to add
     312
     313    $exit_code = $PS_EXIT_PROG_ERROR unless $exit_code;
     314
     315    carp($msg);
     316    if (defined $job_id and not $no_update) {
     317        my $command = "$pstamptool -updatejob";
     318        $command .= " -job_id $job_id";
     319        $command .= " -fault $exit_code";
     320        # XXX: fix pstamptool to not require -state when -fault with nonzero value is provided
     321        $command .= " -state run";
     322        $command .= " -dbname $dbname" if defined $dbname;
     323        $command .= " -dbserver $dbserver" if defined $dbserver;
     324        system($command);
     325    }
     326    exit $exit_code;
     327}
     328
Note: See TracChangeset for help on using the changeset viewer.