source: gs3-extensions/solr/trunk/src/perllib/solrserver.pm@ 24501

Last change on this file since 24501 was 24501, checked in by davidb, 13 years ago

Relocation of files to make solr.solr.home more natural. Plus, more carefully control the order in which the build_dir/index_dir folder is deleted in. For solr we need to do this earlier than lucene

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