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 = undef;
31 my $opt_pid_dir = '/tmp';
32 my $opt_no_daemon = 0;
35 my $hostname = hostfqdn();
39 'action=s' => \$opt_action,
40 'service=s' => \$opt_service,
41 'config=s' => \$opt_config,
42 'pid_dir=s' => \$opt_pid_dir,
43 'no_daemon' => \$opt_no_daemon,
49 kill('INT', -$$); #kill all in process group
53 $SIG{TERM} = \&haltme;
57 return "$opt_pid_dir/$service.pid";
60 # stop a specific service
63 my $pid_file = get_pid_file($service);
65 my $pid = `cat $pid_file`;
70 msg("$service not running");
75 OpenSRF::System->bootstrap_client(config_file => $opt_config);
76 die "Unable to bootstrap client for requests\n"
77 unless OpenSRF::Transport::PeerHandle->retrieve;
79 load_settings(); # load the settings config if we can
81 my $sclient = OpenSRF::Utils::SettingsClient->new;
82 my $apps = $sclient->config_value("activeapps", "appname");
84 # disconnect the top-level network handle
85 OpenSRF::Transport::PeerHandle->retrieve->disconnect;
88 $apps = [$apps] unless ref $apps;
89 for my $app (@$apps) {
90 if($app eq $service) {
91 push(@hosted_services, $app)
92 if $sclient->config_value('apps', $app, 'language') =~ /perl/i);
98 # start a specific service
101 load_settings() if $service eq 'opensrf.settings';
103 my $sclient = OpenSRF::Utils::SettingsClient->new;
104 my $apps = $sclient->config_value("activeapps", "appname");
105 OpenSRF::Transport::PeerHandle->retrieve->disconnect;
107 if(grep { $_ eq $service } @hosted_services) {
108 do_daemon($service) unless $opt_no_daemon;
109 launch_net_server($service);
110 launch_listener($service);
111 $0 = "OpenSRF controller [$service]";
112 while(my $pid = waitpid(-1, 0)) {
113 $logger->debug("Cleaning up Perl $service process $pid");
117 msg("$service is not configured to run on $hostname");
121 do_start('opensrf.settings') if grep {$_ eq 'opensrf.settings'} @hosted_services);
122 for my $service (@hosted_services) {
123 do_start($service) unless $service eq 'opensrf.settings';
128 do_stop($_) for @hosted_services;
134 my $pid_file = get_pid_file($service);
135 exit if OpenSRF::Utils::safe_fork();
141 `echo $$ > $pid_file`;
144 # parses the local settings file
146 my $conf = OpenSRF::Utils::Config->current;
147 my $cfile = $conf->bootstrap->settings_config;
148 return unless $cfile;
149 my $parser = OpenSRF::Utils::SettingsParser->new();
150 $parser->initialize( $cfile );
151 $OpenSRF::Utils::SettingsClient::host_config =
152 $parser->get_server_config($conf->env->hostname);
155 # starts up the unix::server master process
156 sub launch_net_server {
158 push @OpenSRF::UnixServer::ISA, 'Net::Server::PreFork';
159 unless(OpenSRF::Utils::safe_fork()) {
160 $0 = "OpenSRF Drone [$service]";
161 OpenSRF::UnixServer->new($service)->serve;
167 # starts up the inbound listener process
168 sub launch_listener {
170 unless(OpenSRF::Utils::safe_fork()) {
171 $0 = "OpenSRF listener [$service]";
172 OpenSRF::Transport::Listener->new($service)->initialize->listen;
186 Usage: perl $0 --pid_dir /var/run/opensrf --config /etc/opensrf/opensrf_core.xml --service opensrf.settings --action start
189 Actions include start, stop, restart, and start_all, stop_all, and restart_all
192 Specifies which OpenSRF service to control
195 OpenSRF configuration file
198 Directory where process-specific PID files are kept
201 Do not detach and run as a daemon process. Useful for debugging.
204 Print this help message
210 do_help() if $opt_help or not $opt_action;
211 do_init() and do_start($opt_service) if $opt_action eq 'start';
212 do_stop($opt_service) if $opt_action eq 'stop';
213 do_init() and do_stop($opt_service) and do_start($opt_service) if $opt_action eq 'restart';
214 do_init() and do_start_all() if $opt_action eq 'start_all';
215 do_stop_all() if $opt_action eq 'stop_all';
216 do_init() and do_stop_all() and do_start_all() if $opt_action eq 'restart_all';