| 1 | package WebDAO::FCGI::ProcManager; | ||
| 2 | |||
| 3 | # Copyright (c) 2000, FundsXpress Financial Network, Inc. | ||
| 4 | # This library is free software released under the GNU Lesser General | ||
| 5 | # Public License, Version 2.1. Please read the important licensing and | ||
| 6 | # disclaimer information included below. | ||
| 7 | |||
| 8 | # $Id$ | ||
| 9 | |||
| 10 | use strict; | ||
| 11 | use Exporter; | ||
| 12 | use POSIX qw(:signal_h); | ||
| 13 | |||
| 14 | use vars qw($VERSION @ISA @EXPORT_OK %EXPORT_TAGS $Q $SIG_CODEREF); | ||
| 15 | use warnings; | ||
| 16 | |||
| 17 | BEGIN { | ||
| 18 | $VERSION = '0.17'; | ||
| 19 | @ISA = qw(Exporter); | ||
| 20 | @EXPORT_OK = qw(pm_manage pm_die pm_wait | ||
| 21 | pm_write_pid_file pm_remove_pid_file | ||
| 22 | pm_pre_dispatch pm_post_dispatch | ||
| 23 | pm_change_process_name pm_received_signal pm_parameter | ||
| 24 | pm_warn pm_notify pm_abort pm_exit | ||
| 25 | $SIG_CODEREF); | ||
| 26 | $EXPORT_TAGS{all} = \@EXPORT_OK; | ||
| 27 | $WebDAO::FCGI::ProcManager::Default = 'WebDAO::FCGI::ProcManager'; | ||
| 28 | } | ||
| 29 | |||
| 30 | =head1 NAME | ||
| 31 | |||
| 32 | FCGI::ProcManager - functions for managing FastCGI applications. | ||
| 33 | |||
| 34 | =head1 SYNOPSIS | ||
| 35 | |||
| 36 | { | ||
| 37 | # In Object-oriented style. | ||
| 38 | use CGI::Fast; | ||
| 39 | use FCGI::ProcManager; | ||
| 40 | my $proc_manager = FCGI::ProcManager->new({ | ||
| 41 | n_processes => 10 | ||
| 42 | }); | ||
| 43 | $proc_manager->pm_manage(); | ||
| 44 | while (my $cgi = CGI::Fast->new()) { | ||
| 45 | $proc_manager->pm_pre_dispatch(); | ||
| 46 | # ... handle the request here ... | ||
| 47 | $proc_manager->pm_post_dispatch(); | ||
| 48 | } | ||
| 49 | |||
| 50 | # This style is also supported: | ||
| 51 | use CGI::Fast; | ||
| 52 | use FCGI::ProcManager qw(pm_manage pm_pre_dispatch | ||
| 53 | pm_post_dispatch); | ||
| 54 | pm_manage( n_processes => 10 ); | ||
| 55 | while (my $cgi = CGI::Fast->new()) { | ||
| 56 | pm_pre_dispatch(); | ||
| 57 | #... | ||
| 58 | pm_post_dispatch(); | ||
| 59 | } | ||
| 60 | |||
| 61 | =head1 DESCRIPTION | ||
| 62 | |||
| 63 | FCGI::ProcManager is used to serve as a FastCGI process manager. By | ||
| 64 | re-implementing it in perl, developers can more finely tune performance in | ||
| 65 | their web applications, and can take advantage of copy-on-write semantics | ||
| 66 | prevalent in UNIX kernel process management. The process manager should | ||
| 67 | be invoked before the caller''s request loop | ||
| 68 | |||
| 69 | The primary routine, C<pm_manage>, enters a loop in which it maintains a | ||
| 70 | number of FastCGI servers (via fork(2)), and which reaps those servers | ||
| 71 | when they die (via wait(2)). | ||
| 72 | |||
| 73 | C<pm_manage> provides too hooks: | ||
| 74 | |||
| 75 | C<managing_init> - called just before the manager enters the manager loop. | ||
| 76 | C<handling_init> - called just before a server is returns from C<pm_manage> | ||
| 77 | |||
| 78 | It is necessary for the caller, when implementing its request loop, to | ||
| 79 | insert a call to C<pm_pre_dispatch> at the top of the loop, and then | ||
| 80 | 7C<pm_post_dispatch> at the end of the loop. | ||
| 81 | |||
| 82 | =head2 Signal Handling | ||
| 83 | |||
| 84 | FCGI::ProcManager attempts to do the right thing for proper shutdowns now. | ||
| 85 | |||
| 86 | When it receives a SIGHUP, it sends a SIGTERM to each of its children, and | ||
| 87 | then resumes its normal operations. | ||
| 88 | |||
| 89 | When it receives a SIGTERM, it sends a SIGTERM to each of its children, sets | ||
| 90 | an alarm(3) "die timeout" handler, and waits for each of its children to | ||
| 91 | die. If all children die before this timeout, process manager exits with | ||
| 92 | return status 0. If all children do not die by the time the "die timeout" | ||
| 93 | occurs, the process manager sends a SIGKILL to each of the remaining | ||
| 94 | children, and exists with return status 1. | ||
| 95 | |||
| 96 | In order to get FastCGI servers to exit upon receiving a signal, it is | ||
| 97 | necessary to use its FAIL_ACCEPT_ON_INTR. See FCGI.pm's description of | ||
| 98 | FAIL_ACCEPT_ON_INTR. Unfortunately, if you want/need to use CGI::Fast, it | ||
| 99 | appears currently necessary to modify your installation of FCGI.pm, with | ||
| 100 | something like the following: | ||
| 101 | |||
| 102 | -*- patch -*- | ||
| 103 | --- FCGI.pm 2001/03/09 01:44:00 1.1.1.3 | ||
| 104 | +++ FCGI.pm 2001/03/09 01:47:32 1.2 | ||
| 105 | @@ -24,7 +24,7 @@ | ||
| 106 | *FAIL_ACCEPT_ON_INTR = sub() { 1 }; | ||
| 107 | |||
| 108 | sub Request(;***$$$) { | ||
| 109 | - my @defaults = (\*STDIN, \*STDOUT, \*STDERR, \%ENV, 0, 0); | ||
| 110 | + my @defaults = (\*STDIN, \*STDOUT, \*STDERR, \%ENV, 0, FAIL_ACCEPT_ON_INTR()); | ||
| 111 | splice @defaults,0,@_,@_; | ||
| 112 | RequestX(@defaults); | ||
| 113 | } | ||
| 114 | -*- end patch -*- | ||
| 115 | |||
| 116 | Otherwise, if you don't, there is a loop around accept(2) which prevents | ||
| 117 | os_unix.c OS_Accept() from returning the necessary error when FastCGI | ||
| 118 | servers blocking on accept(2) receive the SIGTERM or SIGHUP. | ||
| 119 | |||
| 120 | FCGI::ProcManager uses POSIX::sigaction() to override the default SA_RESTART | ||
| 121 | policy used for perl's %SIG behavior. Specifically, the process manager | ||
| 122 | never uses SA_RESTART, while the child FastCGI servers turn off SA_RESTART | ||
| 123 | around the accept(2) loop, but re-enstate it otherwise. | ||
| 124 | |||
| 125 | The desired (and implemented) effect is to give a request as big a chance as | ||
| 126 | possible to succeed and to delay their exits until after their request, | ||
| 127 | while allowing the FastCGI servers waiting for new requests to die right | ||
| 128 | away. | ||
| 129 | |||
| 130 | =head1 METHODS | ||
| 131 | |||
| 132 | =head2 new | ||
| 133 | |||
| 134 | class or instance | ||
| 135 | (ProcManager) new([hash parameters]) | ||
| 136 | |||
| 137 | Constructs a new process manager. Takes an option has of initial parameter | ||
| 138 | values, and assigns these to the constructed object HASH, overriding any | ||
| 139 | default values. The default parameter values currently are: | ||
| 140 | |||
| 141 | role => manager | ||
| 142 | start_delay => 0 | ||
| 143 | die_timeout => 60 | ||
| 144 | |||
| 145 | =cut | ||
| 146 | |||
| 147 | sub new { | ||
| 148 | my ($proto,$init) = @_; | ||
| 149 | $init ||= {}; | ||
| 150 | |||
| 151 | my $this = { | ||
| 152 | role => "manager", | ||
| 153 | start_delay => 0, | ||
| 154 | die_timeout => 60, | ||
| 155 | %$init | ||
| 156 | }; | ||
| 157 | bless $this, ref($proto)||$proto; | ||
| 158 | |||
| 159 | $this->{PIDS} = {}; | ||
| 160 | |||
| 161 | # initialize signal constructions. | ||
| 162 | unless ($this->no_signals()) { | ||
| 163 | $this->{sigaction_no_sa_restart} = | ||
| 164 | POSIX::SigAction->new('WebDAO::FCGI::ProcManager::sig_sub'); | ||
| 165 | $this->{sigaction_sa_restart} = | ||
| 166 | POSIX::SigAction->new('WebDAO::FCGI::ProcManager::sig_sub',undef,POSIX::SA_RESTART); | ||
| 167 | } | ||
| 168 | |||
| 169 | return $this; | ||
| 170 | } | ||
| 171 | |||
| 172 | =head1 Manager methods | ||
| 173 | |||
| 174 | =head2 pm_manage | ||
| 175 | |||
| 176 | instance or export | ||
| 177 | (int) pm_manage([hash parameters]) | ||
| 178 | |||
| 179 | DESCRIPTION: | ||
| 180 | |||
| 181 | When this is called by a FastCGI script to manage application servers. It | ||
| 182 | defines a sequence of instructions for a process to enter this method and | ||
| 183 | begin forking off and managing those handlers, and it defines a sequence of | ||
| 184 | instructions to intialize those handlers. | ||
| 185 | |||
| 186 | If n_processes < 1, the managing section is subverted, and only the | ||
| 187 | handling sequence is executed. | ||
| 188 | |||
| 189 | Either returns the return value of pm_die() and/or pm_abort() (which will | ||
| 190 | not ever return in general), or returns 1 to the calling script to begin | ||
| 191 | handling requests. | ||
| 192 | |||
| 193 | =cut | ||
| 194 | |||
| 195 | sub pm_manage { | ||
| 196 | my ($this,%values) = self_or_default(@_); | ||
| 197 | map { $this->pm_parameter($_,$values{$_}) } keys %values; | ||
| 198 | |||
| 199 | # skip to handling now if we won't be managing any processes. | ||
| 200 | $this->n_processes() or return; | ||
| 201 | |||
| 202 | # call the (possibly overloaded) management initialization hook. | ||
| 203 | $this->role("manager"); | ||
| 204 | $this->managing_init(); | ||
| 205 | $this->pm_notify("initialized"); | ||
| 206 | |||
| 207 | my $manager_pid = $$; | ||
| 208 | |||
| 209 | MANAGING_LOOP: while (1) { | ||
| 210 | |||
| 211 | # if the calling process goes away, perform cleanup. | ||
| 212 | #getppid() == 1 and | ||
| 213 | # return $this->pm_die("calling process has died"); | ||
| 214 | |||
| 215 | $this->n_processes() > 0 or | ||
| 216 | return $this->pm_die(); | ||
| 217 | |||
| 218 | # while we have fewer servers than we want. | ||
| 219 | PIDS: while (keys(%{$this->{PIDS}}) < $this->n_processes()) { | ||
| 220 | |||
| 221 | if (my $pid = fork()) { | ||
| 222 | # the manager remembers the server. | ||
| 223 | $this->{PIDS}->{$pid} = { pid=>$pid }; | ||
| 224 | $this->pm_notify("server (pid $pid) started"); | ||
| 225 | |||
| 226 | } elsif (! defined $pid) { | ||
| 227 | return $this->pm_abort("fork: $!"); | ||
| 228 | |||
| 229 | } else { | ||
| 230 | $this->{MANAGER_PID} = $manager_pid; | ||
| 231 | # the server exits the managing loop. | ||
| 232 | last MANAGING_LOOP; | ||
| 233 | } | ||
| 234 | |||
| 235 | for (my $s = $this->start_delay(); $s; $s = sleep $s) {}; | ||
| 236 | } | ||
| 237 | |||
| 238 | # this should block until the next server dies. | ||
| 239 | $this->pm_wait(); | ||
| 240 | |||
| 241 | }# while 1 | ||
| 242 | |||
| 243 | HANDLING: | ||
| 244 | |||
| 245 | # forget any children we had been collecting. | ||
| 246 | delete $this->{PIDS}; | ||
| 247 | |||
| 248 | # call the (possibly overloaded) handling init hook | ||
| 249 | $this->role("server"); | ||
| 250 | $this->handling_init(); | ||
| 251 | $this->pm_notify("initialized"); | ||
| 252 | |||
| 253 | # server returns | ||
| 254 | return 1; | ||
| 255 | } | ||
| 256 | |||
| 257 | =head2 managing_init | ||
| 258 | |||
| 259 | instance | ||
| 260 | () managing_init() | ||
| 261 | |||
| 262 | DESCRIPTION: | ||
| 263 | |||
| 264 | Overrideable method which initializes a process manager. In order to | ||
| 265 | handle signals, manage the PID file, and change the process name properly, | ||
| 266 | any method which overrides this should call SUPER::managing_init(). | ||
| 267 | |||
| 268 | =cut | ||
| 269 | |||
| 270 | sub managing_init { | ||
| 271 | my ($this) = @_; | ||
| 272 | |||
| 273 | # begin to handle signals. | ||
| 274 | # We do NOT want SA_RESTART in the process manager. | ||
| 275 | # -- we want start the shutdown sequence immediately upon SIGTERM. | ||
| 276 | unless ($this->no_signals()) { | ||
| 277 | sigaction(SIGTERM, $this->{sigaction_no_sa_restart}) or | ||
| 278 | $this->pm_warn("sigaction: SIGTERM: $!"); | ||
| 279 | sigaction(SIGHUP, $this->{sigaction_no_sa_restart}) or | ||
| 280 | $this->pm_warn("sigaction: SIGHUP: $!"); | ||
| 281 | $SIG_CODEREF = sub { $this->sig_manager(@_) }; | ||
| 282 | } | ||
| 283 | |||
| 284 | # change the name of this process as it appears in ps(1) output. | ||
| 285 | $this->pm_change_process_name("webdao-fcgi-pm"); | ||
| 286 | |||
| 287 | $this->pm_write_pid_file(); | ||
| 288 | } | ||
| 289 | |||
| 290 | =head2 pm_die | ||
| 291 | |||
| 292 | instance or export | ||
| 293 | () pm_die(string msg[, int exit_status]) | ||
| 294 | |||
| 295 | DESCRIPTION: | ||
| 296 | |||
| 297 | This method is called when a process manager receives a notification to | ||
| 298 | shut itself down. pm_die() attempts to shutdown the process manager | ||
| 299 | gently, sending a SIGTERM to each managed process, waiting die_timeout() | ||
| 300 | seconds to reap each process, and then exit gracefully once all children | ||
| 301 | are reaped, or to abort if all children are not reaped. | ||
| 302 | |||
| 303 | =cut | ||
| 304 | |||
| 305 | sub pm_die { | ||
| 306 | my ($this,$msg,$n) = self_or_default(@_); | ||
| 307 | |||
| 308 | # stop handling signals. | ||
| 309 | undef $SIG_CODEREF; | ||
| 310 | $SIG{HUP} = 'DEFAULT'; | ||
| 311 | $SIG{TERM} = 'DEFAULT'; | ||
| 312 | |||
| 313 | $this->pm_remove_pid_file(); | ||
| 314 | |||
| 315 | # prepare to die no matter what. | ||
| 316 | if (defined $this->die_timeout()) { | ||
| 317 | $SIG{ALRM} = sub { $this->pm_abort("wait timeout") }; | ||
| 318 | alarm $this->die_timeout(); | ||
| 319 | } | ||
| 320 | |||
| 321 | # send a TERM to each of the servers. | ||
| 322 | if (my @pids = keys %{$this->{PIDS}}) { | ||
| 323 | $this->pm_notify("sending TERM to PIDs, @pids"); | ||
| 324 | kill "TERM", @pids; | ||
| 325 | } | ||
| 326 | |||
| 327 | # wait for the servers to die. | ||
| 328 | while (%{$this->{PIDS}}) { | ||
| 329 | $this->pm_wait(); | ||
| 330 | } | ||
| 331 | |||
| 332 | # die already. | ||
| 333 | $this->pm_exit("dying: ".$msg,$n); | ||
| 334 | } | ||
| 335 | |||
| 336 | =head2 pm_wait | ||
| 337 | |||
| 338 | instance or export | ||
| 339 | (int pid) pm_wait() | ||
| 340 | |||
| 341 | DESCRIPTION: | ||
| 342 | |||
| 343 | This calls wait() which suspends execution until a child has exited. | ||
| 344 | If the process ID returned by wait corresponds to a managed process, | ||
| 345 | pm_notify() is called with the exit status of that process. | ||
| 346 | pm_wait() returns with the return value of wait(). | ||
| 347 | |||
| 348 | =cut | ||
| 349 | |||
| 350 | sub pm_wait { | ||
| 351 | my ($this) = self_or_default(@_); | ||
| 352 | |||
| 353 | # wait for the next server to die. | ||
| 354 | next if (my $pid = wait()) < 0; | ||
| 355 | |||
| 356 | # notify when one of our servers have died. | ||
| 357 | delete $this->{PIDS}->{$pid} and | ||
| 358 | $this->pm_notify("server (pid $pid) exited with status $?"); | ||
| 359 | |||
| 360 | return $pid; | ||
| 361 | } | ||
| 362 | |||
| 363 | =head2 pm_write_pid_file | ||
| 364 | |||
| 365 | instance or export | ||
| 366 | () pm_write_pid_file([string filename]) | ||
| 367 | |||
| 368 | DESCRIPTION: | ||
| 369 | |||
| 370 | Writes current process ID to optionally specified file. If no filename is | ||
| 371 | specified, it uses the value of the C<pid_fname> parameter. | ||
| 372 | |||
| 373 | =cut | ||
| 374 | |||
| 375 | sub pm_write_pid_file { | ||
| 376 | my ($this,$fname) = self_or_default(@_); | ||
| 377 | $fname ||= $this->pid_fname() or return; | ||
| 378 | if (!open PIDFILE, ">$fname") { | ||
| 379 | $this->pm_warn("open: $fname: $!"); | ||
| 380 | return; | ||
| 381 | } | ||
| 382 | print PIDFILE "$$\n"; | ||
| 383 | close PIDFILE; | ||
| 384 | } | ||
| 385 | |||
| 386 | =head2 pm_remove_pid_file | ||
| 387 | |||
| 388 | instance or export | ||
| 389 | () pm_remove_pid_file() | ||
| 390 | |||
| 391 | DESCRIPTION: | ||
| 392 | |||
| 393 | Removes optionally specified file. If no filename is specified, it uses | ||
| 394 | the value of the C<pid_fname> parameter. | ||
| 395 | |||
| 396 | =cut | ||
| 397 | |||
| 398 | sub pm_remove_pid_file { | ||
| 399 | my ($this,$fname) = self_or_default(@_); | ||
| 400 | $fname ||= $this->pid_fname() or return; | ||
| 401 | my $ret = unlink($fname) or $this->pm_warn("unlink: $fname: $!"); | ||
| 402 | return $ret; | ||
| 403 | } | ||
| 404 | |||
| 405 | =head2 sig_sub | ||
| 406 | |||
| 407 | instance | ||
| 408 | () sig_sub(string name) | ||
| 409 | |||
| 410 | DESCRIPTION: | ||
| 411 | |||
| 412 | The name of this method is passed to POSIX::sigaction(), and handles signals | ||
| 413 | for the process manager. If $SIG_CODEREF is set, then the input arguments | ||
| 414 | to this are passed to a call to that. | ||
| 415 | |||
| 416 | =cut | ||
| 417 | |||
| 418 | sub sig_sub { | ||
| 419 | $SIG_CODEREF->(@_) if ref $SIG_CODEREF; | ||
| 420 | } | ||
| 421 | |||
| 422 | =head2 sig_manager | ||
| 423 | |||
| 424 | instance | ||
| 425 | () sig_manager(string name) | ||
| 426 | |||
| 427 | DESCRIPTION: | ||
| 428 | |||
| 429 | Handles signals of the process manager. Takes as input the name of signal | ||
| 430 | being handled. | ||
| 431 | |||
| 432 | =cut | ||
| 433 | |||
| 434 | sub sig_manager { | ||
| 435 | my ($this,$name) = @_; | ||
| 436 | if ($name eq "TERM") { | ||
| 437 | $this->pm_notify("received signal $name"); | ||
| 438 | $this->pm_die("safe exit from signal $name"); | ||
| 439 | } elsif ($name eq "HUP") { | ||
| 440 | # send a TERM to each of the servers, and pretend like nothing happened.. | ||
| 441 | if (my @pids = keys %{$this->{PIDS}}) { | ||
| 442 | $this->pm_notify("sending TERM to PIDs, @pids"); | ||
| 443 | kill "TERM", @pids; | ||
| 444 | } | ||
| 445 | } else { | ||
| 446 | $this->pm_notify("ignoring signal $name"); | ||
| 447 | } | ||
| 448 | } | ||
| 449 | |||
| 450 | =head1 Handler methods | ||
| 451 | |||
| 452 | =head2 handling_init | ||
| 453 | |||
| 454 | instance or export | ||
| 455 | () handling_init() | ||
| 456 | |||
| 457 | DESCRIPTION: | ||
| 458 | |||
| 459 | =cut | ||
| 460 | |||
| 461 | sub handling_init { | ||
| 462 | my ($this) = @_; | ||
| 463 | |||
| 464 | # begin to handle signals. | ||
| 465 | # We'll want accept(2) to return -1(EINTR) on caught signal.. | ||
| 466 | unless ($this->no_signals()) { | ||
| 467 | sigaction(SIGTERM, $this->{sigaction_no_sa_restart}) or $this->pm_warn("sigaction: SIGTERM: $!"); | ||
| 468 | sigaction(SIGHUP, $this->{sigaction_no_sa_restart}) or $this->pm_warn("sigaction: SIGHUP: $!"); | ||
| 469 | $SIG_CODEREF = sub { $this->sig_handler(@_) }; | ||
| 470 | } | ||
| 471 | |||
| 472 | # change the name of this process as it appears in ps(1) output. | ||
| 473 | $this->pm_change_process_name("webdao-fcgi"); | ||
| 474 | } | ||
| 475 | |||
| 476 | =head2 pm_pre_dispatch | ||
| 477 | |||
| 478 | instance or export | ||
| 479 | () pm_pre_dispatch() | ||
| 480 | |||
| 481 | DESCRIPTION: | ||
| 482 | |||
| 483 | =cut | ||
| 484 | |||
| 485 | sub pm_pre_dispatch { | ||
| 486 | my ($this) = self_or_default(@_); | ||
| 487 | |||
| 488 | # Now, we want the request to continue unhindered.. | ||
| 489 | unless ($this->no_signals()) { | ||
| 490 | sigaction(SIGTERM, $this->{sigaction_sa_restart}) or $this->pm_warn("sigaction: SIGTERM: $!"); | ||
| 491 | sigaction(SIGHUP, $this->{sigaction_sa_restart}) or $this->pm_warn("sigaction: SIGHUP: $!"); | ||
| 492 | } | ||
| 493 | } | ||
| 494 | |||
| 495 | =head2 pm_post_dispatch | ||
| 496 | |||
| 497 | instance or export | ||
| 498 | () pm_post_dispatch() | ||
| 499 | |||
| 500 | DESCRIPTION: | ||
| 501 | |||
| 502 | =cut | ||
| 503 | |||
| 504 | sub pm_post_dispatch { | ||
| 505 | my ($this) = self_or_default(@_); | ||
| 506 | if ($this->pm_received_signal("TERM")) { | ||
| 507 | $this->pm_exit("safe exit after SIGTERM"); | ||
| 508 | } | ||
| 509 | if ($this->pm_received_signal("HUP")) { | ||
| 510 | $this->pm_exit("safe exit after SIGHUP"); | ||
| 511 | } | ||
| 512 | if ($this->{MANAGER_PID} and getppid() != $this->{MANAGER_PID}) { | ||
| 513 | $this->pm_exit("safe exit: manager has died"); | ||
| 514 | } | ||
| 515 | # We'll want accept(2) to return -1(EINTR) on caught signal.. | ||
| 516 | unless ($this->no_signals()) { | ||
| 517 | sigaction(SIGTERM, $this->{sigaction_no_sa_restart}) or $this->pm_warn("sigaction: SIGTERM: $!"); | ||
| 518 | sigaction(SIGHUP, $this->{sigaction_no_sa_restart}) or $this->pm_warn("sigaction: SIGHUP: $!"); | ||
| 519 | } | ||
| 520 | } | ||
| 521 | |||
| 522 | =head2 sig_handler | ||
| 523 | |||
| 524 | instance or export | ||
| 525 | () sig_handler() | ||
| 526 | |||
| 527 | DESCRIPTION: | ||
| 528 | |||
| 529 | =cut | ||
| 530 | |||
| 531 | sub sig_handler { | ||
| 532 | my ($this,$name) = @_; | ||
| 533 | $this->pm_received_signal($name,1); | ||
| 534 | } | ||
| 535 | |||
| 536 | =head1 Common methods and routines | ||
| 537 | |||
| 538 | =head2 self_or_default | ||
| 539 | |||
| 540 | private global | ||
| 541 | (ProcManager, @args) self_or_default([ ProcManager, ] @args); | ||
| 542 | |||
| 543 | DESCRIPTION: | ||
| 544 | |||
| 545 | This is a helper subroutine to acquire or otherwise create a singleton | ||
| 546 | default object if one is not passed in, e.g., a method call. | ||
| 547 | |||
| 548 | =cut | ||
| 549 | |||
| 550 | sub self_or_default { | ||
| 551 | return @_ if defined $_[0] and !ref $_[0] and $_[0] eq 'WebDAO::FCGI::ProcManager'; | ||
| 552 | if (!defined $_[0] or (ref($_[0]) ne 'WebDAO::FCGI::ProcManager' and | ||
| 553 | !UNIVERSAL::isa($_[0],'WebDAO::FCGI::ProcManager'))) { | ||
| 554 | $Q or $Q = $WebDAO::FCGI::ProcManager::Default->new; | ||
| 555 | unshift @_, $Q; | ||
| 556 | } | ||
| 557 | return wantarray ? @_ : $Q; | ||
| 558 | } | ||
| 559 | |||
| 560 | =head2 pm_change_process_name | ||
| 561 | |||
| 562 | instance or export | ||
| 563 | () pm_change_process_name() | ||
| 564 | |||
| 565 | DESCRIPTION: | ||
| 566 | |||
| 567 | =cut | ||
| 568 | |||
| 569 | sub pm_change_process_name { | ||
| 570 | my ($this,$name) = self_or_default(@_); | ||
| 571 | $0 = $name; | ||
| 572 | } | ||
| 573 | |||
| 574 | =head2 pm_received_signal | ||
| 575 | |||
| 576 | instance or export | ||
| 577 | () pm_received signal() | ||
| 578 | |||
| 579 | DESCRIPTION: | ||
| 580 | |||
| 581 | =cut | ||
| 582 | |||
| 583 | sub pm_received_signal { | ||
| 584 | my ($this,$sig,$received) = self_or_default(@_); | ||
| 585 | $sig or return $this->{SIG_RECEIVED}; | ||
| 586 | $received and $this->{SIG_RECEIVED}->{$sig}++; | ||
| 587 | return $this->{SIG_RECEIVED}->{$sig}; | ||
| 588 | } | ||
| 589 | |||
| 590 | =head1 parameters | ||
| 591 | |||
| 592 | =head2 pm_parameter | ||
| 593 | |||
| 594 | instance or export | ||
| 595 | () pm_parameter() | ||
| 596 | |||
| 597 | DESCRIPTION: | ||
| 598 | |||
| 599 | =cut | ||
| 600 | |||
| 601 | sub pm_parameter { | ||
| 602 | my ($this,$key,$value) = self_or_default(@_); | ||
| 603 | defined $value and $this->{$key} = $value; | ||
| 604 | return $this->{$key}; | ||
| 605 | } | ||
| 606 | |||
| 607 | =head2 n_processes | ||
| 608 | |||
| 609 | =head2 no_signals | ||
| 610 | |||
| 611 | =head2 pid_fname | ||
| 612 | |||
| 613 | =head2 die_timeout | ||
| 614 | |||
| 615 | =head2 role | ||
| 616 | |||
| 617 | =head2 start_delay | ||
| 618 | |||
| 619 | DESCRIPTION: | ||
| 620 | |||
| 621 | =cut | ||
| 622 | |||
| 623 | sub n_processes { shift->pm_parameter("n_processes", @_); } | ||
| 624 | sub pid_fname { shift->pm_parameter("pid_fname", @_); } | ||
| 625 | sub no_signals { shift->pm_parameter("no_signals", @_); } | ||
| 626 | sub die_timeout { shift->pm_parameter("die_timeout", @_); } | ||
| 627 | sub role { shift->pm_parameter("role", @_); } | ||
| 628 | sub start_delay { shift->pm_parameter("start_delay", @_); } | ||
| 629 | |||
| 630 | =head1 notification and death | ||
| 631 | |||
| 632 | =head2 pm_warn | ||
| 633 | |||
| 634 | instance or export | ||
| 635 | () pm_warn() | ||
| 636 | |||
| 637 | DESCRIPTION: | ||
| 638 | |||
| 639 | =cut | ||
| 640 | |||
| 641 | sub pm_warn { | ||
| 642 | my ($this,$msg) = self_or_default(@_); | ||
| 643 | $this->pm_notify($msg); | ||
| 644 | } | ||
| 645 | |||
| 646 | =head2 pm_notify | ||
| 647 | |||
| 648 | instance or export | ||
| 649 | () pm_notify() | ||
| 650 | |||
| 651 | DESCRIPTION: | ||
| 652 | |||
| 653 | =cut | ||
| 654 | |||
| 655 | sub pm_notify { | ||
| 656 | my ($this,$msg) = self_or_default(@_); | ||
| 657 | $msg =~ s/\s*$/\n/; | ||
| 658 | print STDERR "FastCGI: ".$this->role()." (pid $$): ".$msg; | ||
| 659 | } | ||
| 660 | |||
| 661 | =head2 pm_exit | ||
| 662 | |||
| 663 | instance or export | ||
| 664 | () pm_exit(string msg[, int exit_status]) | ||
| 665 | |||
| 666 | DESCRIPTION: | ||
| 667 | |||
| 668 | =cut | ||
| 669 | |||
| 670 | sub pm_exit { | ||
| 671 | my ($this,$msg,$n) = self_or_default(@_); | ||
| 672 | $n ||= 0; | ||
| 673 | |||
| 674 | # if we still have children at this point, something went wrong. | ||
| 675 | # SIGKILL them now. | ||
| 676 | kill "KILL", keys %{$this->{PIDS}} if $this->{PIDS}; | ||
| 677 | |||
| 678 | $this->pm_warn($msg); | ||
| 679 | $@ = $msg; | ||
| 680 | exit $n; | ||
| 681 | } | ||
| 682 | |||
| 683 | =head2 pm_abort | ||
| 684 | |||
| 685 | instance or export | ||
| 686 | () pm_abort(string msg[, int exit_status]) | ||
| 687 | |||
| 688 | DESCRIPTION: | ||
| 689 | |||
| 690 | =cut | ||
| 691 | |||
| 692 | sub pm_abort { | ||
| 693 | my ($this,$msg,$n) = self_or_default(@_); | ||
| 694 | $n ||= 1; | ||
| 695 | $this->pm_exit($msg,1); | ||
| 696 | } | ||
| 697 | |||
| 698 | 1; | ||
| 699 | __END__ | ||
| 700 | |||
| 701 | =head1 BUGS | ||
| 702 | |||
| 703 | No known bugs, but this does not mean no bugs exist. | ||
| 704 | |||
| 705 | =head1 SEE ALSO | ||
| 706 | |||
| 707 | L<FCGI>. | ||
| 708 | |||
| 709 | =head1 COPYRIGHT | ||
| 710 | |||
| 711 | FCGI-ProcManager - A Perl FCGI Process Manager | ||
| 712 | Copyright (c) 2000, FundsXpress Financial Network, Inc. | ||
| 713 | |||
| 714 | This library is free software; you can redistribute it and/or | ||
| 715 | modify it under the terms of the GNU Lesser General Public | ||
| 716 | License as published by the Free Software Foundation; either | ||
| 717 | version 2 of the License, or (at your option) any later version. | ||
| 718 | |||
| 719 | BECAUSE THIS LIBRARY IS LICENSED FREE OF CHARGE, THIS LIBRARY IS | ||
| 720 | BEING PROVIDED "AS IS WITH ALL FAULTS," WITHOUT ANY WARRANTIES | ||
| 721 | OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING, WITHOUT | ||
| 722 | LIMITATION, ANY IMPLIED WARRANTIES OF TITLE, NONINFRINGEMENT, | ||
| 723 | MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, AND THE | ||
| 724 | ENTIRE RISK AS TO SATISFACTORY QUALITY, PERFORMANCE, ACCURACY, | ||
| 725 | AND EFFORT IS WITH THE YOU. See the GNU Lesser General Public | ||
| 726 | License for more details. | ||
| 727 | |||
| 728 | You should have received a copy of the GNU Lesser General Public | ||
| 729 | License along with this library; if not, write to the Free Software | ||
| 730 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
| 731 | |||
| 732 | =cut |