########################################################################### # # solrserver.pm -- class for starting and stopping the Solr/jetty server # A component of the Greenstone digital library software # from the New Zealand Digital Library Project at the # University of Waikato, New Zealand. # # Copyright (C) 1999 New Zealand Digital Library Project # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. # ########################################################################### package solrserver; use strict; #no strict 'refs'; use solrutil; sub new { my $class = shift(@_); my $self = { 'jetty_stop_key' => "greenstone-solr" }; my $search_path = &solrutil::get_search_path(); my $server_jar = &util::filename_cat("lib","java","solr-jetty-server.jar"); my $full_server_jar = solrutil::locate_file($search_path,$server_jar); $self->{'full_server_jar'} = $full_server_jar; $self->{'jetty_explicitly_started'} = undef; my $jetty_server_port = $ENV{'SOLR_JETTY_PORT'}; my $base_url = "http://localhost:$jetty_server_port/solr/"; my $admin_url = "http://localhost:$jetty_server_port/solr/admin/cores"; $self->{'base-url'} = $base_url; $self->{'admin-url'} = $admin_url; return bless $self, $class; } sub _wget_service { my $self = shift (@_); my ($output_format,$url,$cgi_get_args) = @_; my $full_url = $url; $url .= "?$cgi_get_args" if (defined $cgi_get_args); my $cmd = "wget -O - \"$url\" 2>&1"; my $preamble_output = ""; my $xml_output = ""; my $error_output = undef; my $in_preamble = ($output_format eq "xml") ? 1 : 0; if (open(WIN,"$cmd |")) { my $line; while (defined ($line=)) { if ($line =~ m/ERROR \d+:/) { chomp $line; $error_output = $line; last; } elsif ($line =~ m/failed: Connection refused/) { chomp $line; $error_output = $line; last; } elsif ($in_preamble) { if ($line =~ m/<.*>/) { $in_preamble = 0; } else { $preamble_output .= $line; } } if (! $in_preamble) { $xml_output .= $line; } } close(WIN); } else { $error_output = "Error: failed to run $cmd\n"; $error_output .= " $!\n"; } my $output = { 'preamble' => $preamble_output, 'output' => $xml_output, 'error' => $error_output }; return $output; } sub _base_service { my $self = shift (@_); my ($cgi_get_args) = @_; my $base_url = $self->{'base-url'}; return $self->_wget_service("html",$base_url,$cgi_get_args); } sub _admin_service { my $self = shift (@_); my ($cgi_get_args) = @_; my $admin_url = $self->{'admin-url'}; return $self->_wget_service("xml",$admin_url,$cgi_get_args); } sub server_running { my $self = shift @_; my $output = $self->_base_service(); my $have_error = defined $output->{'error'}; my $running = !$have_error; return $running; } sub admin_ping_core { my $self = shift @_; my ($core) = @_; my $cgi_get_args = "action=STATUS&core=$core"; my $ping_status = 1; my $output = $self->_admin_service($cgi_get_args); if (defined $output->{'error'}) { # severe error, such as failing to connect to the server $ping_status = 0; my $url = $output->{'url'}; my $preamble = $output->{'preamble'}; my $error = $output->{'error'}; print STDERR "----\n"; print STDERR "Error: Failed to get XML response from:\n"; print STDERR " $url\n"; print STDERR "Output was:\n"; print STDERR $preamble if ($preamble ne ""); print STDERR "$error\n"; print STDERR "----\n"; } else { # If the collection doesn't exist yet, then there will be # an empty element of the form: # # where 'collect' is the actual name of the collection, # such as demo my $xml_output = $output->{'output'}; my $empty_element=""; $ping_status = !($xml_output =~ m/$empty_element/s); } return $ping_status; } sub admin_reload_core { my $self = shift @_; my ($core) = @_; my $cgi_get_args = "action=RELOAD&core=$core"; $self->_admin_service($cgi_get_args); } sub admin_create_core { my $self = shift @_; my ($core) = @_; my ($ds_idx) = ($core =~ m/^.*-(.*)$/); my $cgi_get_args = "action=CREATE&name=$core"; my $collect_home = $ENV{'GSDLCOLLECTDIR'}; my $etc_dirname = &util::filename_cat($collect_home,"etc"); my $build_dir = $self->{'build_dir'}; my $idx_dirname = &util::filename_cat($build_dir,$ds_idx); $cgi_get_args .= "&instanceDir=$etc_dirname"; $cgi_get_args .= "&dataDir=$idx_dirname"; $self->_admin_service($cgi_get_args); } sub start { my $self = shift @_; my $solr_home = $ENV{'GEXT_SOLR'}; my $jetty_stop_port = $ENV{'JETTY_STOP_PORT'}; my $jetty_server_port = $ENV{'SOLR_JETTY_PORT'}; chdir($solr_home); my $solr_etc = &util::filename_cat($solr_home,"etc"); my $server_props = "-DSTOP.PORT=$jetty_stop_port"; $server_props .= " -DSTOP.KEY=".$self->{'jetty_stop_key'}; $server_props .= " -Dsolr.solr.home=$solr_etc"; my $full_server_jar = $self->{'full_server_jar'}; my $server_java_cmd = "java $server_props -jar \"$full_server_jar\""; ## print STDERR "**** server cmd start = $server_java_cmd\n"; my $server_status = "unknown"; if ($self->server_running()) { $server_status = "already-running"; } elsif (open(STARTIN,"$server_java_cmd 2>&1 |")) { my $line; while (defined($line=)) { # Scan through output until you see a line like: # 2011-08-22 .. :INFO::Started SocketConnector@0.0.0.0:8983 # which signifies that the server has started up and is # "ready and listening" if (($line =~ m/^(WARN|ERROR|SEVERE):/) || ($line =~ m/^[0-9 :-]*(WARN|ERROR|SEVERE)::/)) { print "Jetty startup: $line"; } if ($line =~ m/WARN::failed SocketConnector/) { if ($line =~ m/Address already in use/) { $server_status = "already-running"; } else { $server_status = "failed-to-start"; } last; } if ($line =~ m/INFO::Started SocketConnector/) { $server_status = "explicitly-started"; last; } } } else { print STDERR "Error: failed to start solr-jetty-server\n"; print STDERR "$!\n"; print STDERR "Command attempted was:\n"; print STDERR " $server_java_cmd\n"; print STDERR "run from directory:\n"; print STDERR " $solr_home\n"; print STDERR "----\n"; exit -1; } if ($server_status eq "explicitly-started") { $self->{'jetty_explicitly_started'} = 1; print "Jetty server ready and listening for connections on port"; print " $jetty_server_port\n"; # now we know the server is ready to accept connections, fork a # child process that continues to listen to the output and # prints out any lines that are not INFO lines if (fork()==0) { # child process my $line; while (defined ($line = )) { # if (($line =~ m/^(WARN|ERROR|SEVERE):/) # || ($line =~ m/^[0-9 :-]*(WARN|ERROR|SEVERE)::/)) { # print "Jetty $line"; # } # skip info lines next if ($line =~ m/^INFO:/); next if ($line =~ m/^[0-9 :-]*INFO::/); next if ($line =~ m/^\d{2}\/\d{2}\/\d{4}\s+/); next if ($line =~ m/^\d{4}-\d{2}-\d{2}\s+/); ## next if ($line =~ m/^\s*\w+{.*}/); # if here, then some non-trival message has been logged print "Jetty/Solr processing: $line"; } close(STARTIN); # And now stop nicely exit 0; } # otherwise let the parent continue on } elsif ($server_status eq "already-running") { print STDERR "Using existing server detected on port $jetty_server_port\n"; $self->{'jetty_explicitly_started'} = 0; } elsif ($server_status eq "failed-to-start") { print STDERR "Started Solr/Jetty web server on port $jetty_server_port"; print STDERR ", but encountered an initialization error\n"; exit -1; } } sub explicitly_started { my $self = shift @_; return $self->{'jetty_explicitly_started'}; } sub stop { my $self = shift @_; my ($options) = @_; my $solr_home = $ENV{'GEXT_SOLR'}; chdir($solr_home); # defaults my $do_wait = 1; my $output_verbosity = 1; if (defined $options) { if (defined $options->{'do_wait'}) { $do_wait = $options->{'do_wait'}; } if (defined $options->{'output_verbosity'}) { $output_verbosity = $options->{'output_verbosity'}; } } my $full_server_jar = $self->{'full_server_jar'}; my $jetty_stop_port = $ENV{'JETTY_STOP_PORT'}; my $server_props = "-DSTOP.PORT=$jetty_stop_port"; $server_props .= " -DSTOP.KEY=".$self->{'jetty_stop_key'}; my $server_java_cmd = "java $server_props -jar \"$full_server_jar\" --stop"; ## print STDERR "**** java server stop cmd:\n $server_java_cmd\n"; if (open(STOPIN,"$server_java_cmd 2>&1 |")) { my $line; while (defined($line=)) { print "Jetty shutdown: $line" if ($output_verbosity>1); } close(STOPIN); if ($do_wait) { wait(); # let the child process finish } if ($output_verbosity>0) { print "Jetty server shutdown\n"; } } else { print STDERR "Error: failed to stop solr-jetty-server\n"; print STDERR "$!\n"; print STDERR "Command attempted was:\n"; print STDERR " $server_java_cmd\n"; print STDERR "run from directory:\n"; print STDERR " $solr_home\n"; print STDERR "----\n"; exit -2; } } 1;