root/gs3-extensions/solr/trunk/src/perllib/solrserver.pm @ 24486

Revision 24486, 9.9 KB (checked in by davidb, 8 years ago)

Tidy up (pass 2)

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
37    my $self = { 'jetty_stop_key' => "greenstone-solr" };
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
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
54    return bless $self, $class;
55}
56
57
58
59sub _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
120sub _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 
130sub _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
141sub 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
155sub 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
201sub 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
212sub 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
235sub 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
255##    print STDERR "**** server cmd start = $server_java_cmd\n";
256
257    my $server_status = "unknown";
258
259    if ($self->server_running()) {
260    $server_status = "already-running";
261    }
262    elsif (open(STARTIN,"$server_java_cmd 2>&1 |")) {
263
264    my $line;
265    while (defined($line=<STARTIN>)) {
266        # Scan through output until you see a line like:
267        #   2011-08-22 .. :INFO::Started SocketConnector@0.0.0.0:8983
268        # which signifies that the server has started up and is
269        # "ready and listening"
270   
271        if (($line =~ m/^(WARN|ERROR|SEVERE):/)
272        || ($line =~ m/^[0-9 :-]*(WARN|ERROR|SEVERE)::/)) {
273        print "Jetty startup: $line";
274        }
275       
276        if ($line =~ m/WARN::failed SocketConnector/) {
277        if ($line =~ m/Address already in use/) {
278            $server_status = "already-running";
279        }
280        else {
281            $server_status = "failed-to-start";
282        }
283        last;
284        }
285       
286        if ($line =~ m/INFO::Started SocketConnector/) {
287        $server_status = "explicitly-started";
288        last;
289        }
290    }
291    }
292    else {
293    print STDERR "Error: failed to start solr-jetty-server\n";
294    print STDERR "$!\n";
295    print STDERR "Command attempted was:\n";
296    print STDERR "  $server_java_cmd\n";
297    print STDERR "run from directory:\n";
298    print STDERR "  $solr_home\n";
299    print STDERR "----\n";
300
301    exit -1;
302    }
303
304    if ($server_status eq "explicitly-started") {
305    $self->{'jetty_explicitly_started'} = 1;
306    print "Jetty server ready and listening for connections on port";
307    print " $jetty_server_port\n";
308       
309    # now we know the server is ready to accept connections, fork a
310    # child process that continues to listen to the output and
311    # prints out any lines that are not INFO lines
312
313    if (fork()==0) {
314        # child process
315
316        my $line;
317        while (defined ($line = <STARTIN>)) {
318
319#       if (($line =~ m/^(WARN|ERROR|SEVERE):/)
320#           || ($line =~ m/^[0-9 :-]*(WARN|ERROR|SEVERE)::/)) {
321#           print "Jetty $line";
322#       }
323
324        # skip info lines
325        next if ($line =~ m/^INFO:/);
326        next if ($line =~ m/^[0-9 :-]*INFO::/);
327        next if ($line =~ m/^\d{2}\/\d{2}\/\d{4}\s+/);
328        next if ($line =~ m/^\d{4}-\d{2}-\d{2}\s+/);
329
330##      next if ($line =~ m/^\s*\w+{.*}/);
331
332        # if here, then some non-trival message has been logged
333        print "Jetty/Solr processing: $line";
334        }
335        close(STARTIN);
336       
337        # And now stop nicely
338        exit 0;
339    }
340    # otherwise let the parent continue on
341    }
342    elsif ($server_status eq "already-running") {
343    print STDERR "Using existing server detected on port $jetty_server_port\n";
344    $self->{'jetty_explicitly_started'} = 0;
345    }
346    elsif ($server_status eq "failed-to-start") {
347    print STDERR "Started Solr/Jetty web server on port $jetty_server_port";
348    print STDERR ", but encountered an initialization error\n";
349    exit -1;
350    }
351
352}
353
354sub explicitly_started
355{
356    my $self = shift @_;
357
358    return $self->{'jetty_explicitly_started'};
359}
360
361sub stop
362{   
363    my $self = shift @_;
364    my ($options) = @_;
365
366    my $solr_home         = $ENV{'GEXT_SOLR'};
367
368    chdir($solr_home);
369
370    # defaults
371    my $do_wait = 1;
372    my $output_verbosity = 1;
373
374    if (defined $options) {
375    if (defined $options->{'do_wait'}) {
376        $do_wait = $options->{'do_wait'};
377    }
378    if (defined $options->{'output_verbosity'}) {
379        $output_verbosity = $options->{'output_verbosity'};
380    }
381    }
382
383    my $full_server_jar = $self->{'full_server_jar'};
384    my $jetty_stop_port = $ENV{'JETTY_STOP_PORT'};
385   
386    my $server_props = "-DSTOP.PORT=$jetty_stop_port";
387    $server_props   .= " -DSTOP.KEY=".$self->{'jetty_stop_key'};
388    my $server_java_cmd = "java $server_props -jar \"$full_server_jar\" --stop";
389
390##    print STDERR "**** java server stop cmd:\n  $server_java_cmd\n";
391
392    if (open(STOPIN,"$server_java_cmd 2>&1 |")) {
393
394    my $line;
395    while (defined($line=<STOPIN>)) {
396        print "Jetty shutdown: $line" if ($output_verbosity>1);
397    }
398    close(STOPIN);
399
400    if ($do_wait) {
401        wait(); # let the child process finish
402    }
403
404    if ($output_verbosity>0) {
405        print "Jetty server shutdown\n";
406    }
407    }
408    else {
409    print STDERR "Error: failed to stop solr-jetty-server\n";
410    print STDERR "$!\n";
411    print STDERR "Command attempted was:\n";
412    print STDERR "  $server_java_cmd\n";
413    print STDERR "run from directory:\n";
414    print STDERR "  $solr_home\n";
415    print STDERR "----\n";
416
417    exit -2;
418    }
419}
420
421
422
4231;
Note: See TracBrowser for help on using the browser.