]> git.evergreen-ils.org Git - OpenSRF.git/blob - bin/opensrf-perl.pl
added a configurable startup pause delay. after opensrf.settings has been started...
[OpenSRF.git] / bin / opensrf-perl.pl
1 #!/usr/bin/perl
2 # ---------------------------------------------------------------
3 # Copyright (C) 2008  Georgia Public Library Service
4 # Bill Erickson <erickson@esilibrary.com>
5 #
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License
8 # as published by the Free Software Foundation; either version 2
9 # of the License, or (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15 # ---------------------------------------------------------------
16 use strict; use warnings;
17 use Getopt::Long;
18 use Net::Domain qw/hostfqdn/;
19 use POSIX qw/setsid :sys_wait_h/;
20 use OpenSRF::Utils::Logger q/$logger/;
21 use OpenSRF::System;
22 use OpenSRF::Transport::PeerHandle;
23 use OpenSRF::Utils::SettingsClient;
24 use OpenSRF::Transport::Listener;
25 use OpenSRF::Utils;
26 use OpenSRF::Utils::Config;
27
28 my $opt_action = undef;
29 my $opt_service = undef;
30 my $opt_config = undef;
31 my $opt_pid_dir = '/tmp';
32 my $opt_no_daemon = 0;
33 my $opt_settings_pause = 0;
34 my $opt_help = 0;
35 my $sclient;
36 my $hostname = hostfqdn();
37 my @hosted_services;
38
39 GetOptions(
40     'action=s' => \$opt_action,
41     'service=s' => \$opt_service,
42     'config=s' => \$opt_config,
43     'pid-dir=s' => \$opt_pid_dir,
44     'no-daemon' => \$opt_no_daemon,
45     'settings-startup-pause=i' => \$opt_settings_pause,
46     'help' => \$opt_help,
47 );
48
49
50 sub haltme {
51     kill('INT', -$$); #kill all in process group
52     exit;
53 };
54 $SIG{INT} = \&haltme;
55 $SIG{TERM} = \&haltme;
56
57 sub get_pid_file {
58     my $service = shift;
59     return "$opt_pid_dir/$service.pid";
60 }
61
62 # stop a specific service
63 sub do_stop {
64     my $service = shift;
65     my $pid_file = get_pid_file($service);
66     if(-e $pid_file) {
67         my $pid = `cat $pid_file`;
68         kill('INT', $pid);
69         waitpid($pid, 0);
70         unlink $pid_file;
71     } else {
72         msg("$service not running");
73     }
74     return 1;
75 }
76
77 sub do_init {
78     OpenSRF::System->bootstrap_client(config_file => $opt_config);
79     die "Unable to bootstrap client for requests\n"
80         unless OpenSRF::Transport::PeerHandle->retrieve;
81
82     load_settings(); # load the settings config if we can
83
84     my $sclient = OpenSRF::Utils::SettingsClient->new;
85     my $apps = $sclient->config_value("activeapps", "appname");
86
87     # disconnect the top-level network handle
88     OpenSRF::Transport::PeerHandle->retrieve->disconnect;
89
90     if($apps) {
91         $apps = [$apps] unless ref $apps;
92         for my $app (@$apps) {
93             push(@hosted_services, $app) 
94                 if $sclient->config_value('apps', $app, 'language') =~ /perl/i;
95         }
96     }
97     return 1;
98 }
99
100 # start a specific service
101 sub do_start {
102     my $service = shift;
103     load_settings() if $service eq 'opensrf.settings';
104
105     my $sclient = OpenSRF::Utils::SettingsClient->new;
106     my $apps = $sclient->config_value("activeapps", "appname");
107     OpenSRF::Transport::PeerHandle->retrieve->disconnect;
108
109     if(grep { $_ eq $service } @hosted_services) {
110         return unless do_daemon($service);
111         launch_net_server($service);
112         launch_listener($service);
113         $0 = "OpenSRF controller [$service]";
114         while(my $pid = waitpid(-1, 0)) {
115             $logger->debug("Cleaning up Perl $service process $pid");
116         }
117     }
118
119     msg("$service is not configured to run on $hostname");
120     return 1;
121 }
122
123 sub do_start_all {
124     if(grep {$_ eq 'opensrf.settings'} @hosted_services) {
125         do_start('opensrf.settings');
126         # in batch mode, give opensrf.settings plenty of time to start 
127         # before any non-Perl services try to connect
128         sleep $opt_settings_pause if $opt_settings_pause;
129     }
130     for my $service (@hosted_services) {
131         do_start($service) unless $service eq 'opensrf.settings';
132     }
133     return 1;
134 }
135
136 sub do_stop_all {
137     do_stop($_) for @hosted_services;
138     return 1;
139 }
140
141 # daemonize us.  return true if we're the child, false if parent
142 sub do_daemon {
143     return 1 if $opt_no_daemon;
144     my $service = shift;
145     my $pid_file = get_pid_file($service);
146     #exit if OpenSRF::Utils::safe_fork();
147     return 0 if OpenSRF::Utils::safe_fork();
148     chdir('/');
149     setsid();
150     close STDIN;
151     close STDOUT;
152     close STDERR;
153     `echo $$ > $pid_file`;
154     return 1;
155 }
156
157 # parses the local settings file
158 sub load_settings {
159     my $conf = OpenSRF::Utils::Config->current;
160     my $cfile = $conf->bootstrap->settings_config;
161     return unless $cfile;
162     my $parser = OpenSRF::Utils::SettingsParser->new();
163     $parser->initialize( $cfile );
164     $OpenSRF::Utils::SettingsClient::host_config =
165         $parser->get_server_config($conf->env->hostname);
166 }
167
168 # starts up the unix::server master process
169 sub launch_net_server {
170     my $service = shift;
171     push @OpenSRF::UnixServer::ISA, 'Net::Server::PreFork';
172     unless(OpenSRF::Utils::safe_fork()) {
173         $0 = "OpenSRF Drone [$service]";
174         OpenSRF::UnixServer->new($service)->serve;
175         exit;
176     }
177     return 1;
178 }
179
180 # starts up the inbound listener process
181 sub launch_listener {
182     my $service = shift;
183     unless(OpenSRF::Utils::safe_fork()) {
184         $0 = "OpenSRF listener [$service]";
185         OpenSRF::Transport::Listener->new($service)->initialize->listen;
186         exit;
187     }
188     return 1;
189 }
190
191 sub msg {
192     my $m = shift;
193     print "* $m\n";
194 }
195
196 sub do_help {
197     print <<HELP;
198
199     Usage: perl $0 --pid_dir /var/run/opensrf --config /etc/opensrf/opensrf_core.xml --service opensrf.settings --action start
200
201     --action <action>
202         Actions include start, stop, restart, and start_all, stop_all, and restart_all
203
204     --service <service>
205         Specifies which OpenSRF service to control
206
207     --config <file>
208         OpenSRF configuration file 
209         
210     --pid-dir <dir>
211         Directory where process-specific PID files are kept
212         
213     --no-daemon
214         Do not detach and run as a daemon process.  Useful for debugging.
215
216     --settings-startup-pause
217         How long to give the opensrf.settings server to start up when running 
218         in batch mode (start_all).  The purpose is to give plenty of time for
219         the settings server to be up and active before any non-Perl services
220         attempt to connect.
221         
222     --help
223         Print this help message
224 HELP
225 exit;
226 }
227
228
229 do_help() if $opt_help or not $opt_action;
230 do_init() and do_start($opt_service) if $opt_action eq 'start';
231 do_stop($opt_service) if $opt_action eq 'stop';
232 do_init() and do_stop($opt_service) and do_start($opt_service) if $opt_action eq 'restart';
233 do_init() and do_start_all() if $opt_action eq 'start_all';
234 do_init() and do_stop_all() if $opt_action eq 'stop_all';
235 do_init() and do_stop_all() and do_start_all() if $opt_action eq 'restart_all';
236
237