]> git.evergreen-ils.org Git - OpenSRF.git/blob - src/perlmods/OpenSRF/System.pm
added named params to bootstrap_client to allow config file
[OpenSRF.git] / src / perlmods / OpenSRF / System.pm
1 package OpenSRF::System;
2 use strict; use warnings;
3 use base 'OpenSRF';
4 use OpenSRF::Utils::Logger qw(:level);
5 use OpenSRF::Transport::Listener;
6 use OpenSRF::Transport;
7 use OpenSRF::UnixServer;
8 use OpenSRF::Utils;
9 use OpenSRF::Utils::LogServer;
10 use OpenSRF::DOM;
11 use OpenSRF::EX qw/:try/;
12 use POSIX ":sys_wait_h";
13 use OpenSRF::Utils::Config; 
14 use OpenSRF::Utils::SettingsParser;
15 use OpenSRF::Utils::SettingsClient;
16 use Net::Server::PreFork;
17 use strict;
18
19 my $bootstrap_config_file;
20 sub import {
21         my( $self, $config ) = @_;
22         $bootstrap_config_file = $config;
23 }
24
25 =head2 Name/Description
26
27 OpenSRF::System
28
29 To start the system: OpenSRF::System->bootstrap();
30
31 Simple system process management and automation.  After instantiating the class, simply call
32 bootstrap() to launch the system.  Each launched process is stored as a process-id/method-name
33 pair in a local hash.  When we receive a SIG{CHILD}, we loop through this hash and relaunch
34 any child processes that may have terminated.  
35
36 Currently automated processes include launching the internal Unix Servers, launching the inbound 
37 connections for each application, and starting the system shell.
38
39
40 Note: There should be only one instance of this class
41 alive at any given time.  It is designed as a globel process handler and, hence, will cause much
42 oddness if you call the bootstrap() method twice or attempt to create two of these by trickery.
43 There is a single instance of the class created on the first call to new().  This same instance is 
44 returned on subsequent calls to new().
45
46 =cut
47
48 $| = 1;
49
50 sub DESTROY {}
51
52 # ----------------------------------------------
53
54 $SIG{INT} = sub { instance()->killall(); };
55
56 $SIG{HUP} = sub{ instance()->hupall(); };
57
58 $SIG{CHLD} = \&process_automation;
59
60
61
62         # --- 
63         # put $instance in a closure and return it for requests to new()
64         # since there should only be one System instance running
65         # ----- 
66         my $instance;
67         sub instance { return __PACKAGE__->new(); }
68         sub new {
69                 my( $class ) = @_;
70
71                 if( ! $instance ) {
72                         $class = ref( $class ) || $class;
73                         my $self = {};
74                         $self->{'pid_hash'} = {};
75                         bless( $self, $class );
76                         $instance = $self;
77                 }
78                 return $instance;
79         }
80 }
81
82 # ----------------------------------------------
83 # Commands to execute at system launch
84
85 sub _unixserver {
86         my( $app ) = @_;
87         return "OpenSRF::UnixServer->new( '$app')->serve()";
88 }
89
90 sub _listener {
91         my( $app ) = @_;
92
93 =head
94         my $service = $app;
95         my $username = $app;
96         my $password = "jkjkasdf";
97         my $jserver = "elroy";
98         my $port = "5222";
99         my $hostname = "elroy.pls-hq.org";
100         my $unix_file = "/pines/var/sock/$app" . "_unix.sock";
101         my $router = 'router\@elroy/router'; 
102         return "exec(\"/pines/cvs/ILS/OpenSRF/src/forker/oils_inbound $service $username " .
103                 "$password $jserver $port $hostname $unix_file $router\")";
104 =cut
105
106         return "OpenSRF::Transport::Listener->new( '$app' )->initialize()->listen()";
107 }
108
109
110 # ----------------------------------------------
111 # Boot up the system
112
113 sub load_bootstrap_config {
114
115         if(OpenSRF::Utils::Config->current) {
116                 return;
117         }
118
119         warn "Loading $bootstrap_config_file\n";
120         if(!$bootstrap_config_file) {
121                 die "Please provide a bootstrap config file to OpenSRF::System!\n" . 
122                         "use OpenSRF::System qw(/path/to/bootstrap_config);";
123         }
124
125         OpenSRF::Utils::Config->load( config_file => $bootstrap_config_file );
126
127         JSON->register_class_hint( name => "OpenSRF::Application", hint => "", type => "hash" );
128
129         OpenSRF::Transport->message_envelope(  "OpenSRF::Transport::SlimJabber::MessageWrapper" );
130         OpenSRF::Transport::PeerHandle->set_peer_client(  "OpenSRF::Transport::SlimJabber::PeerConnection" );
131         OpenSRF::Transport::Listener->set_listener( "OpenSRF::Transport::SlimJabber::Inbound" );
132 }
133
134 sub bootstrap {
135
136         my $self = __PACKAGE__->instance();
137         load_bootstrap_config();
138         OpenSRF::Utils::Logger::set_config();
139         my $bsconfig = OpenSRF::Utils::Config->current;
140
141         # Start a process group and make me the captain
142         setpgrp( 0, 0 ); 
143         $0 = "System";
144
145         # -----------------------------------------------
146         # Launch the settings sever if necessary...
147         my $are_settings_server = 0;
148         if( (my $cfile = $bsconfig->bootstrap->settings_config) ) {
149                 my $parser = OpenSRF::Utils::SettingsParser->new();
150
151                 # since we're (probably) the settings server, we can go ahead and load the real config file
152                 $parser->initialize( $cfile );
153                 $OpenSRF::Utils::SettingsClient::host_config = 
154                         $parser->get_server_config($bsconfig->env->hostname);
155
156                 my $client = OpenSRF::Utils::SettingsClient->new();
157                 my $apps = $client->config_value("activeapps", "appname");
158                 if(!ref($apps) eq "ARRAY") { $apps = [$apps]; }
159
160                 for my $app (@$apps) {
161                         # verify we are a settings server and launch 
162                         if( $app eq "settings" ) {
163                                 $are_settings_server = 1;
164                                 $self->launch_settings();
165                                 sleep 1;
166                                 $self->launch_settings_listener();
167                                 last;
168                         } 
169                 }
170         }
171
172         # Launch everything else
173         my $client = OpenSRF::Utils::SettingsClient->new();
174         my $apps = $client->config_value("activeapps", "appname" );
175         if(!ref($apps)) { $apps = [$apps]; }
176         my $server_type = $client->config_value("server_type");
177         $server_type ||= "basic";
178
179         my $con = OpenSRF::Transport::PeerHandle->retrieve;
180         if($con) {
181                 $con->disconnect;
182         }
183
184
185
186         if(  $server_type eq "prefork" ) { 
187                 $server_type = "Net::Server::PreFork"; 
188         } else { 
189                 $server_type = "Net::Server::Single"; 
190         }
191
192         _log( " * Server type: $server_type", INTERNAL );
193
194         eval "use $server_type";
195
196         if( $@ ) {
197                 throw OpenSRF::EX::PANIC ("Cannot set $server_type: $@" );
198         }
199
200         push @OpenSRF::UnixServer::ISA, $server_type;
201
202         _log( " * System boostrap" );
203         
204         # --- Boot the Unix servers
205         $self->launch_unix($apps);
206
207         _sleep();
208
209         # --- Boot the listeners
210         $self->launch_listener($apps);
211
212         _sleep();
213
214         _log( " * System is ready..." );
215
216         sleep 1;
217         my $ps = `ps ax | grep " Open" | grep -v grep | sort -r -k5`;
218
219         print "\n --- PS --- \n$ps --- PS ---\n\n";
220
221         while( 1 ) { sleep; }
222         exit;
223 }
224         
225         
226
227 # ----------------------------------------------
228 # Bootstraps a single client connection.  
229
230 sub bootstrap_client {
231
232         my %params = @_;
233
234         $bootstrap_config_file = 
235                 $params{config_file} || $bootstrap_config_file;
236
237         my $app = $params{client_name} || "client";
238
239
240         my $self = __PACKAGE__->instance();
241         load_bootstrap_config();
242         OpenSRF::Utils::Logger::set_config();
243
244         OpenSRF::Transport::PeerHandle->construct( $app );
245
246 }
247
248 sub bootstrap_logger {
249         $0 = "Log Server";
250         OpenSRF::Utils::LogServer->serve();
251 }
252
253
254 # ----------------------------------------------
255 # Cycle through the known processes, reap the dead child 
256 # and put a new child in its place. (MMWWAHAHHAHAAAA!)
257
258 sub process_automation {
259
260         my $self = __PACKAGE__->instance();
261
262         foreach my $pid ( keys %{$self->pid_hash} ) {
263
264                 if( waitpid( $pid, WNOHANG ) == $pid ) {
265
266                         my $method = $self->pid_hash->{$pid};
267                         delete $self->pid_hash->{$pid};
268
269                         my $newpid =  OpenSRF::Utils::safe_fork();
270
271                         OpenSRF::Utils::Logger->debug( "Relaunching $method", ERROR );
272                         _log( "Relaunching => $method" );
273
274                         if( $newpid ) {
275                                 $self->pid_hash( $newpid, $method );
276                         }
277                         else { eval $method; exit; }
278                 }
279         }
280
281         $SIG{CHLD} = \&process_automation;
282 }
283
284
285
286 sub launch_settings {
287
288         #       XXX the $self like this and pid automation will not work with this setup....
289         my($self) = @_;
290         @OpenSRF::UnixServer::ISA = qw(OpenSRF Net::Server::PreFork);
291
292         my $pid = OpenSRF::Utils::safe_fork();
293         if( $pid ) {
294                 $self->pid_hash( $pid , "launch_settings()" );
295         }
296         else {
297                 my $apname = "settings";
298                 $0 = "OpenSRF App ($apname)";
299                 eval _unixserver( "settings" );
300                 if($@) { die "$@\n"; }
301                 exit;
302         }
303
304         @OpenSRF::UnixServer::ISA = qw(OpenSRF);
305
306 }
307
308
309 sub launch_settings_listener {
310
311         my $self = shift;
312         my $app = "settings";
313         my $pid = OpenSRF::Utils::safe_fork();
314         if ( $pid ) {
315                 $self->pid_hash( $pid , _listener( $app ) );
316         }
317         else {
318                 my $apname = $app;
319                 $0 = "OpenSRF listener ($apname)";
320                 eval _listener( $app );
321                 exit;
322         }
323
324 }
325
326 # ----------------------------------------------
327 # Launch the Unix Servers
328
329 sub launch_unix {
330         my( $self, $apps ) = @_;
331
332         foreach my $app ( @$apps ) {
333                 next unless $app;
334
335                 if( $app eq "settings" ) { next; }
336
337                 _log( " * Starting UnixServer for $app..." );
338
339                 my $pid = OpenSRF::Utils::safe_fork();
340                 if( $pid ) {
341                         $self->pid_hash( $pid , _unixserver( $app ) );
342                 }
343                 else {
344                         my $apname = $app;
345                         $0 = "OpenSRF App ($apname)";
346                         eval _unixserver( $app );
347                         exit;
348                 }
349         }
350 }
351
352 # ----------------------------------------------
353 # Launch the inbound clients
354
355 sub launch_listener {
356
357         my( $self, $apps ) = @_;
358
359         foreach my $app ( @$apps ) {
360
361                 if( $app eq "settings" ) { next; }
362
363                 _log( " * Starting Listener for $app..." );
364
365                 my $pid = OpenSRF::Utils::safe_fork();
366                 if ( $pid ) {
367                         $self->pid_hash( $pid , _listener( $app ) );
368                 }
369                 else {
370                         my $apname = $app;
371                         $0 = "OpenSRF listener ($apname)";
372                         eval _listener( $app );
373                         exit;
374                 }
375         }
376 }
377
378 # ----------------------------------------------
379
380 =head comment
381 sub launch_shell {
382
383         my $self = shift;
384
385         my $pid = OpenSRF::Utils::safe_fork();
386
387         if( $pid ) { $self->pid_hash( $pid , _shell() ); }
388         else {
389                 $0 = "System Shell";
390                 for( my $x = 0; $x != 10; $x++ ) {
391                         eval _shell();
392                         if( ! $@ ) { last; }
393                 }
394                 exit;
395         }
396 }
397 =cut
398
399
400 # ----------------------------------------------
401
402 sub pid_hash {
403         my( $self, $pid, $method ) = @_;
404         $self->{'pid_hash'}->{$pid} = $method
405                 if( $pid and $method );
406         return $self->{'pid_hash'};
407 }
408
409 # ----------------------------------------------
410 # If requested, the System can shut down.
411
412 sub killall {
413
414         $SIG{CHLD} = 'IGNORE';
415         $SIG{INT} = 'IGNORE';
416         kill( 'INT', -$$ ); #kill all in process group
417         exit;
418
419 }
420
421 # ----------------------------------------------
422 # Handle $SIG{HUP}
423 sub hupall {
424
425         _log( "HUPping brood" );
426         $SIG{CHLD} = 'IGNORE';
427         $SIG{HUP} = 'IGNORE';
428         set_config(); # reload config
429         kill( 'HUP', -$$ );
430 #       $SIG{CHLD} = \&process_automation;
431         $SIG{HUP} = sub{ instance()->hupall(); };
432 }
433
434
435 # ----------------------------------------------
436 # Log to debug, and stdout
437
438 sub _log {
439         my $string = shift;
440         OpenSRF::Utils::Logger->debug( $string, INFO );
441         print $string . "\n";
442 }
443
444 # ----------------------------------------------
445
446 sub _sleep {
447         select( undef, undef, undef, 0.3 );
448 }
449
450 1;
451
452