1 package OpenSRF::System;
2 use strict; use warnings;
5 use OpenSRF::Utils::Logger qw($logger);
6 use OpenSRF::Transport::Listener;
7 use OpenSRF::Transport;
9 use OpenSRF::EX qw/:try/;
10 use POSIX qw/setsid :sys_wait_h/;
11 use OpenSRF::Utils::Config;
12 use OpenSRF::Utils::SettingsParser;
13 use OpenSRF::Utils::SettingsClient;
14 use OpenSRF::Application;
17 my $bootstrap_config_file;
19 my( $self, $config ) = @_;
20 $bootstrap_config_file = $config;
27 sub load_bootstrap_config {
28 return if OpenSRF::Utils::Config->current;
30 die "Please provide a bootstrap config file to OpenSRF::System\n"
31 unless $bootstrap_config_file;
33 OpenSRF::Utils::Config->load(config_file => $bootstrap_config_file);
34 OpenSRF::Utils::JSON->register_class_hint(name => "OpenSRF::Application", hint => "method", type => "hash");
35 OpenSRF::Transport->message_envelope("OpenSRF::Transport::SlimJabber::MessageWrapper");
36 OpenSRF::Transport::PeerHandle->set_peer_client("OpenSRF::Transport::SlimJabber::PeerConnection");
37 OpenSRF::Application->server_class('client');
38 # Read in a shared portion of the config file
39 # for later use in log parameter redaction
40 $OpenSRF::Application::shared_conf = OpenSRF::Utils::Config->load(
41 'config_file' => OpenSRF::Utils::Config->current->FILE,
44 'base_path' => '/config/shared'
48 # ----------------------------------------------
49 # Bootstraps a single client connection.
50 # named params are 'config_file' and 'client_name'
51 sub bootstrap_client {
54 my $con = OpenSRF::Transport::PeerHandle->retrieve;
55 return if $con and $con->tcp_connected;
59 $bootstrap_config_file =
60 $params{config_file} || $bootstrap_config_file;
62 my $app = $params{client_name} || "client";
64 load_bootstrap_config();
65 OpenSRF::Utils::Logger::set_config();
66 OpenSRF::Transport::PeerHandle->construct($app);
70 if (my $con = OpenSRF::Transport::PeerHandle->retrieve) {
71 return 1 if $con->tcp_connected;
77 my($class, $service, $pid_dir) = @_;
79 $0 = "OpenSRF Listener [$service]";
81 # temp connection to use for application initialization
82 OpenSRF::System->bootstrap_client(client_name => "system_client");
84 my $sclient = OpenSRF::Utils::SettingsClient->new;
85 my $getval = sub { $sclient->config_value(apps => $service => @_); };
87 my $impl = $getval->('implementation');
89 OpenSRF::Application::server_class($service);
90 OpenSRF::Application->application_implementation($impl);
91 OpenSRF::Utils::JSON->register_class_hint(name => $impl, hint => $service, type => 'hash');
92 OpenSRF::Application->application_implementation->initialize()
93 if (OpenSRF::Application->application_implementation->can('initialize'));
95 # kill the temp connection
96 OpenSRF::Transport::PeerHandle->retrieve->disconnect;
98 # if this service does not want stderr output, it will be redirected to /dev/null
99 my $disable_stderr = $getval->('disable_stderr') || '';
100 my $stderr_path = ($disable_stderr =~ /true/i) ? undef : $sclient->config_value(dirs => 'log');
102 my $server = OpenSRF::Server->new(
104 keepalive => $getval->('keepalive') || 5,
105 max_requests => $getval->(unix_config => 'max_requests') || 10000,
106 max_children => $getval->(unix_config => 'max_children') || 20,
107 min_children => $getval->(unix_config => 'min_children') || 1,
108 min_spare_children => $getval->(unix_config => 'min_spare_children'),
109 max_spare_children => $getval->(unix_config => 'max_spare_children'),
110 stderr_log_path => $stderr_path
114 eval { $server->run; };
115 # we only arrive here if the server died a painful death
116 $logger->error("server: died with error $@");
118 $logger->info("server: restarting after fatal crash...");