1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
|
package FCGI::Engine::Manager;
use Moose;
use Class::Load ();
use FCGI::Engine::Types;
use FCGI::Engine::Manager::Server;
use Config::Any;
our $VERSION = '0.22';
our $AUTHORITY = 'cpan:STEVAN';
with 'MooseX::Getopt';
has 'conf' => (
is => 'ro',
isa => 'Path::Class::File',
coerce => 1,
required => 1,
);
has '_config' => (
is => 'ro',
isa => 'FCGI::Engine::Manager::Config',
lazy => 1,
default => sub {
my $self = shift;
my $file = $self->conf->stringify;
my $config = Config::Any->load_files({
files => [ $file ],
use_ext => 1
})->[0]->{$file};
#use Data::Dumper;
#warn Dumper $config;
return $config;
}
);
has '_servers' => (
reader => 'servers',
isa => 'ArrayRef[FCGI::Engine::Manager::Server]',
lazy => 1,
default => sub {
my $self = shift;
return [
map {
$_->{server_class} ||= "FCGI::Engine::Manager::Server";
Class::Load::load_class($_->{server_class});
$_->{server_class}->new(%$_);
} @{$self->_config}
];
},
);
sub log { shift; print @_, "\n" }
sub start {
my $self = shift;
local $| = 1;
$self->log("Starting up the FCGI servers ...");
my @servers = (@_ && defined $_[0]) ? $self->_find_server_by_name( @_ ) : @{ $self->servers };
foreach my $server ( @servers ) {
if (-e $server->pidfile) {
my $pid = $server->pid_obj;
if ($pid->is_running) {
$self->log("Pid " . $pid->pid . " is already running");
return;
}
$server->remove_pid_obj;
}
my @cli = $server->construct_command_line();
$self->log("Running @cli");
unless (system(@cli) == 0) {
$self->log("Could not execute command (@cli) exited with status $?");
return;
}
my $count = 1;
until (-e $server->pidfile) {
$self->log("pidfile (" . $server->pidfile . ") does not exist yet ... (trying $count times)");
sleep 2;
$count++;
}
my $pid = $server->pid_obj;
while (!$pid->is_running) {
$self->log("pid (" . $pid->pid . ") with pid_file (" . $server->pidfile . ") is not running yet, sleeping ...");
sleep 2;
}
$self->log("Pid " . $pid->pid . " is running");
}
$self->log("... FCGI servers have been started");
}
sub status {
my $self = shift;
my @servers = (@_ && defined $_[0]) ? $self->_find_server_by_name( @_ ) : @{ $self->servers };
my $status = '';
foreach my $server ( @servers ) {
$status .= $server->name;
if (! -f $server->pidfile ) {
$status .= " is not running\n";
next;
}
my $pid = $server->pid_obj;
$status .= $pid->is_running ? " is running\n" : " is not running\n"
}
return $status;
}
sub stop {
my $self = shift;
local $| = 1;
$self->log("Killing the FCGI servers ...");
my @servers = (@_ && defined $_[0]) ? $self->_find_server_by_name( @_ ) : @{ $self->servers };
foreach my $server ( @servers ) {
if (-f $server->pidfile) {
my $pid = $server->pid_obj;
$self->log("Killing PID " . $pid->pid . " from $$ ");
kill TERM => $pid->pid;
while ($pid->is_running) {
$self->log("pid (" . $server->pidfile . ") is still running, sleeping ...");
sleep 1;
}
$server->pid_obj->remove;
$server->remove_pid_obj;
}
if (-e $server->socket) {
unlink($server->socket);
}
}
$self->log("... FCGI servers have been killed");
}
sub restart {
my $self = shift;
$self->stop( @_ );
sleep( 2 ); # give stop() some time
$self->start( @_ );
}
sub graceful {
my $self = shift;
my @servers = (@_ && defined $_[0]) ? $self->_find_server_by_name( @_ ) : @{ $self->servers };
my @pids;
foreach my $server ( @servers ) {
push @pids, $server->pid_obj->pid;
unlink($server->pidfile);
$server->remove_pid_obj;
}
$self->start( @_ );
foreach my $pid ( @pids ) {
$self->log("... Killing old fcgi process $pid");
kill TERM => $pid;
}
foreach my $server ( @servers ) {
while (-f $server->pidfile) {
$self->log("pid (" . $server->pidfile . ") has not been removed, sleeping ...");
sleep 1;
}
$server->pid_obj->write;
}
}
sub _find_server_by_name {
my( $self, @names ) = @_;
my %wanted = map { $_ => 1 } @names;
my @servers = grep { exists $wanted{ $_->name } } @{ $self->servers };
return @servers;
}
1;
__END__
=pod
=head1 NAME
FCGI::Engine::Manager - Manage multiple FCGI::Engine instances
=head1 SYNOPSIS
#!/usr/bin/perl
my $m = FCGI::Engine::Manager->new(
conf => 'conf/my_app_conf.yml'
);
my ($command, $server_name) = @ARGV;
$m->start($server_name) if $command eq 'start';
$m->stop($server_name) if $command eq 'stop';
$m->restart($server_name) if $command eq 'restart';
$m->graceful($server_name) if $command eq 'graceful';
print $m->status($server_name) if $command eq 'status';
# on the command line
perl all_my_fcgi_backends.pl start
perl all_my_fcgi_backends.pl stop
perl all_my_fcgi_backends.pl restart foo.server
# etc ...
=head1 DESCRIPTION
This module handles multiple L<FCGI::Engine> instances for you, it can
start, stop and provide basic status info. It is configurable using
L<Config::Any>, but only really the YAML format has been tested.
=head2 Use with Catalyst
Since L<FCGI::Engine> is pretty much compatible with
L<Catalyst::Engine::FastCGI>, this module can also be used to manage
your L<Catalyst::Engine::FastCGI> based apps as well as your
L<FCGI::Engine> based apps.
=head2 Use with Plack
L<Plack> support is provided via the L<FCGI::Engine::Manager::Server::Plackup>
module. All that is required is setting the C<server_class> parameter
in the configuarion and it will Just Work.
=head1 EXAMPLE CONFIGURATION
Here is an example configuration in YAML, it should be noted that
the options for each server are basically the constructor params to
L<FCGI::Engine::Manager::Server> and are passed verbatim to it.
This means that if you subclass L<FCGI::Engine::Manager::Server>
and set the C<server_class:> option appropriately, it should pass
any new options you added to your subclass automatically. The third
server in the list shows exactly how this is used with a L<Plack>
application.
---
- name: "foo.server"
server_class: "FCGI::Engine::Manager::Server"
scriptname: "t/scripts/foo.pl"
nproc: 1
pidfile: "/tmp/foo.pid"
socket: "/tmp/foo.socket"
additional_args: [ "-I", "lib/" ]
- name: "bar.server"
scriptname: "t/scripts/bar.pl"
nproc: 1
pidfile: "/tmp/bar.pid"
socket: "/tmp/bar.socket"
- name: "baz.server"
server_class: "FCGI::Engine::Manager::Server::Plackup"
scriptname: "t/scripts/baz.psgi" # the .psgi file
nproc: 1
pidfile: "/tmp/baz.pid"
socket: "/tmp/baz.socket"
additional_args: [ "-E", "production" ] # plackup specific option
=head1 BUGS
All complex software has bugs lurking in it, and this module is no
exception. If you find a bug please either email me, or add the bug
to cpan-RT.
=head1 AUTHOR
Stevan Little E<lt>stevan@iinteractive.comE<gt>
=head1 COPYRIGHT AND LICENSE
Copyright 2007-2010 by Infinity Interactive, Inc.
L<http://www.iinteractive.com>
This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.
=cut
|