1 package OpenSRF::System;
2 use strict; use warnings;
5 use OpenSRF::Utils::Logger qw(:level);
6 use OpenSRF::Transport::Listener;
7 use OpenSRF::Transport;
8 use OpenSRF::UnixServer;
10 use OpenSRF::Utils::LogServer;
12 use OpenSRF::EX qw/:try/;
13 use POSIX ":sys_wait_h";
14 use OpenSRF::Utils::Config;
15 use OpenSRF::Utils::SettingsParser;
16 use OpenSRF::Utils::SettingsClient;
17 use OpenSRF::Application;
18 use Net::Server::PreFork;
21 my $bootstrap_config_file;
23 my( $self, $config ) = @_;
24 $bootstrap_config_file = $config;
27 =head2 Name/Description
31 To start the system: OpenSRF::System->bootstrap();
33 Simple system process management and automation. After instantiating the class, simply call
34 bootstrap() to launch the system. Each launched process is stored as a process-id/method-name
35 pair in a local hash. When we receive a SIG{CHILD}, we loop through this hash and relaunch
36 any child processes that may have terminated.
38 Currently automated processes include launching the internal Unix Servers, launching the inbound
39 connections for each application, and starting the system shell.
42 Note: There should be only one instance of this class
43 alive at any given time. It is designed as a globel process handler and, hence, will cause much
44 oddness if you call the bootstrap() method twice or attempt to create two of these by trickery.
45 There is a single instance of the class created on the first call to new(). This same instance is
46 returned on subsequent calls to new().
54 # ----------------------------------------------
56 $SIG{INT} = sub { instance()->killall(); };
58 $SIG{HUP} = sub{ instance()->hupall(); };
60 #$SIG{CHLD} = \&process_automation;
65 # put $instance in a closure and return it for requests to new()
66 # since there should only be one System instance running
69 sub instance { return __PACKAGE__->new(); }
74 $class = ref( $class ) || $class;
76 $self->{'pid_hash'} = {};
77 bless( $self, $class );
84 # ----------------------------------------------
85 # Commands to execute at system launch
89 return "OpenSRF::UnixServer->new( '$app')->serve()";
94 return "OpenSRF::Transport::Listener->new( '$app' )->initialize()->listen()";
98 # ----------------------------------------------
101 sub load_bootstrap_config {
103 if(OpenSRF::Utils::Config->current) {
107 if(!$bootstrap_config_file) {
108 die "Please provide a bootstrap config file to OpenSRF::System!\n" .
109 "use OpenSRF::System qw(/path/to/bootstrap_config);";
112 OpenSRF::Utils::Config->load( config_file => $bootstrap_config_file );
114 JSON->register_class_hint( name => "OpenSRF::Application", hint => "method", type => "hash" );
116 OpenSRF::Transport->message_envelope( "OpenSRF::Transport::SlimJabber::MessageWrapper" );
117 OpenSRF::Transport::PeerHandle->set_peer_client( "OpenSRF::Transport::SlimJabber::PeerConnection" );
118 OpenSRF::Transport::Listener->set_listener( "OpenSRF::Transport::SlimJabber::Inbound" );
119 OpenSRF::Application->server_class('client');
124 my $self = __PACKAGE__->instance();
125 load_bootstrap_config();
126 OpenSRF::Utils::Logger::set_config();
127 my $bsconfig = OpenSRF::Utils::Config->current;
129 # Start a process group and make me the captain
131 $0 = "OpenSRF System";
133 # -----------------------------------------------
134 # Launch the settings sever if necessary...
135 my $are_settings_server = 0;
136 if( (my $cfile = $bsconfig->bootstrap->settings_config) ) {
137 my $parser = OpenSRF::Utils::SettingsParser->new();
139 # since we're (probably) the settings server, we can go ahead and load the real config file
140 $parser->initialize( $cfile );
141 $OpenSRF::Utils::SettingsClient::host_config =
142 $parser->get_server_config($bsconfig->env->hostname);
144 my $client = OpenSRF::Utils::SettingsClient->new();
145 my $apps = $client->config_value("activeapps", "appname");
146 if(ref($apps) ne "ARRAY") { $apps = [$apps]; }
148 if(!defined($apps) || @$apps == 0) {
149 print "No apps to load, exiting...";
153 for my $app (@$apps) {
154 # verify we are a settings server and launch
155 if( $app eq "opensrf.settings" and
156 $client->config_value("apps","opensrf.settings", "language") =~ /perl/i ) {
158 $are_settings_server = 1;
159 $self->launch_settings();
161 $self->launch_settings_listener();
167 # Launch everything else
168 OpenSRF::System->bootstrap_client(client_name => "system_client");
169 my $client = OpenSRF::Utils::SettingsClient->new();
170 my $apps = $client->config_value("activeapps", "appname" );
171 if(!ref($apps)) { $apps = [$apps]; }
173 if(!defined($apps) || @$apps == 0) {
174 print "No apps to load, exiting...";
178 my $server_type = $client->config_value("server_type");
179 $server_type ||= "basic";
181 my $con = OpenSRF::Transport::PeerHandle->retrieve;
188 if( $server_type eq "prefork" ) {
189 $server_type = "Net::Server::PreFork";
191 $server_type = "Net::Server::Single";
194 _log( " * Server type: $server_type", INTERNAL );
199 throw OpenSRF::EX::PANIC ("Cannot set $server_type: $@" );
202 push @OpenSRF::UnixServer::ISA, $server_type;
204 _log( " * System boostrap" );
206 # --- Boot the Unix servers
207 $self->launch_unix($apps);
213 # --- Boot the listeners
214 $self->launch_listener($apps);
218 _log( " * System is ready..." );
221 my $ps = `ps ax | grep " Open" | grep -v grep | sort -r -k5`;
223 print "\n --- PS --- \n$ps --- PS ---\n\n";
225 while( 1 ) { sleep; }
231 # ----------------------------------------------
232 # Bootstraps a single client connection.
234 # named params are 'config_file' and 'client_name'
236 sub bootstrap_client {
239 my $con = OpenSRF::Transport::PeerHandle->retrieve;
240 if($con and $con->tcp_connected) {
241 warn "PeerHandle is already connected in 'bootstrap_client'... returning\n";
242 _log( "PeerHandle is already connected in 'bootstrap_client'... returning");
248 $bootstrap_config_file =
249 $params{config_file} || $bootstrap_config_file;
251 my $app = $params{client_name} || "client";
254 load_bootstrap_config();
255 OpenSRF::Utils::Logger::set_config();
256 OpenSRF::Transport::PeerHandle->construct( $app );
260 sub bootstrap_logger {
262 OpenSRF::Utils::LogServer->serve();
266 # ----------------------------------------------
267 # Cycle through the known processes, reap the dead child
268 # and put a new child in its place. (MMWWAHAHHAHAAAA!)
270 sub process_automation {
272 my $self = __PACKAGE__->instance();
274 foreach my $pid ( keys %{$self->pid_hash} ) {
276 if( waitpid( $pid, WNOHANG ) == $pid ) {
278 my $method = $self->pid_hash->{$pid};
279 delete $self->pid_hash->{$pid};
281 my $newpid = OpenSRF::Utils::safe_fork();
283 OpenSRF::Utils::Logger->debug( "Relaunching $method", ERROR );
284 _log( "Relaunching => $method" );
287 $self->pid_hash( $newpid, $method );
289 else { eval $method; exit; }
293 $SIG{CHLD} = \&process_automation;
298 sub launch_settings {
300 # XXX the $self like this and pid automation will not work with this setup....
302 @OpenSRF::UnixServer::ISA = qw(OpenSRF Net::Server::PreFork);
304 my $pid = OpenSRF::Utils::safe_fork();
306 $self->pid_hash( $pid , "launch_settings()" );
309 my $apname = "opensrf.settings";
310 #$0 = "OpenSRF App [$apname]";
311 eval _unixserver( $apname );
312 if($@) { die "$@\n"; }
316 @OpenSRF::UnixServer::ISA = qw(OpenSRF);
321 sub launch_settings_listener {
324 my $app = "opensrf.settings";
325 my $pid = OpenSRF::Utils::safe_fork();
327 $self->pid_hash( $pid , _listener( $app ) );
331 $0 = "OpenSRF listener [$apname]";
332 eval _listener( $app );
338 # ----------------------------------------------
339 # Launch the Unix Servers
342 my( $self, $apps ) = @_;
344 my $client = OpenSRF::Utils::SettingsClient->new();
346 foreach my $app ( @$apps ) {
349 my $lang = $client->config_value( "apps", $app, "language");
350 next unless $lang =~ /perl/i;
351 next if $app eq "opensrf.settings";
353 _log( " * Starting UnixServer for $app..." );
355 my $pid = OpenSRF::Utils::safe_fork();
357 $self->pid_hash( $pid , _unixserver( $app ) );
361 $0 = "OpenSRF App ($apname)";
362 eval _unixserver( $app );
368 # ----------------------------------------------
369 # Launch the inbound clients
371 sub launch_listener {
373 my( $self, $apps ) = @_;
374 my $client = OpenSRF::Utils::SettingsClient->new();
376 foreach my $app ( @$apps ) {
379 my $lang = $client->config_value( "apps", $app, "language");
380 next unless $lang =~ /perl/i;
381 next if $app eq "opensrf.settings";
383 _log( " * Starting Listener for $app..." );
385 my $pid = OpenSRF::Utils::safe_fork();
387 $self->pid_hash( $pid , _listener( $app ) );
391 $0 = "OpenSRF listener [$apname]";
392 eval _listener( $app );
398 # ----------------------------------------------
405 my $pid = OpenSRF::Utils::safe_fork();
407 if( $pid ) { $self->pid_hash( $pid , _shell() ); }
410 for( my $x = 0; $x != 10; $x++ ) {
420 # ----------------------------------------------
423 my( $self, $pid, $method ) = @_;
424 $self->{'pid_hash'}->{$pid} = $method
425 if( $pid and $method );
426 return $self->{'pid_hash'};
429 # ----------------------------------------------
430 # If requested, the System can shut down.
434 $SIG{CHLD} = 'IGNORE';
435 $SIG{INT} = 'IGNORE';
436 kill( 'INT', -$$ ); #kill all in process group
441 # ----------------------------------------------
445 _log( "HUPping brood" );
446 $SIG{CHLD} = 'IGNORE';
447 $SIG{HUP} = 'IGNORE';
448 set_config(); # reload config
450 # $SIG{CHLD} = \&process_automation;
451 $SIG{HUP} = sub{ instance()->hupall(); };
455 # ----------------------------------------------
456 # Log to debug, and stdout
460 OpenSRF::Utils::Logger->debug( $string, INFO );
461 #print $string . "\n";
464 # ----------------------------------------------
467 select( undef, undef, undef, 0.3 );