2 # ---------------------------------------------------------------
3 # Copyright (C) 2008 Georgia Public Library Service
4 # Bill Erickson <erickson@esilibrary.com>
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.
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;
18 use Net::Domain qw/hostfqdn/;
19 use POSIX qw/setsid :sys_wait_h/;
20 use OpenSRF::Utils::Logger q/$logger/;
22 use OpenSRF::Transport::PeerHandle;
23 use OpenSRF::Utils::SettingsClient;
24 use OpenSRF::Transport::Listener;
26 use OpenSRF::Utils::Config;
28 my $opt_action = undef;
29 my $opt_service = undef;
30 my $opt_config = "@CONF_DIR@/opensrf_core.xml";
31 my $opt_pid_dir = "@PID_DIR@/run/opensrf";
32 my $opt_no_daemon = 0;
33 my $opt_settings_pause = 0;
34 my $opt_localhost = 0;
38 my $hostname = $ENV{OSRF_HOSTNAME} || hostfqdn();
42 'action=s' => \$opt_action,
43 'service=s' => \$opt_service,
44 'config=s' => \$opt_config,
45 'pid-dir=s' => \$opt_pid_dir,
46 'no-daemon' => \$opt_no_daemon,
47 'settings-startup-pause=i' => \$opt_settings_pause,
48 'localhost' => \$opt_localhost,
50 'verbose' => \$verbose,
54 $hostname = 'localhost';
55 $ENV{OSRF_HOSTNAME} = $hostname;
59 kill('INT', -$$); #kill all in process group
63 $SIG{TERM} = \&haltme;
67 return "$opt_pid_dir/$service.pid";
70 # stop a specific service
73 my $pid_file = get_pid_file($service);
75 my $pid = `cat $pid_file`;
77 msg("stopping service pid=$pid $service", 1);
82 msg("$service not running");
88 OpenSRF::System->bootstrap_client(config_file => $opt_config);
89 die "Unable to bootstrap client for requests\n"
90 unless OpenSRF::Transport::PeerHandle->retrieve;
92 load_settings(); # load the settings config if we can
94 my $sclient = OpenSRF::Utils::SettingsClient->new;
95 my $apps = $sclient->config_value("activeapps", "appname");
97 # disconnect the top-level network handle
98 OpenSRF::Transport::PeerHandle->retrieve->disconnect;
101 $apps = [$apps] unless ref $apps;
102 for my $app (@$apps) {
103 if (!$sclient->config_value('apps', $app)) {
104 msg("Service '$app' is listed for this host, but there is no configuration for it in $opt_config");
107 if ($sclient->config_value('apps', $app, 'language') =~ /perl/i) {
108 push(@hosted_services, $app);
115 # start a specific service
119 if(-e get_pid_file($service)) {
120 msg("$service is already running");
124 load_settings() if $service eq 'opensrf.settings';
126 if(grep { $_ eq $service } @hosted_services) {
127 return unless do_daemon($service);
128 OpenSRF::System->run_service($service);
131 msg("$service is not configured to run on $hostname");
136 msg("starting all services for $hostname", 1);
137 if(grep {$_ eq 'opensrf.settings'} @hosted_services) {
138 do_start('opensrf.settings');
139 # in batch mode, give opensrf.settings plenty of time to start
140 # before any non-Perl services try to connect
141 sleep $opt_settings_pause if $opt_settings_pause;
143 for my $service (@hosted_services) {
144 do_start($service) unless $service eq 'opensrf.settings';
150 msg("stopping all services for $hostname", 1);
151 do_stop($_) for @hosted_services;
155 # daemonize us. return true if we're the child, false if parent
157 return 1 if $opt_no_daemon;
159 my $pid_file = get_pid_file($service);
160 #exit if OpenSRF::Utils::safe_fork();
161 return 0 if OpenSRF::Utils::safe_fork();
162 msg("starting service pid=$$ $service", 1);
168 open STDIN, '</dev/null';
169 open STDOUT, '>/dev/null';
170 open STDERR, '>/dev/null';
171 `echo $$ > $pid_file`;
175 # parses the local settings file
177 my $conf = OpenSRF::Utils::Config->current;
178 my $cfile = $conf->bootstrap->settings_config;
179 return unless $cfile;
180 my $parser = OpenSRF::Utils::SettingsParser->new();
181 $parser->initialize( $cfile );
182 $OpenSRF::Utils::SettingsClient::host_config =
183 $parser->get_server_config($conf->env->hostname);
189 print "* $m\n" unless $v and not $verbose;
195 Usage: perl $0 --pid-dir @TMP@ --config @CONF_DIR@/opensrf_core.xml --service opensrf.settings --action start
198 Actions include start, stop, restart, and start_all, stop_all, and restart_all
201 Specifies which OpenSRF service to control
204 OpenSRF configuration file
207 Directory where process-specific PID files are kept
210 Do not detach and run as a daemon process. Useful for debugging.
212 --settings-startup-pause
213 How long to give the opensrf.settings server to start up when running
214 in batch mode (start_all). The purpose is to give plenty of time for
215 the settings server to be up and active before any non-Perl services
219 Force the hostname to be 'localhost', instead of the fully qualified
220 domain name for the machine.
223 Print this help message
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';