[24453] | 1 | ###########################################################################
|
---|
| 2 | #
|
---|
| 3 | # solrserver.pm -- class for starting and stopping the Solr/jetty server
|
---|
| 4 | # A component of the Greenstone digital library software
|
---|
| 5 | # from the New Zealand Digital Library Project at the
|
---|
| 6 | # University of Waikato, New Zealand.
|
---|
| 7 | #
|
---|
| 8 | # Copyright (C) 1999 New Zealand Digital Library Project
|
---|
| 9 | #
|
---|
| 10 | # This program is free software; you can redistribute it and/or modify
|
---|
| 11 | # it under the terms of the GNU General Public License as published by
|
---|
| 12 | # the Free Software Foundation; either version 2 of the License, or
|
---|
| 13 | # (at your option) any later version.
|
---|
| 14 | #
|
---|
| 15 | # This program is distributed in the hope that it will be useful,
|
---|
| 16 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 17 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 18 | # GNU General Public License for more details.
|
---|
| 19 | #
|
---|
| 20 | # You should have received a copy of the GNU General Public License
|
---|
| 21 | # along with this program; if not, write to the Free Software
|
---|
| 22 | # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
| 23 | #
|
---|
| 24 | ###########################################################################
|
---|
| 25 |
|
---|
| 26 |
|
---|
| 27 | package solrserver;
|
---|
| 28 |
|
---|
| 29 | use strict;
|
---|
[24483] | 30 | #no strict 'refs';
|
---|
[24453] | 31 |
|
---|
| 32 | use solrutil;
|
---|
| 33 |
|
---|
| 34 | sub new {
|
---|
| 35 | my $class = shift(@_);
|
---|
| 36 |
|
---|
[24484] | 37 | my $self = { 'jetty_stop_key' => "greenstone-solr" };
|
---|
[24453] | 38 |
|
---|
| 39 | my $search_path = &solrutil::get_search_path();
|
---|
| 40 |
|
---|
| 41 | my $server_jar = &util::filename_cat("lib","java","solr-jetty-server.jar");
|
---|
| 42 | my $full_server_jar = solrutil::locate_file($search_path,$server_jar);
|
---|
| 43 | $self->{'full_server_jar'} = $full_server_jar;
|
---|
| 44 |
|
---|
| 45 | $self->{'jetty_explicitly_started'} = undef;
|
---|
| 46 |
|
---|
[24483] | 47 | my $jetty_server_port = $ENV{'SOLR_JETTY_PORT'};
|
---|
| 48 | my $base_url = "http://localhost:$jetty_server_port/solr/";
|
---|
| 49 | my $admin_url = "http://localhost:$jetty_server_port/solr/admin/cores";
|
---|
| 50 |
|
---|
| 51 | $self->{'base-url'} = $base_url;
|
---|
| 52 | $self->{'admin-url'} = $admin_url;
|
---|
| 53 |
|
---|
[24453] | 54 | return bless $self, $class;
|
---|
| 55 | }
|
---|
| 56 |
|
---|
[24483] | 57 |
|
---|
| 58 |
|
---|
| 59 | sub _wget_service
|
---|
| 60 | {
|
---|
| 61 | my $self = shift (@_);
|
---|
| 62 | my ($output_format,$url,$cgi_get_args) = @_;
|
---|
| 63 |
|
---|
| 64 | my $full_url = $url;
|
---|
| 65 |
|
---|
| 66 | $url .= "?$cgi_get_args" if (defined $cgi_get_args);
|
---|
| 67 |
|
---|
| 68 | my $cmd = "wget -O - \"$url\" 2>&1";
|
---|
| 69 |
|
---|
| 70 | my $preamble_output = "";
|
---|
| 71 | my $xml_output = "";
|
---|
| 72 | my $error_output = undef;
|
---|
| 73 |
|
---|
| 74 | my $in_preamble = ($output_format eq "xml") ? 1 : 0;
|
---|
| 75 |
|
---|
| 76 | if (open(WIN,"$cmd |")) {
|
---|
| 77 |
|
---|
| 78 | my $line;
|
---|
| 79 | while (defined ($line=<WIN>)) {
|
---|
| 80 |
|
---|
| 81 | if ($line =~ m/ERROR \d+:/) {
|
---|
| 82 | chomp $line;
|
---|
| 83 | $error_output = $line;
|
---|
| 84 | last;
|
---|
| 85 | }
|
---|
| 86 | elsif ($line =~ m/failed: Connection refused/) {
|
---|
| 87 | chomp $line;
|
---|
| 88 | $error_output = $line;
|
---|
| 89 | last;
|
---|
| 90 | }
|
---|
| 91 | elsif ($in_preamble) {
|
---|
| 92 | if ($line =~ m/<.*>/) {
|
---|
| 93 | $in_preamble = 0;
|
---|
| 94 | }
|
---|
| 95 | else {
|
---|
| 96 | $preamble_output .= $line;
|
---|
| 97 | }
|
---|
| 98 | }
|
---|
| 99 |
|
---|
| 100 | if (! $in_preamble) {
|
---|
| 101 | $xml_output .= $line;
|
---|
| 102 | }
|
---|
| 103 | }
|
---|
| 104 | close(WIN);
|
---|
| 105 |
|
---|
| 106 | }
|
---|
| 107 | else {
|
---|
| 108 | $error_output = "Error: failed to run $cmd\n";
|
---|
| 109 | $error_output .= " $!\n";
|
---|
| 110 | }
|
---|
| 111 |
|
---|
| 112 | my $output = { 'preamble' => $preamble_output,
|
---|
| 113 | 'output' => $xml_output,
|
---|
| 114 | 'error' => $error_output };
|
---|
| 115 |
|
---|
| 116 | return $output;
|
---|
| 117 | }
|
---|
| 118 |
|
---|
| 119 |
|
---|
| 120 | sub _base_service
|
---|
| 121 | {
|
---|
| 122 | my $self = shift (@_);
|
---|
| 123 | my ($cgi_get_args) = @_;
|
---|
| 124 |
|
---|
| 125 | my $base_url = $self->{'base-url'};
|
---|
| 126 |
|
---|
| 127 | return $self->_wget_service("html",$base_url,$cgi_get_args);
|
---|
| 128 | }
|
---|
| 129 |
|
---|
| 130 | sub _admin_service
|
---|
| 131 | {
|
---|
| 132 | my $self = shift (@_);
|
---|
| 133 | my ($cgi_get_args) = @_;
|
---|
| 134 |
|
---|
| 135 | my $admin_url = $self->{'admin-url'};
|
---|
| 136 |
|
---|
| 137 | return $self->_wget_service("xml",$admin_url,$cgi_get_args);
|
---|
| 138 | }
|
---|
| 139 |
|
---|
| 140 |
|
---|
| 141 | sub server_running
|
---|
| 142 | {
|
---|
| 143 | my $self = shift @_;
|
---|
| 144 |
|
---|
| 145 | my $output = $self->_base_service();
|
---|
| 146 |
|
---|
| 147 | my $have_error = defined $output->{'error'};
|
---|
| 148 |
|
---|
| 149 | my $running = !$have_error;
|
---|
| 150 |
|
---|
| 151 | return $running;
|
---|
| 152 | }
|
---|
| 153 |
|
---|
| 154 |
|
---|
| 155 | sub admin_ping_core
|
---|
| 156 | {
|
---|
| 157 | my $self = shift @_;
|
---|
| 158 | my ($core) = @_;
|
---|
| 159 |
|
---|
| 160 | my $cgi_get_args = "action=STATUS&core=$core";
|
---|
| 161 |
|
---|
| 162 | my $ping_status = 1;
|
---|
| 163 |
|
---|
| 164 | my $output = $self->_admin_service($cgi_get_args);
|
---|
| 165 |
|
---|
| 166 | if (defined $output->{'error'}) {
|
---|
| 167 | # severe error, such as failing to connect to the server
|
---|
| 168 | $ping_status = 0;
|
---|
| 169 |
|
---|
| 170 | my $url = $output->{'url'};
|
---|
| 171 | my $preamble = $output->{'preamble'};
|
---|
| 172 | my $error = $output->{'error'};
|
---|
| 173 |
|
---|
| 174 | print STDERR "----\n";
|
---|
| 175 | print STDERR "Error: Failed to get XML response from:\n";
|
---|
| 176 | print STDERR " $url\n";
|
---|
| 177 | print STDERR "Output was:\n";
|
---|
| 178 | print STDERR $preamble if ($preamble ne "");
|
---|
| 179 | print STDERR "$error\n";
|
---|
| 180 | print STDERR "----\n";
|
---|
| 181 | }
|
---|
| 182 | else {
|
---|
| 183 |
|
---|
| 184 | # If the collection doesn't exist yet, then there will be
|
---|
| 185 | # an empty element of the form:
|
---|
| 186 | # <lst name="collect-doc"/>
|
---|
| 187 | # where 'collect' is the actual name of the collection,
|
---|
| 188 | # such as demo
|
---|
| 189 |
|
---|
| 190 | my $xml_output = $output->{'output'};
|
---|
| 191 |
|
---|
| 192 | my $empty_element="<lst\\s+name=\"$core\"\\s*\\/>";
|
---|
| 193 |
|
---|
| 194 | $ping_status = !($xml_output =~ m/$empty_element/s);
|
---|
| 195 | }
|
---|
| 196 |
|
---|
| 197 | return $ping_status;
|
---|
| 198 | }
|
---|
| 199 |
|
---|
| 200 |
|
---|
| 201 | sub admin_reload_core
|
---|
| 202 | {
|
---|
| 203 | my $self = shift @_;
|
---|
| 204 | my ($core) = @_;
|
---|
| 205 |
|
---|
| 206 | my $cgi_get_args = "action=RELOAD&core=$core";
|
---|
| 207 |
|
---|
| 208 | $self->_admin_service($cgi_get_args);
|
---|
| 209 | }
|
---|
| 210 |
|
---|
| 211 |
|
---|
| 212 | sub admin_create_core
|
---|
| 213 | {
|
---|
| 214 | my $self = shift @_;
|
---|
| 215 | my ($core) = @_;
|
---|
| 216 |
|
---|
| 217 | my ($ds_idx) = ($core =~ m/^.*-(.*)$/);
|
---|
| 218 |
|
---|
| 219 | my $cgi_get_args = "action=CREATE&name=$core";
|
---|
| 220 |
|
---|
| 221 | my $collect_home = $ENV{'GSDLCOLLECTDIR'};
|
---|
| 222 | my $etc_dirname = &util::filename_cat($collect_home,"etc");
|
---|
| 223 |
|
---|
| 224 | my $build_dir = $self->{'build_dir'};
|
---|
| 225 | my $idx_dirname = &util::filename_cat($build_dir,$ds_idx);
|
---|
| 226 |
|
---|
| 227 | $cgi_get_args .= "&instanceDir=$etc_dirname";
|
---|
| 228 | $cgi_get_args .= "&dataDir=$idx_dirname";
|
---|
| 229 |
|
---|
| 230 | $self->_admin_service($cgi_get_args);
|
---|
| 231 | }
|
---|
| 232 |
|
---|
| 233 |
|
---|
| 234 |
|
---|
[24453] | 235 | sub start
|
---|
| 236 | {
|
---|
| 237 | my $self = shift @_;
|
---|
| 238 |
|
---|
| 239 | my $solr_home = $ENV{'GEXT_SOLR'};
|
---|
| 240 | my $jetty_stop_port = $ENV{'JETTY_STOP_PORT'};
|
---|
| 241 | my $jetty_server_port = $ENV{'SOLR_JETTY_PORT'};
|
---|
| 242 |
|
---|
| 243 | chdir($solr_home);
|
---|
| 244 |
|
---|
| 245 | my $solr_etc = &util::filename_cat($solr_home,"etc");
|
---|
| 246 |
|
---|
| 247 | my $server_props = "-DSTOP.PORT=$jetty_stop_port";
|
---|
| 248 | $server_props .= " -DSTOP.KEY=".$self->{'jetty_stop_key'};
|
---|
| 249 | $server_props .= " -Dsolr.solr.home=$solr_etc";
|
---|
| 250 |
|
---|
| 251 | my $full_server_jar = $self->{'full_server_jar'};
|
---|
| 252 |
|
---|
| 253 | my $server_java_cmd = "java $server_props -jar \"$full_server_jar\"";
|
---|
| 254 |
|
---|
[24483] | 255 | ## print STDERR "**** server cmd start = $server_java_cmd\n";
|
---|
[24453] | 256 |
|
---|
[24483] | 257 | my $server_status = "unknown";
|
---|
[24453] | 258 |
|
---|
[24483] | 259 | my $server_already_running = $self->server_running();
|
---|
[24453] | 260 |
|
---|
[24483] | 261 | if ($server_already_running) {
|
---|
| 262 | $server_status = "already-running";
|
---|
[24453] | 263 | }
|
---|
[24483] | 264 | elsif (open(STARTIN,"$server_java_cmd 2>&1 |")) {
|
---|
[24453] | 265 |
|
---|
[24483] | 266 | my $line;
|
---|
| 267 | while (defined($line=<STARTIN>)) {
|
---|
| 268 | # Scan through output until you see a line like:
|
---|
| 269 | # 2011-08-22 .. :INFO::Started [email protected]:8983
|
---|
| 270 | # which signifies that the server has started up and is
|
---|
| 271 | # "ready and listening"
|
---|
[24453] | 272 |
|
---|
[24483] | 273 | ## print STDERR "**** $line";
|
---|
[24453] | 274 |
|
---|
[24483] | 275 | # skip annoying "not listening" message
|
---|
| 276 | next if ($line =~ m/WARN:\s*Not listening on monitor port/);
|
---|
[24453] | 277 |
|
---|
[24483] | 278 | if (($line =~ m/^(WARN|ERROR|SEVERE):/)
|
---|
| 279 | || ($line =~ m/^[0-9 :-]*(WARN|ERROR|SEVERE)::/)) {
|
---|
| 280 | print "Jetty startup: $line";
|
---|
[24453] | 281 | }
|
---|
[24483] | 282 |
|
---|
| 283 | if ($line =~ m/WARN::failed SocketConnector/) {
|
---|
| 284 | if ($line =~ m/Address already in use/) {
|
---|
| 285 | $server_status = "already-running";
|
---|
| 286 | }
|
---|
| 287 | else {
|
---|
| 288 | $server_status = "failed-to-start";
|
---|
| 289 | }
|
---|
| 290 | last;
|
---|
[24453] | 291 | }
|
---|
[24483] | 292 |
|
---|
| 293 | if ($line =~ m/INFO::Started SocketConnector/) {
|
---|
| 294 | $server_status = "explicitly-started";
|
---|
| 295 | last;
|
---|
| 296 | }
|
---|
[24453] | 297 | }
|
---|
| 298 | }
|
---|
[24483] | 299 | else {
|
---|
| 300 | print STDERR "Error: failed to start solr-jetty-server\n";
|
---|
| 301 | print STDERR "$!\n";
|
---|
| 302 | print STDERR "Command attempted was:\n";
|
---|
| 303 | print STDERR " $server_java_cmd\n";
|
---|
| 304 | print STDERR "run from directory:\n";
|
---|
| 305 | print STDERR " $solr_home\n";
|
---|
| 306 | print STDERR "----\n";
|
---|
| 307 |
|
---|
| 308 | exit -1;
|
---|
| 309 | }
|
---|
| 310 |
|
---|
[24453] | 311 | if ($server_status eq "explicitly-started") {
|
---|
| 312 | $self->{'jetty_explicitly_started'} = 1;
|
---|
[24483] | 313 | print "Jetty server ready and listening for connections on port";
|
---|
| 314 | print " $jetty_server_port\n";
|
---|
[24453] | 315 |
|
---|
| 316 | # now we know the server is ready to accept connections, fork a
|
---|
| 317 | # child process that continues to listen to the output and
|
---|
| 318 | # prints out any lines that are not INFO lines
|
---|
| 319 |
|
---|
| 320 | if (fork()==0) {
|
---|
| 321 | # child process
|
---|
[24483] | 322 |
|
---|
[24453] | 323 | my $line;
|
---|
| 324 | while (defined ($line = <STARTIN>)) {
|
---|
| 325 |
|
---|
| 326 | # if (($line =~ m/^(WARN|ERROR|SEVERE):/)
|
---|
| 327 | # || ($line =~ m/^[0-9 :-]*(WARN|ERROR|SEVERE)::/)) {
|
---|
| 328 | # print "Jetty $line";
|
---|
| 329 | # }
|
---|
| 330 |
|
---|
| 331 |
|
---|
| 332 | # skip info lines
|
---|
| 333 | next if ($line =~ m/^INFO:/);
|
---|
| 334 | next if ($line =~ m/^[0-9 :-]*INFO::/);
|
---|
| 335 | next if ($line =~ m/^\d{2}\/\d{2}\/\d{4}\s+/);
|
---|
| 336 | next if ($line =~ m/^\d{4}-\d{2}-\d{2}\s+/);
|
---|
| 337 |
|
---|
| 338 | ## next if ($line =~ m/^\s*\w+{.*}/);
|
---|
| 339 |
|
---|
| 340 | # if here, then some non-trival message has been logged
|
---|
| 341 | print "Jetty/Solr processing: $line";
|
---|
| 342 | }
|
---|
| 343 | close(STARTIN);
|
---|
| 344 |
|
---|
| 345 | # And now stop nicely
|
---|
| 346 | exit 0;
|
---|
| 347 | }
|
---|
| 348 | # otherwise let the parent continue on
|
---|
| 349 | }
|
---|
| 350 | elsif ($server_status eq "already-running") {
|
---|
[24483] | 351 | print STDERR "Using existing server detected on port $jetty_server_port\n";
|
---|
[24453] | 352 | $self->{'jetty_explicitly_started'} = 0;
|
---|
| 353 | }
|
---|
[24483] | 354 | elsif ($server_status eq "failed-to-start") {
|
---|
| 355 | print STDERR "Started Solr/Jetty web server on port $jetty_server_port";
|
---|
| 356 | print STDERR ", but encountered an initialization error\n";
|
---|
[24453] | 357 | exit -1;
|
---|
| 358 | }
|
---|
| 359 |
|
---|
| 360 | }
|
---|
| 361 |
|
---|
| 362 | sub explicitly_started
|
---|
| 363 | {
|
---|
| 364 | my $self = shift @_;
|
---|
| 365 |
|
---|
| 366 | return $self->{'jetty_explicitly_started'};
|
---|
| 367 | }
|
---|
| 368 |
|
---|
| 369 | sub stop
|
---|
| 370 | {
|
---|
| 371 | my $self = shift @_;
|
---|
| 372 | my ($options) = @_;
|
---|
| 373 |
|
---|
| 374 | my $solr_home = $ENV{'GEXT_SOLR'};
|
---|
| 375 |
|
---|
| 376 | chdir($solr_home);
|
---|
| 377 |
|
---|
| 378 | # defaults
|
---|
| 379 | my $do_wait = 1;
|
---|
| 380 | my $output_verbosity = 1;
|
---|
| 381 |
|
---|
| 382 | if (defined $options) {
|
---|
| 383 | if (defined $options->{'do_wait'}) {
|
---|
| 384 | $do_wait = $options->{'do_wait'};
|
---|
| 385 | }
|
---|
| 386 | if (defined $options->{'output_verbosity'}) {
|
---|
| 387 | $output_verbosity = $options->{'output_verbosity'};
|
---|
| 388 | }
|
---|
| 389 | }
|
---|
| 390 |
|
---|
| 391 | my $full_server_jar = $self->{'full_server_jar'};
|
---|
| 392 | my $jetty_stop_port = $ENV{'JETTY_STOP_PORT'};
|
---|
| 393 |
|
---|
| 394 | my $server_props = "-DSTOP.PORT=$jetty_stop_port";
|
---|
| 395 | $server_props .= " -DSTOP.KEY=".$self->{'jetty_stop_key'};
|
---|
| 396 | my $server_java_cmd = "java $server_props -jar \"$full_server_jar\" --stop";
|
---|
[24483] | 397 |
|
---|
| 398 | ## print STDERR "**** java server stop cmd:\n $server_java_cmd\n";
|
---|
| 399 |
|
---|
[24453] | 400 | if (open(STOPIN,"$server_java_cmd 2>&1 |")) {
|
---|
| 401 |
|
---|
| 402 | my $line;
|
---|
| 403 | while (defined($line=<STOPIN>)) {
|
---|
| 404 | print "Jetty shutdown: $line" if ($output_verbosity>1);
|
---|
| 405 | }
|
---|
| 406 | close(STOPIN);
|
---|
| 407 |
|
---|
| 408 | if ($do_wait) {
|
---|
| 409 | wait(); # let the child process finish
|
---|
| 410 | }
|
---|
| 411 |
|
---|
| 412 | if ($output_verbosity>0) {
|
---|
| 413 | print "Jetty server shutdown\n";
|
---|
| 414 | }
|
---|
| 415 | }
|
---|
| 416 | else {
|
---|
| 417 | print STDERR "Error: failed to stop solr-jetty-server\n";
|
---|
[24483] | 418 | print STDERR "$!\n";
|
---|
[24453] | 419 | print STDERR "Command attempted was:\n";
|
---|
| 420 | print STDERR " $server_java_cmd\n";
|
---|
| 421 | print STDERR "run from directory:\n";
|
---|
| 422 | print STDERR " $solr_home\n";
|
---|
| 423 | print STDERR "----\n";
|
---|
| 424 |
|
---|
| 425 | exit -2;
|
---|
| 426 | }
|
---|
| 427 | }
|
---|
| 428 |
|
---|
| 429 |
|
---|
| 430 |
|
---|
| 431 | 1;
|
---|