Changeset 24456 for gs3-extensions/solr


Ignore:
Timestamp:
2011-08-24T13:48:52+12:00 (11 years ago)
Author:
davidb
Message:

Improved error handling of solr-admin over 'wget'

File:
1 edited

Legend:

Unmodified
Added
Removed
  • gs3-extensions/solr/trunk/src/perllib/solrbuilder.pm

    r24453 r24456  
    277277    my ($url) = @_;
    278278
    279     my $cmd = "wget -q -O - \"$url\"";
    280 
    281     my $status = undef;
    282 
     279    my $cmd = "wget -O - \"$url\" 2>&1";
     280
     281    my $preamble_output = "";   
     282    my $xml_output = "";
     283    my $error_output = undef;
     284
     285    my $in_preamble = 1;
     286   
    283287    if (open(WIN,"$cmd |")) {
    284288
    285     my $xml_output = "";
    286289    my $line;
    287290    while (defined ($line=<WIN>)) {
    288291
    289         $xml_output .= $line;
     292        if ($line =~ m/ERROR \d+:/) {
     293        chomp $line;
     294        $error_output = $line;
     295        last;
     296        }
     297        elsif ($in_preamble) {
     298        if ($line =~ m/<.*>/) {
     299            $in_preamble = 0;
     300        }
     301        else {
     302            $preamble_output .= $line;
     303        }
     304        }
     305
     306        if (!$in_preamble) {
     307        $xml_output .= $line;
     308        }
    290309    }
    291310    close(WIN);
    292311
    293 ##  print $xml_output;
    294 
    295     ($status) = ($xml_output =~ m!<int name="status">(\d+)</int>!s);
    296    
    297312    }
    298313    else {
    299     print STDERR "Warning: failed to run $cmd\n";
    300     print STDERR "  $!\n";
    301     }
    302 
    303     return $status;
    304 
     314    $error_output = "Error: failed to run $cmd\n";
     315    $error_output .= "  $!\n";
     316    }
     317
     318    my $output = { 'preamble' => $preamble_output,
     319           'output'   => $xml_output,
     320           'error'    => $error_output };
     321
     322    return $output;
    305323}
    306324
     
    402420    my $collection = $self->{'collection'};
    403421
     422    # my $idx = $self->{'index_mapping'}->{$index};
     423    my $idx = "idx";
     424
    404425    foreach my $level (keys %{$self->{'levels'}}) {
    405 
     426   
     427    my ($pindex) = $level =~ /^(.)/;
     428   
    406429    my $llevel = $mgppbuilder::level_map{$level};
    407 
    408430    my $core = $collection."-".lc($llevel);
    409     my $check_core_url = "$base_url?action=STATUS&core=$core";
    410 
    411     my $check_status = $self->solr_core_admin($check_core_url);
    412    
    413     print STDERR "*** check status = $check_status\n";
    414 
    415 
     431   
     432   
    416433    # if collect==core not already in solr.xml (check with STATUS)
    417434    # => use CREATE API to add to solr.xml
     
    419436    # else
    420437    # => use RELOAD call to refresh fields now expressed in schema.xml
     438   
     439   
     440    my $check_core_url    = "$base_url?action=STATUS&core=$core";
     441    my $output = $self->solr_core_admin($check_core_url);
     442
     443    if (defined $output->{'error'}) {
     444
     445        my $preamble = $output->{'preamble'};
     446        my $error    = $output->{'error'};
     447
     448        print STDERR "----\n";
     449        print STDERR "Error: Failed to get XML response from:\n";
     450        print STDERR "         $check_core_url\n";
     451        print STDERR "Output was:\n";
     452        print STDERR $preamble if ($preamble ne "");
     453        print STDERR "$error\n";
     454        print STDERR "----\n";
     455
     456        next;
     457    }
     458   
     459    # If the collection doesn't exist yet, then there will be
     460    # an empty element of the form:
     461    #   <lst name="collect-doc"/>
     462    # where 'collect' is the actual name of the collection,
     463    # such as demo
     464
     465    my $xml_output = $output->{'output'};
     466   
     467    my $empty_element="<lst\\s+name=\"$core\"\\s*\\/>";
     468   
     469    my $check_core_exists = !($xml_output =~ m/$empty_element/s);
     470   
     471    if ($check_core_exists) {
     472       
     473        my $reload_core_url    = "$base_url?action=RELOAD&core=$core";
     474       
     475        print $outhandle "Reloading Solr core: $core\n";
     476        $self->solr_core_admin($reload_core_url);
     477    }
     478    else {
     479       
     480        my $collect_home = $ENV{'GSDLCOLLECTDIR'};
     481        my $etc_dirname = &util::filename_cat($collect_home,"etc");
     482       
     483        my $build_dir = $self->{'build_dir'};
     484        my $idx_dirname = &util::filename_cat($build_dir,$pindex.$idx);
     485       
     486        my $create_core_url    = "$base_url?action=CREATE&name=$core";
     487        $create_core_url .= "&instanceDir=$etc_dirname";
     488        $create_core_url .= "&dataDir=$idx_dirname";
     489       
     490        print $outhandle "Creating Solr core: $core\n";
     491        $self->solr_core_admin($create_core_url);
     492    }
    421493    }
    422494
Note: See TracChangeset for help on using the changeset viewer.