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

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

Tidy up of code. Better structuring of classes

File size: 6.7 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
31use solrutil;
32
33sub new {
34 my $class = shift(@_);
35
36 my $self = { 'jetty_stop_key' => "greenstone-solr" };
37
38 my $search_path = &solrutil::get_search_path();
39
40 my $server_jar = &util::filename_cat("lib","java","solr-jetty-server.jar");
41 my $full_server_jar = solrutil::locate_file($search_path,$server_jar);
42 $self->{'full_server_jar'} = $full_server_jar;
43
44 $self->{'jetty_explicitly_started'} = undef;
45
46 return bless $self, $class;
47}
48
49sub start
50{
51 my $self = shift @_;
52
53 my $solr_home = $ENV{'GEXT_SOLR'};
54 my $jetty_stop_port = $ENV{'JETTY_STOP_PORT'};
55 my $jetty_server_port = $ENV{'SOLR_JETTY_PORT'};
56
57 chdir($solr_home);
58
59 my $solr_etc = &util::filename_cat($solr_home,"etc");
60
61 my $server_props = "-DSTOP.PORT=$jetty_stop_port";
62 $server_props .= " -DSTOP.KEY=".$self->{'jetty_stop_key'};
63 $server_props .= " -Dsolr.solr.home=$solr_etc";
64
65 my $full_server_jar = $self->{'full_server_jar'};
66
67 my $server_java_cmd = "java $server_props -jar \"$full_server_jar\"";
68
69## print STDERR "**** server cmd = $server_java_cmd\n";
70
71 my $pid = open(STARTIN,"-|");
72
73 if ($pid==0) {
74 # child process that will start up jetty and monitor output
75
76 setpgrp(0,0);
77
78 exec "$server_java_cmd 2>&1" || die "Failed to execute $server_java_cmd\n$!\n";
79 # everything stops here
80 }
81
82 my $server_status = "unknown";
83
84 my $line;
85 while (defined($line=<STARTIN>)) {
86 # Scan through output until you see a line like:
87 # 2011-08-22 .. :INFO::Started [email protected]:8983
88 # which signifies that the server has started up and is
89 # "ready and listening"
90
91## print STDERR "**** $line";
92
93 # skip annoying "not listening" message
94 next if ($line =~ m/WARN:\s*Not listening on monitor port/);
95
96 if (($line =~ m/^(WARN|ERROR|SEVERE):/)
97 || ($line =~ m/^[0-9 :-]*(WARN|ERROR|SEVERE)::/)) {
98 print "Jetty startup: $line";
99 }
100
101
102 if ($line =~ m/WARN::failed SocketConnector/) {
103 if ($line =~ m/Address already in use/) {
104 $server_status = "already-running";
105 }
106 else {
107 $server_status = "failed-to-start";
108 }
109 last;
110 }
111
112 if ($line =~ m/INFO::Started SocketConnector/) {
113 $server_status = "explicitly-started";
114 last;
115 }
116 }
117
118 if ($server_status eq "explicitly-started") {
119 $self->{'jetty_explicitly_started'} = 1;
120 print "Jetty server ready and listening for connections on port $jetty_server_port\n";
121
122 # now we know the server is ready to accept connections, fork a
123 # child process that continues to listen to the output and
124 # prints out any lines that are not INFO lines
125
126 if (fork()==0) {
127 # child process
128
129 my $line;
130 while (defined ($line = <STARTIN>)) {
131
132# if (($line =~ m/^(WARN|ERROR|SEVERE):/)
133# || ($line =~ m/^[0-9 :-]*(WARN|ERROR|SEVERE)::/)) {
134# print "Jetty $line";
135# }
136
137
138 # skip info lines
139 next if ($line =~ m/^INFO:/);
140 next if ($line =~ m/^[0-9 :-]*INFO::/);
141 next if ($line =~ m/^\d{2}\/\d{2}\/\d{4}\s+/);
142 next if ($line =~ m/^\d{4}-\d{2}-\d{2}\s+/);
143
144## next if ($line =~ m/^\s*\w+{.*}/);
145
146 # if here, then some non-trival message has been logged
147 print "Jetty/Solr processing: $line";
148 }
149 close(STARTIN);
150
151 # And now stop nicely
152 exit 0;
153 }
154 # otherwise let the parent continue on
155 }
156 elsif ($server_status eq "already-running") {
157 print "Using existing server detected on port $jetty_server_port\n";
158 $self->{'jetty_explicitly_started'} = 0;
159
160 # Kill of the child process
161
162 my $ks = kill(9,-$pid);
163
164 # Consume any remaining (buffered) output (not interested in values)
165 while (defined ($line = <STARTIN>)) {
166 # skip info lines
167 }
168 close(STARTIN);
169 }
170 else {
171 print STDERR "Failed to start Solr/Jetty web server on $jetty_server_port\n";
172 exit -1;
173 }
174
175
176# else {
177# print STDERR "Error: failed to start solr-jetty-server\n";
178# print STDERR "!$\n\n";
179# print STDERR "Command attempted was:\n";
180# print STDERR " $server_java_cmd\n";
181# print STDERR "run from directory:\n";
182# print STDERR " $solr_home\n";
183# print STDERR "----\n";
184
185# exit -1;
186# }
187
188# # If get to here then server started (and ready and listening)
189# # *and* we are the parent process of the fork()
190
191}
192
193
194sub explicitly_started
195{
196 my $self = shift @_;
197
198 return $self->{'jetty_explicitly_started'};
199}
200
201
202sub stop
203{
204 my $self = shift @_;
205 my ($options) = @_;
206
207 my $solr_home = $ENV{'GEXT_SOLR'};
208
209 chdir($solr_home);
210
211 # defaults
212 my $do_wait = 1;
213 my $output_verbosity = 1;
214
215 if (defined $options) {
216 if (defined $options->{'do_wait'}) {
217 $do_wait = $options->{'do_wait'};
218 }
219 if (defined $options->{'output_verbosity'}) {
220 $output_verbosity = $options->{'output_verbosity'};
221 }
222 }
223
224 my $full_server_jar = $self->{'full_server_jar'};
225 my $jetty_stop_port = $ENV{'JETTY_STOP_PORT'};
226
227 my $server_props = "-DSTOP.PORT=$jetty_stop_port";
228 $server_props .= " -DSTOP.KEY=".$self->{'jetty_stop_key'};
229 my $server_java_cmd = "java $server_props -jar \"$full_server_jar\" --stop";
230 if (open(STOPIN,"$server_java_cmd 2>&1 |")) {
231
232 my $line;
233 while (defined($line=<STOPIN>)) {
234 print "Jetty shutdown: $line" if ($output_verbosity>1);
235 }
236 close(STOPIN);
237
238 if ($do_wait) {
239 wait(); # let the child process finish
240 }
241
242 if ($output_verbosity>0) {
243 print "Jetty server shutdown\n";
244 }
245 }
246 else {
247 print STDERR "Error: failed to stop solr-jetty-server\n";
248 print STDERR "!$\n";
249 print STDERR "Command attempted was:\n";
250 print STDERR " $server_java_cmd\n";
251 print STDERR "run from directory:\n";
252 print STDERR " $solr_home\n";
253 print STDERR "----\n";
254
255 exit -2;
256 }
257}
258
259
260
2611;
Note: See TracBrowser for help on using the repository browser.