IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
May 18, 2010, 12:49:05 PM (16 years ago)
Author:
eugene
Message:

merging changes from trunk into branches/pap

Location:
branches/pap
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/pap

  • branches/pap/pstamp/scripts/pstamp_checkdependent.pl

    r27703 r28003  
    2121use Carp;
    2222
     23# XXX: put this in a module somewhere
     24my $IPP_DIFF_MODE_WARP_WARP   = 1;
     25my $IPP_DIFF_MODE_WARP_STACK  = 2;
     26my $IPP_DIFF_MODE_STACK_WARP  = 3;
     27my $IPP_DIFF_MODE_STACK_STACK = 4;
    2328
    2429my ($dep_id, $stage, $stage_id, $component, $imagedb, $rlabel, $need_magic);
     
    3035    'stage_id=i'    =>  \$stage_id,
    3136    'component=s'   =>  \$component,
    32     'imagedb=s'     =>  \$imagedb,      # dbname for images
     37    'imagedb=s'     =>  \$imagedb,      # dbname for images.
    3338    'rlabel=s'      =>  \$rlabel,
    3439    'need_magic'    =>  \$need_magic,
     
    4853my $warptool = can_run('warptool') or (warn "Can't find warptool" and $missing_tools = 1);
    4954my $difftool = can_run('difftool') or (warn "Can't find difftool" and $missing_tools = 1);
     55my $stacktool = can_run('stacktool') or (warn "Can't find stacktool" and $missing_tools = 1);
    5056my $magicdstool = can_run('magicdstool') or (warn "Can't find magicdstool" and $missing_tools = 1);
    5157my $pstamptool = can_run('pstamptool') or (warn "Can't find pstamptool" and $missing_tools = 1);
     
    5561}
    5662
     63# Append imagedb to the ippTools
    5764$chiptool    .= " -dbname $imagedb";
    5865$warptool    .= " -dbname $imagedb";
    5966$difftool    .= " -dbname $imagedb";
     67$stacktool   .= " -dbname $imagedb";
    6068$magicdstool .= " -dbname $imagedb";
    61 
    6269
    6370my $ipprc = PS::IPP::Config->new();
     
    6673    $dbserver =  metadataLookupStr($ipprc->{_siteConfig}, 'PS_DBSERVER');
    6774}
    68 
    6975
    7076
     
    8692} else {
    8793    if ($stage eq "chip") {
    88         $cmd = "$chiptool -processedimfile -chip_id $stage_id -class_id $component";
     94        $cmd = "$chiptool -processedimfile -allfiles -chip_id $stage_id -class_id $component";
    8995    } elsif ($stage eq "warp") {
    9096        $cmd = "$warptool -warped -warp_id $stage_id -skycell_id $component";
     
    96102}
    97103
    98 my $components = runToolAndParse($cmd, $verbose);
    99 
    100 my $n = scalar @$components;
    101 my_die("Unexpected number of components found $n", $PS_EXIT_PROG_ERROR) if ($n != 1);
     104my $it = runToolAndParseExpectOne($cmd, $verbose);
     105
     106my_die("no components found", $PS_EXIT_PROG_ERROR) if ( !$it);
    102107
    103108# Got "it"
    104 my $it = $components->[0];
    105 
     109
     110my $magic_ok = 0;
     111if ($stage eq 'diff') {
     112    if ($it->{diff_mode} == $IPP_DIFF_MODE_STACK_STACK) {
     113        # stack stack diffs don't need magic, but since the warps need to have the chips destreaked
     114        # in order to be processed se set need_magic in the database for all runs
     115        # Now the diffs themselves don't need to be destreaked so
     116        $magic_ok = 1;
     117    }
     118}
    106119my $status = 0;
    107120if (($it->{state} eq 'full') or ($it->{state} eq 'update') and ($whole_run or ($it->{data_state} eq 'full'))
    108         and (!$need_magic or $it->{magicked} > 0)) {
    109 
    110     # This Dependency is satisfied. all done
     121        and (!$need_magic or $magic_ok or $it->{magicked} > 0)) {
     122
     123    # This Dependency is satisfied. All done!
    111124    my $command = "$pstamptool -updatedependent -set_state full -dep_id $dep_id";
    112125    $command .= " -dbname $dbname" if $dbname;
     
    125138        ($whole_run or ($it->{data_state} eq 'cleaned'))) {
    126139
     140    # Update has not been queued for this component yet. 
     141
    127142    # XXX: if $whole_run this is going to repeatedly call -setimfiletoupdate. This will be a no-op
    128143    # but is wasteful.
     
    130145    # need to queue the updates
    131146    if ($stage eq 'chip') {
    132         # queue_update_chip takes an array so that queue_update_warp can pass a set of
    133         # chips to it
     147        # queue_update_chip takes an array so that queue_update_warp can pass it set of chips
    134148        my $chips = [$it];
    135149        $status = queue_update_chip($it->{chip_id}, $whole_run, $chips, $rlabel, $need_magic);
     
    142156    }
    143157} else {
     158    # update for this component has been queued by hasn't completed yet or the state is an error state
     159    # XXX: in this state we aren't checking the dependents anymore. If something goes wrong this
     160    # will never complete. Consider calling the 'queue_update' functions again just to check. They are relatively cheap
     161
     162
    144163    print "${stage}Run $stage_id state is $it->{state} $component data_state is $it->{data_state}\n";
    145164
    146165    # detect states that cannot be updated and update the job state
    147     # XXX: we should be more assertive here and check for the specific states where we can continue
     166    # XXX: Perhaps I should be more assertive here and check for the specific states that we know
     167    # that we can continue.
     168
    148169    my $state = $it->{state};
    149170    my $job_fault = 0;
    150     if (($state =~ /scrub/) or ($state =~ /purge/)) {
     171
     172    # temporary hack to deal with data with old burntool tables. This check is now done by pstampparse.pl
     173    # so runs in this state should not get queued
     174    if ($stage eq 'chip') {
     175        my $burntool_state = $it->{burntool_state};
     176        if ($burntool_state and (abs($burntool_state) < 14)) {
     177            print STDERR "chip $it->{chip_id} $it->{class_id} has burntool_state $burntool_state. Not avaiable.\n";
     178            $job_fault = $PSTAMP_NOT_AVAILABLE;
     179        }
     180    }
     181    if ($state eq 'error_cleaned') {
     182        $job_fault = $PSTAMP_NOT_AVAILABLE;
     183    } elsif (($state =~ /scrub/) or ($state =~ /purge/)) {
    151184        # jobs must have changed state since depenency was made
    152185        print STDERR "Dependency cannot be satisfied\n";
     
    156189        my_die ("Unexpected state for ${stage}Run $stage_id $state", $PS_EXIT_PROG_ERROR);
    157190    }
     191    if (!$job_fault and ($stage eq 'chip')) {
     192        # chip processing is done, start destreaking.
     193        $job_fault = queue_update_magicDSRun($stage, $stage_id, $rlabel, $need_magic, $it->{raw_magicked}, $it->{dsRun_state});
     194    }
    158195    if ($job_fault) {
    159196        faultJobs($state, $stage, $stage_id, $job_fault);
     
    171208    my $need_magic = shift;
    172209
     210
    173211    my $dsRun_state;
    174212    my $raw_all_magicked = 1; # this gets cleared if any of the inputs aren't destreaked
     
    178216            $raw_all_magicked &= ($chip->{raw_magicked} > 0);
    179217
    180             if ($chip->{data_state} ne 'update') {
     218            if ($chip->{state} =~ /error/) {
     219                faultJobs('stop', undef, undef, $PSTAMP_GONE);
     220            } elsif (($chip->{data_state} ne 'update') and ($chip->{data_state} ne 'full')) {
    181221                my $command = "$chiptool -setimfiletoupdate -chip_id $chip_id -class_id $chip->{class_id}";
    182222                $command .= " -set_label $rlabel" if $rlabel;
     
    213253    }
    214254
     255    my $status = queue_update_magicDSRun('chip', $chip_id, $rlabel, $need_magic, $raw_all_magicked, $dsRun_state);
     256
     257    return $status;
     258}
     259
     260sub queue_update_warp {
     261    # check status of input chips. If state is not updatable set error code for job
     262
     263    # if chipProcessedImfile.state is cleaned call queue_update_chip
     264
     265    # need to code warptool -setskyfiletoupdate
     266    my $metadata = shift;
     267    my $whole_run = shift;  # if true queue entire run for update
     268    my $rlabel = shift;     # if defined a new label for the chipRun
     269    my $need_magic = shift;
     270
     271    my $raw_all_magicked = 1; # this gets cleared if any of the inputs aren't destreaked
     272
     273    my $warp_id = $metadata->{warp_id};
     274    my $state = $metadata->{state};
     275    if ($state =~ /error/) {
     276        print STDERR "warpRun $warp_id has state $state faulting jobs\n";
     277        faultJobs('stop', undef, undef, $PSTAMP_GONE);
     278        exit 0;
     279    }
     280
     281    if (!$whole_run) {
     282        my $skycell = $metadata;
     283        my $skycell_id = $skycell->{skycell_id};
     284
     285        my $command = "$warptool -scmap -warp_id $warp_id -skycell_id $skycell_id";
     286        my $data = runToolAndParse($command, $verbose);
     287        my_die("failed to find warpSkyCelllMap for warpRun $warp_id skycell_id $skycell_id", $PS_EXIT_UNKNOWN_ERROR)
     288            if !$data or scalar @$data == 0;
     289
     290        my $chips_ready = 1;
     291        my @chipsToUpdate;
     292        my $chip_id;
     293        foreach my $chip (@$data) {
     294            $chip_id = $chip->{chip_id};
     295            if (($chip->{data_state} ne 'full') or ($need_magic and ($chip->{magicked} <= 0))) {
     296                $chips_ready = 0;
     297                push @chipsToUpdate, $chip;
     298            } else {
     299                # this chip is good to go
     300            }
     301        }
     302
     303        if ($chips_ready) {
     304            # the reason we defer setting the warp to update is so that we can handle error conditions at previous
     305            # stages more easily.
     306            my $command = "$warptool -setskyfiletoupdate -warp_id $warp_id -skycell_id $skycell->{skycell_id}";
     307            $command .= " -set_label $rlabel" if $rlabel;
     308
     309            if (!$no_update) {
     310                my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
     311                            run(command => $command, verbose => $verbose);
     312                unless ($success) {
     313                    my_die("failed to queue ${stage}Run $stage_id $component for update", $PS_EXIT_UNKNOWN_ERROR);
     314                }
     315            } else {
     316                print "skipping $command\n";
     317            }
     318        } elsif (scalar @chipsToUpdate > 0) {
     319            return queue_update_chip($chip_id, 0, \@chipsToUpdate, $rlabel, $need_magic);
     320        }
     321    } else {
     322        my $warpRun = $metadata;
     323        my $command = "$chiptool -listrun -chip_id $warpRun->{chip_id}";
     324        my $data = runToolAndParse($command, $verbose);
     325        my_die("failed to find chipRun $warpRun->{chip_id} for warpRun $warp_id", $PS_EXIT_UNKNOWN_ERROR)
     326            if !$data or scalar @$data != 1;
     327
     328        my $chipRun = $data->[0];
     329
     330        my $chipRunState = $chipRun->{state};
     331        if (($chipRunState =~ /purge/) or ($chipRunState =~ /scrub/)) {
     332            print STDERR "warpRun $warp_id depends on chipRun $chipRun->{chip_id} which is in state $chipRunState\n";
     333            faultJobs('stop', 'warp', $warp_id, $PSTAMP_GONE);
     334            return 0;
     335        }
     336        my $warpRunState = $warpRun->{state};
     337        if (($chipRunState eq 'full') and (! $need_magic or ($chipRun->{magicked} > 0)) and ($warpRunState eq 'cleaned')) {
     338            # The inputs and outputs are ready. Queue the warpRun for update.
     339
     340            # providing no -skycell_id arguments changes all skyfiles with data_state = 'cleaned' to 'update'
     341            my $command = "$warptool -setskyfiletoupdate -warp_id $warp_id";
     342            $command .= " -set_label $rlabel" if $rlabel;
     343
     344            if (!$no_update) {
     345                my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
     346                            run(command => $command, verbose => $verbose);
     347                unless ($success) {
     348                    my_die("failed to queue ${stage}Run $stage_id $component for update", $PS_EXIT_UNKNOWN_ERROR);
     349                }
     350            } else {
     351                print "skipping $command\n";
     352            }
     353        } elsif ($chipRunState eq 'cleaned' or
     354            (($chipRun->{state} eq 'full') and ($need_magic and ($chipRun->{magicked} < 0) and ($chipRun->{dsRun_state} ne 'new')))) {
     355            my $data = [$chipRun];
     356            return queue_update_chip($chipRun->{chip_id}, 1, $data, $rlabel, $need_magic);
     357        }
     358    }
     359
     360    # return value may be the return status of script so zero is good
     361    return 0;
     362}
     363sub queue_update_diff {
     364    my $metadata = shift;
     365    my $whole_run = shift;  # if true queue entire run for update
     366    my $rlabel = shift;     # if defined a new label for the chipRun
     367    my $need_magic = shift;
     368
     369    my $diff_id   = $metadata->{diff_id};
     370    my $diff_mode = $metadata->{diff_mode};
     371    if (!$whole_run) {
     372        my $skycell = $metadata;
     373        my $skycell_id = $skycell->{skycell_id};
     374
     375        if ($diff_mode == $IPP_DIFF_MODE_WARP_STACK ) {
     376            # check the state of the template stack
     377            my $command = "$stacktool -sumskyfile -stack_id $skycell->{stack2}";
     378            my $stack = runToolAndParseExpectOne($command, $verbose);
     379            my_die("failed to find stackSumSkyfile for stack_id $skycell->{stack2}", $PS_EXIT_UNKNOWN_ERROR) if !$stack;
     380
     381            if ($stack->{state} ne 'full') {
     382                print STDERR "template stack for diffRun $diff_id $skycell_id is not in full state faulting jobs\n";
     383                # this faults all jobs depending on this dep_id
     384                faultJobs('stop', 'diff', $diff_id, $PSTAMP_GONE);
     385                return $PSTAMP_GONE;
     386            }
     387
     388            # now check the warp
     389            $command = "$warptool -warped -warp_id $skycell->{warp1} -skycell_id $skycell_id";
     390            my $warp = runToolAndParseExpectOne($command, $verbose);
     391            my_die("failed to find warpSkyfile for warpRun $skycell->{warp1} skycell_id $skycell_id", $PS_EXIT_UNKNOWN_ERROR) if !$warp;
     392
     393            if ($warp->{data_state} ne 'full') {
     394                return queue_update_warp($warp, 0, $rlabel, $need_magic);
     395            }
     396            # warps are ready fall through and queue the diff update
     397        } elsif ($diff_mode eq $IPP_DIFF_MODE_WARP_WARP) {
     398            my $command = "$warptool -warped -warp_id $skycell->{warp1} -skycell_id $skycell_id";
     399            my $warp1 = runToolAndParseExpectOne($command, $verbose);
     400            my_die("failed to find warpSkyfile for warpRun $skycell->{warp1} skycell_id $skycell_id", $PS_EXIT_UNKNOWN_ERROR) if !$warp1;
     401
     402            my $warps_ready = 1;
     403            my $status = 0;
     404            if ($warp1->{data_state} ne 'full') {
     405                $warps_ready = 0;
     406                $status = queue_update_warp($warp1, 0, $rlabel, $need_magic);
     407                if ($status) {
     408                    return $status;
     409                }
     410            }
     411            $command = "$warptool -warped -warp_id $skycell->{warp2} -skycell_id $skycell_id";
     412            my $warp2 = runToolAndParseExpectOne($command, $verbose);
     413            my_die("failed to find warpSkyfile for warpRun $skycell->{warp2} skycell_id $skycell_id", $PS_EXIT_UNKNOWN_ERROR) if !$warp2;
     414
     415            if ($warp2->{data_state} ne 'full') {
     416                $warps_ready = 0;
     417                $status = queue_update_warp($warp2, 0, $rlabel, $need_magic);
     418            }
     419
     420            if (!$warps_ready) {
     421                # don't queue the diff update yet
     422                return $status;
     423            }
     424
     425        } elsif ($diff_mode == $IPP_DIFF_MODE_STACK_STACK ) {
     426            # check the state of the input stack
     427            my $command = "$stacktool -sumskyfile -stack_id $skycell->{stack2}";
     428            my $stack1 = runToolAndParseExpectOne($command, $verbose);
     429            my_die("failed to find stackSumSkyfile for stack_id $skycell->{stack2}", $PS_EXIT_UNKNOWN_ERROR) if !$stack1;
     430
     431            if ($stack1->{state} ne 'full') {
     432                print STDERR "input stack for diffRun $diff_id $skycell_id is not in full state faulting jobs\n";
     433                # this faults all jobs depending on this dep_id
     434                faultJobs('stop', 'diff', $diff_id, $PSTAMP_GONE);
     435                return $PSTAMP_GONE;
     436            }
     437            # check the state of the template stack
     438            $command = "$stacktool -sumskyfile -stack_id $skycell->{stack2}";
     439            my $stack2 = runToolAndParseExpectOne($command, $verbose);
     440            my_die("failed to find stackSumSkyfile for stack_id $skycell->{stack2}", $PS_EXIT_UNKNOWN_ERROR) if !$stack2;
     441
     442            if ($stack2->{state} ne 'full') {
     443                print STDERR "template stack for diffRun $diff_id $skycell_id is not in full state faulting jobs\n";
     444                # this faults all jobs depending on this dep_id
     445                faultJobs('stop', 'diff', $diff_id, $PSTAMP_GONE);
     446                return $PSTAMP_GONE;
     447            }
     448
     449            # inputs are ready fall through and queue the diff update
     450        } elsif ($diff_mode == $IPP_DIFF_MODE_STACK_WARP ) {
     451            # check the state of the input stack
     452            my $command = "$stacktool -sumskyfile -stack_id $skycell->{stack1}";
     453            my $stack = runToolAndParseExpectOne($command, $verbose);
     454            my_die("failed to find stackSumSkyfile for stack_id $skycell->{stack1}", $PS_EXIT_UNKNOWN_ERROR) if !$stack;
     455
     456            if ($stack->{state} ne 'full') {
     457                print STDERR "input stack for diffRun $diff_id $skycell_id is not in full state faulting jobs\n";
     458                # this faults all jobs depending on this dep_id
     459                faultJobs('stop', 'diff', $diff_id, $PSTAMP_GONE);
     460                return $PSTAMP_GONE;
     461            }
     462
     463            # now check the template warp
     464            $command = "$warptool -warped -warp_id $skycell->{warp2} -skycell_id $skycell_id";
     465            my $warp = runToolAndParseExpectOne($command, $verbose);
     466            my_die("failed to find warpSkyfile for warpRun $skycell->{warp2} skycell_id $skycell_id", $PS_EXIT_UNKNOWN_ERROR) if !$warp;
     467
     468            if ($warp->{data_state} ne 'full') {
     469                return queue_update_warp($warp, 0, $rlabel, $need_magic);
     470            }
     471            # warps are ready fall through and queue the diff update
     472        } else {
     473            my_die("unexpected diff_mode found: $diff_mode", $PS_EXIT_PROG_ERROR);
     474        }
     475
     476        my $command = "$difftool -setskyfiletoupdate -diff_id $diff_id -skycell_id $skycell_id";
     477        $command .= " -set_label $rlabel" if $rlabel;
     478
     479        if (!$no_update) {
     480            my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
     481                        run(command => $command, verbose => $verbose);
     482            unless ($success) {
     483                my_die("failed to queue ${stage}Run $stage_id $component for update", $PS_EXIT_UNKNOWN_ERROR);
     484            }
     485        } else {
     486            print "skipping $command\n";
     487        }
     488    } else {
     489        # XXX todo whole_run
     490        return $PS_EXIT_PROG_ERROR;
     491    }
     492
     493    # return value is a unix style exit status so zero is good
     494    return 0;
     495}
     496
     497
     498# run a command that produces metadata output and parse the results into an array of objects
     499sub runToolAndParse {
     500    my $command = shift;
     501    my $verbose = shift;
     502
     503    my ($program) = split " ", $command;
     504    $program = basename($program);
     505
     506    print "Running $command\n" if !$verbose;
     507    my $start_tool = DateTime->now->mjd;
     508    # run the command and parse the output
     509    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
     510                run(command => $command, verbose => $verbose);
     511    unless ($success) {
     512        print STDERR @$stderr_buf if !$verbose;
     513        return undef;
     514    }
     515
     516    my $now = DateTime->now->mjd;
     517    my $dtime_tool = (DateTime->now->mjd - $start_tool) * 86400.;
     518    print "Time to run $program: $dtime_tool\n";
     519
     520    my $buf = join "", @$stdout_buf;
     521    if (!$buf) {
     522        return undef;
     523    }
     524
     525    my $start_parse = DateTime->now->mjd;
     526
     527    my $mdcParser = PS::IPP::Metadata::Config->new;
     528    my $results = parse_md_fast($mdcParser, $buf)
     529        or my_die ("Unable to parse metadata config doc", $PS_EXIT_UNKNOWN_ERROR);
     530
     531    my $dtime_parse = (DateTime->now->mjd - $start_parse) * 86400.;
     532    print "Time to parse results from $program: $dtime_parse\n";
     533
     534    return $results;
     535}
     536
     537# run an command returning metadata where we expect 1 entry
     538sub runToolAndParseExpectOne {
     539    my $command = shift;
     540    my $verbose = shift;
     541
     542    my $data = runToolAndParse($command, $verbose);
     543
     544    if (!$data) {
     545        return undef;
     546    }
     547
     548    my $n = scalar @$data;
     549    if ($n > 1) {
     550        my_die("Unexpected number of components $n returned by $command", $PS_EXIT_PROG_ERROR);
     551    }
     552
     553    return $data->[0];
     554}
     555
     556# splits meta data config input stream into single units to work around the pathalogically
     557# slow parser. This is similar to and adapted from code in various ippScripts.
     558sub parse_md_fast {
     559    my $mdcParser = shift;
     560    my $input = shift;
     561    my $output = ();
     562
     563    my @whole = split /\n/, $input;
     564    my @single = ();
     565
     566    my $n;
     567    while ( ($n = @whole) > 0) {
     568        my $value = shift @whole;
     569        push @single, $value;
     570        if ($value =~ /^\s*END\s*$/) {
     571            push @single, "\n";
     572
     573            my $list = parse_md_list( $mdcParser->parse( join("\n", @single ) ) ) or
     574                print STDERR "Unable to parse metdata config doc" and return undef;
     575            push @$output, $list->[0];
     576
     577            @single = ();
     578        }
     579    }
     580    return $output;
     581}
     582
     583sub faultJobs {
     584    my ($state, $stage, $stage_id, $job_fault) = @_;
     585
     586    my $command = "$pstamptool -updatejob -set_state stop -set_fault $job_fault -dep_id $dep_id";
     587    $command .= " -dbname $dbname" if $dbname;
     588    $command .= " -dbserver $dbserver" if $dbserver;
     589    if (!$no_update) {
     590        my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
     591                    run(command => $command, verbose => $verbose);
     592        unless ($success) {
     593            my_die("failed to set pstampJob.fault for dep_id: $dep_id",
     594                $PS_EXIT_UNKNOWN_ERROR);
     595        }
     596    } else {
     597        print "skipping $command\n";
     598    }
     599}
     600
     601sub queue_update_magicDSRun {
     602    my $stage = shift;
     603    my $stage_id = shift;
     604    my $rlabel  = shift;
     605    my $need_magic = shift;
     606    my $input_magicked = shift;
     607    my $dsRun_state = shift;
     608
     609    # XXX: this code assumes that destreaking is handled at the chip stage
     610    my_die ("queue_update_magicDSRun only works for stage chip", $PS_EXIT_PROG_ERROR) if $stage ne 'chip';
     611
    215612    # if called from queue_update_warp dsRun_state is unknown. Go find it.
    216613    if (!$dsRun_state) {
    217         my $command = "$chiptool -listrun -chip_id $chip_id";
     614        my $command = "$chiptool -listrun -chip_id $stage_id";
    218615        my $data = runToolAndParse($command, $verbose);
    219616        my $chipRun = $data->[0];
     
    222619
    223620    # if the input file is already magicked no need to queue destreaking for this chipRun
    224     if ($need_magic and !$raw_all_magicked) {
     621    if ($need_magic and !$input_magicked) {
    225622        if ($dsRun_state eq 'cleaned') {
    226623            my $command = "$magicdstool -updaterun -set_state new -stage $stage -stage_id $stage_id";
     
    236633                print "skipping $command\n";
    237634            }
     635        } elsif ($dsRun_state eq 'failed_revert') {
     636            print "magicDSRun.state = $dsRun_state for chipRun $stage_id is in state failed_revert cannot update";
     637            faultJobs('stop', undef, undef, $PSTAMP_NOT_AVAILABLE);
    238638        } else {
    239639            print "magicDSRun.state = $dsRun_state for chipRun $stage_id";
     
    242642        }
    243643    }
    244 
    245     # return value may be the return status of script so zero is good
    246644    return 0;
    247645}
    248646
    249 sub queue_update_warp {
    250     # check status of input chips. If state is not updatable set error code for job
    251 
    252     # if chipProcessedImfile.state is cleaned call queue_update_chip
    253 
    254     # need to code warptool -setskyfiletoupdate
    255     my $metadata = shift;
    256     my $whole_run = shift;  # if true queue entire run for update
    257     my $rlabel = shift;     # if defined a new label for the chipRun
    258     my $need_magic = shift;
    259 
    260     my $raw_all_magicked = 1; # this gets cleared if any of the inputs aren't destreaked
    261 
    262     my $warp_id = $metadata->{warp_id};
    263     if (!$whole_run) {
    264         my $skycell = $metadata;
    265         my $skycell_id = $skycell->{skycell_id};
    266 
    267         my $command = "$warptool -scmap -warp_id $warp_id -skycell_id $skycell_id";
    268         my $data = runToolAndParse($command, $verbose);
    269         my_die("failed to find warpSkyCelllMap for warpRun $warp_id skycell_id $skycell_id", $PS_EXIT_UNKNOWN_ERROR)
    270             if !$data or scalar @$data == 0;
    271 
    272         my $good_to_go = 1;
    273         my @chipsToUpdate;
    274         my $chip_id;
    275         foreach my $chip (@$data) {
    276             $chip_id = $chip->{chip_id};
    277             if (($chip->{data_state} ne 'full') or ($need_magic and ($chip->{magicked} <= 0))) {
    278                 $good_to_go = 0;
    279                 push @chipsToUpdate, $chip;
    280             } else {
    281                 # this chip is good to go
    282             }
    283         }
    284 
    285         if ($good_to_go) {
    286             my $command = "$warptool -setskyfiletoupdate -warp_id $warp_id -skycell_id $skycell->{skycell_id}";
    287             $command .= " -set_label $rlabel" if $rlabel;
    288 
    289             if (!$no_update) {
    290                 my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
    291                             run(command => $command, verbose => $verbose);
    292                 unless ($success) {
    293                     my_die("failed to queue ${stage}Run $stage_id $component for update", $PS_EXIT_UNKNOWN_ERROR);
    294                 }
    295             } else {
    296                 print "skipping $command\n";
    297             }
    298         } elsif (scalar @chipsToUpdate > 0) {
    299             return queue_update_chip($chip_id, 0, \@chipsToUpdate, $rlabel, $need_magic);
    300         }
    301     } else {
    302         my $warpRun = $metadata;
    303         my $command = "$chiptool -listrun -chip_id $warpRun->{chip_id}";
    304         my $data = runToolAndParse($command, $verbose);
    305         my_die("failed to find chipRun $warpRun->{chip_id} for warpRun $warp_id", $PS_EXIT_UNKNOWN_ERROR)
    306             if !$data or scalar @$data != 1;
    307 
    308         my $chipRun = $data->[0];
    309 
    310         my $chipRunState = $chipRun->{state};
    311         if (($chipRunState =~ /purge/) or ($chipRunState =~ /scrub/)) {
    312             print STDERR "warpRun $warp_id depends on chipRun $chipRun->{chip_id} which is in state $chipRunState\n";
    313             faultJobs('stop', 'warp', $warp_id, $PSTAMP_GONE);
    314             return 0;
    315         }
    316         my $warpRunState = $warpRun->{state};
    317         if (($chipRunState eq 'full') and (! $need_magic or ($chipRun->{magicked} > 0)) and ($warpRunState eq 'cleaned')) {
    318             # The inputs and outputs are ready. Queue the warpRun for update.
    319 
    320             # providing no -skycell_id arguments changes all skyfiles with data_state = 'cleaned' to 'update'
    321             my $command = "$warptool -setskyfiletoupdate -warp_id $warp_id";
    322             $command .= " -set_label $rlabel" if $rlabel;
    323 
    324             if (!$no_update) {
    325                 my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
    326                             run(command => $command, verbose => $verbose);
    327                 unless ($success) {
    328                     my_die("failed to queue ${stage}Run $stage_id $component for update", $PS_EXIT_UNKNOWN_ERROR);
    329                 }
    330             } else {
    331                 print "skipping $command\n";
    332             }
    333         } elsif ($chipRunState eq 'cleaned' or
    334             (($chipRun->{state} eq 'full') and ($need_magic and ($chipRun->{magicked} < 0) and ($chipRun->{dsRun_state} ne 'new')))) {
    335             my $data = [$chipRun];
    336             return queue_update_chip($chipRun->{chip_id}, 1, $data, $rlabel, $need_magic);
    337         }
    338     }
    339 
    340     # return value may be the return status of script so zero is good
    341     return 0;
    342 }
    343 sub queue_update_diff {
    344     return 0;
    345 }
    346 
    347 
    348 # run a command that produces metadata output and parse the results into an array of objects
    349 sub runToolAndParse {
    350     my $command = shift;
    351     my $verbose = shift;
    352 
    353     my ($program) = split " ", $command;
    354     $program = basename($program);
    355 
    356     print "Running $command\n" if !$verbose;
    357     my $start_tool = DateTime->now->mjd;
    358     # run the command and parse the output
    359     my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
    360                 run(command => $command, verbose => $verbose);
    361     unless ($success) {
    362         print STDERR @$stderr_buf if !$verbose;
    363         return undef;
    364     }
    365 
    366     my $now = DateTime->now->mjd;
    367     my $dtime_tool = (DateTime->now->mjd - $start_tool) * 86400.;
    368     print "Time to run $program: $dtime_tool\n";
    369 
    370     my $buf = join "", @$stdout_buf;
    371     if (!$buf) {
    372         return undef;
    373     }
    374 
    375     my $start_parse = DateTime->now->mjd;
    376 
    377     my $mdcParser = PS::IPP::Metadata::Config->new;
    378     my $results = parse_md_fast($mdcParser, $buf)
    379         or my_die ("Unable to parse metadata config doc", $PS_EXIT_UNKNOWN_ERROR);
    380 
    381     my $dtime_parse = (DateTime->now->mjd - $start_parse) * 86400.;
    382     print "Time to parse results from $program: $dtime_parse\n";
    383 
    384     return $results;
    385 }
    386 
    387 # splits meta data config input stream into single units to work around the pathalogically
    388 # slow parser. This is similar to and adapted from code in various ippScripts.
    389 sub parse_md_fast {
    390     my $mdcParser = shift;
    391     my $input = shift;
    392     my $output = ();
    393 
    394     my @whole = split /\n/, $input;
    395     my @single = ();
    396 
    397     my $n;
    398     while ( ($n = @whole) > 0) {
    399         my $value = shift @whole;
    400         push @single, $value;
    401         if ($value =~ /^\s*END\s*$/) {
    402             push @single, "\n";
    403 
    404             my $list = parse_md_list( $mdcParser->parse( join("\n", @single ) ) ) or
    405                 print STDERR "Unable to parse metdata config doc" and return undef;
    406             push @$output, $list->[0];
    407 
    408             @single = ();
    409         }
    410     }
    411     return $output;
    412 }
    413 sub faultJobs {
    414     my ($state, $stage, $stage_id, $job_fault) = @_;
    415 
    416     my $command = "$pstamptool -updatejob -state stop -fault $job_fault -dep_id $dep_id";
     647sub my_die
     648{
     649    my $msg = shift;
     650    my $fault = shift;
     651    carp $msg;
     652
     653    my $command = "$pstamptool -updatedependent -set_fault $fault -dep_id $dep_id";
    417654    $command .= " -dbname $dbname" if $dbname;
    418655    $command .= " -dbserver $dbserver" if $dbserver;
     
    421658                    run(command => $command, verbose => $verbose);
    422659        unless ($success) {
    423             my_die("failed to set pstamJob.fault for dep_id: $dep_id",
    424                 $PS_EXIT_UNKNOWN_ERROR);
     660            carp "$cmd failed";
    425661        }
    426662    } else {
    427663        print "skipping $command\n";
    428664    }
    429 }
    430 
    431 sub my_die
    432 {
    433     my $msg = shift;
    434     my $fault = shift;
    435 
    436     carp $msg;
    437 
    438     return $fault;
    439 }
     665
     666    exit $fault;
     667}
Note: See TracChangeset for help on using the changeset viewer.