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;
35 my $hostname = hostfqdn();
38 'action=s' => \$action,
39 'service=s' => \$service,
40 'config=s' => \$config,
41 'pid_dir=s' => \$pid_dir,
42 'no_daemon' => \$no_daemon,
46 my $pid_file = "$pid_dir/$service.pid" if $pid_dir and $service;
48 # stop a specific service
51 my $pid = `cat $pid_file`;
52 msg("Stopping $service parent process $pid");
56 msg("$service not running");
60 # start a specific service
63 OpenSRF::System->bootstrap_client(config_file => $config);
65 die "Unable to bootstrap client for requests\n"
66 unless OpenSRF::Transport::PeerHandle->retrieve;
68 load_settings() if $service eq 'opensrf.settings';
70 my $sclient = OpenSRF::Utils::SettingsClient->new;
71 my $apps = $sclient->config_value("activeapps", "appname");
72 OpenSRF::Transport::PeerHandle->retrieve->disconnect;
75 $apps = [$apps] unless ref $apps;
76 for my $app (@$apps) {
77 if($app eq $service) {
78 if($sclient->config_value('apps', $app, 'language') =~ /perl/i) {
79 do_daemon() unless $no_daemon;
82 while(my $pid = waitpid(-1, 0)) {
83 $logger->debug("Cleaning up Perl $service process $pid");
90 msg("$service is not configured to run on $hostname");
95 exit if OpenSRF::Utils::safe_fork();
101 `echo $$ > $pid_file`;
104 # parses the local settings file
106 my $conf = OpenSRF::Utils::Config->current;
107 my $cfile = $conf->bootstrap->settings_config;
108 my $parser = OpenSRF::Utils::SettingsParser->new();
109 $parser->initialize( $cfile );
110 $OpenSRF::Utils::SettingsClient::host_config =
111 $parser->get_server_config($conf->env->hostname);
114 # starts up the unix::server master process
115 sub launch_net_server {
116 push @OpenSRF::UnixServer::ISA, 'Net::Server::PreFork';
117 unless(OpenSRF::Utils::safe_fork()) {
118 $0 = "OpenSRF App ($service)";
119 OpenSRF::UnixServer->new($service)->serve;
125 # starts up the inbound listener process
126 sub launch_listener {
127 unless(OpenSRF::Utils::safe_fork()) {
128 $0 = "OpenSRF listener [$service]";
129 OpenSRF::Transport::Listener->new($service)->initialize->listen;
141 # XXX add more comments
144 Usage: perl $0 --pid_dir /var/run/opensrf --config /etc/opensrf/opensrf_core.xml --service opensrf.settings --action start
147 Actions include start, stop, restart, and start_all, stop_all, and restart_all
150 Specifies which OpenSRF service to control
153 OpenSRF configuration file
156 Directory where process-specific PID files are kept
159 Do not detach and run as a daemon process. Useful for debugging.
162 Print this help message
168 do_help() if $help or not $action;
169 do_start() if $action eq 'start';
170 do_stop() if $action eq 'stop';
171 do_stop() and do_start() if $action eq 'restart';