Perl pipe reading overhaul : data size header
[OpenSRF.git] / src / perl / lib / OpenSRF / System.pm
1 package OpenSRF::System;
2 use strict; use warnings;
3 use OpenSRF;
4 use base 'OpenSRF';
5 use OpenSRF::Utils::Logger qw($logger);
6 use OpenSRF::Transport::Listener;
7 use OpenSRF::Transport;
8 use OpenSRF::Utils;
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;
15 use OpenSRF::Server;
16
17 my $bootstrap_config_file;
18 sub import {
19         my( $self, $config ) = @_;
20         $bootstrap_config_file = $config;
21 }
22
23 $| = 1;
24
25 sub DESTROY {}
26
27 sub load_bootstrap_config {
28         return if OpenSRF::Utils::Config->current;
29
30     die "Please provide a bootstrap config file to OpenSRF::System\n"
31         unless $bootstrap_config_file;
32
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 }
39
40 # ----------------------------------------------
41 # Bootstraps a single client connection.  
42 # named params are 'config_file' and 'client_name'
43 sub bootstrap_client {
44         my $self = shift;
45
46         my $con = OpenSRF::Transport::PeerHandle->retrieve;
47     return if $con and $con->tcp_connected;
48
49         my %params = @_;
50
51         $bootstrap_config_file = 
52                 $params{config_file} || $bootstrap_config_file;
53
54         my $app = $params{client_name} || "client";
55
56         load_bootstrap_config();
57         OpenSRF::Utils::Logger::set_config();
58         OpenSRF::Transport::PeerHandle->construct($app);
59 }
60
61 sub connected {
62         if (my $con = OpenSRF::Transport::PeerHandle->retrieve) {
63                 return 1 if $con->tcp_connected;
64         }
65         return 0;
66 }
67
68 sub run_service {
69     my($class, $service, $pid_dir) = @_;
70
71     $0 = "OpenSRF Listener [$service]";
72
73     # temp connection to use for application initialization
74     OpenSRF::System->bootstrap_client(client_name => "system_client");
75
76     my $sclient = OpenSRF::Utils::SettingsClient->new;
77     my $getval = sub { $sclient->config_value(apps => $service => @_); };
78
79     my $impl = $getval->('implementation');
80
81     OpenSRF::Application::server_class($service);
82     OpenSRF::Application->application_implementation($impl);
83     OpenSRF::Utils::JSON->register_class_hint(name => $impl, hint => $service, type => 'hash');
84     OpenSRF::Application->application_implementation->initialize()
85         if (OpenSRF::Application->application_implementation->can('initialize'));
86
87     # kill the temp connection
88     OpenSRF::Transport::PeerHandle->retrieve->disconnect;
89     
90     # if this service does not want stderr output, it will be redirected to /dev/null
91     my $disable_stderr = $getval->('disable_stderr') || '';
92     my $stderr_path = ($disable_stderr =~ /true/i) ? undef : $sclient->config_value(dirs => 'log');
93
94     my $server = OpenSRF::Server->new(
95         $service,
96         keepalive => $getval->('keepalive') || 5,
97         max_requests =>  $getval->(unix_config => 'max_requests') || 10000,
98         max_children =>  $getval->(unix_config => 'max_children') || 20,
99         min_children =>  $getval->(unix_config => 'min_children') || 1,
100         min_spare_children =>  $getval->(unix_config => 'min_spare_children'),
101         max_spare_children =>  $getval->(unix_config => 'max_spare_children'),
102         stderr_log_path => $stderr_path
103     );
104
105     while(1) {
106         eval { $server->run; };
107         # we only arrive here if the server died a painful death
108         $logger->error("server: died with error $@");
109         $server->cleanup(1);
110         $logger->info("server: restarting after fatal crash...");
111         sleep 2;
112     }
113 }
114
115
116 1;