| 1 | package Parallel::Prefork; |
|---|
| 2 | |
|---|
| 3 | use strict; |
|---|
| 4 | use warnings; |
|---|
| 5 | |
|---|
| 6 | use base qw/Class::Accessor::Fast/; |
|---|
| 7 | use List::Util qw/first/; |
|---|
| 8 | use Proc::Wait3; |
|---|
| 9 | |
|---|
| 10 | __PACKAGE__->mk_accessors(qw/max_workers err_respawn_interval trap_signals signal_received manager_pid/); |
|---|
| 11 | |
|---|
| 12 | our $VERSION = '0.01'; |
|---|
| 13 | |
|---|
| 14 | sub new { |
|---|
| 15 | my ($klass, $opts) = @_; |
|---|
| 16 | $opts ||= {}; |
|---|
| 17 | my $self = bless { |
|---|
| 18 | worker_pids => {}, |
|---|
| 19 | max_workers => 10, |
|---|
| 20 | err_respawn_interval => 1, |
|---|
| 21 | trap_signals => { |
|---|
| 22 | TERM => 'TERM', |
|---|
| 23 | }, |
|---|
| 24 | signal_received => '', |
|---|
| 25 | manager_pid => undef, |
|---|
| 26 | %$opts, |
|---|
| 27 | }, $klass; |
|---|
| 28 | $SIG{$_} = sub { |
|---|
| 29 | $self->signal_received($_[0]); |
|---|
| 30 | } for keys %{$self->trap_signals}; |
|---|
| 31 | $self; |
|---|
| 32 | } |
|---|
| 33 | |
|---|
| 34 | sub start { |
|---|
| 35 | my $self = shift; |
|---|
| 36 | |
|---|
| 37 | $self->manager_pid($$); |
|---|
| 38 | $self->signal_received(''); |
|---|
| 39 | |
|---|
| 40 | die 'cannot start another process while you are in child process' |
|---|
| 41 | if $self->{in_child}; |
|---|
| 42 | |
|---|
| 43 | # for debugging |
|---|
| 44 | return if $self->{max_workers} == 0; |
|---|
| 45 | |
|---|
| 46 | # main loop |
|---|
| 47 | while (! $self->signal_received) { |
|---|
| 48 | my $pid; |
|---|
| 49 | if (keys %{$self->{worker_pids}} < $self->max_workers) { |
|---|
| 50 | $pid = fork; |
|---|
| 51 | die 'fork error' unless defined $pid; |
|---|
| 52 | unless ($pid) { |
|---|
| 53 | # child process |
|---|
| 54 | $self->{in_child} = 1; |
|---|
| 55 | $SIG{$_} = 'DEFAULT' for keys %{$self->trap_signals}; |
|---|
| 56 | exit 0 if $self->signal_received; |
|---|
| 57 | return; |
|---|
| 58 | } |
|---|
| 59 | $self->{worker_pids}{$pid} = 1; |
|---|
| 60 | } |
|---|
| 61 | if (my ($exit_pid, $status) = wait3(! $pid)) { |
|---|
| 62 | delete $self->{worker_pids}{$exit_pid}; |
|---|
| 63 | unless ($status == 0) { |
|---|
| 64 | sleep $self->err_respawn_interval; |
|---|
| 65 | } |
|---|
| 66 | } |
|---|
| 67 | } |
|---|
| 68 | # send signals to workers |
|---|
| 69 | if (my $sig = $self->{trap_signals}{$self->signal_received}) { |
|---|
| 70 | $self->signal_all_children($sig); |
|---|
| 71 | } |
|---|
| 72 | |
|---|
| 73 | 1; # return from parent process |
|---|
| 74 | } |
|---|
| 75 | |
|---|
| 76 | sub finish { |
|---|
| 77 | my ($self, $exit_code) = @_; |
|---|
| 78 | return unless $self->{max_workers}; |
|---|
| 79 | exit($exit_code || 0); |
|---|
| 80 | } |
|---|
| 81 | |
|---|
| 82 | sub signal_all_children { |
|---|
| 83 | my ($self, $sig) = @_; |
|---|
| 84 | foreach my $pid (sort keys %{$self->{worker_pids}}) { |
|---|
| 85 | kill $sig, $pid; |
|---|
| 86 | } |
|---|
| 87 | } |
|---|
| 88 | |
|---|
| 89 | sub wait_all_children { |
|---|
| 90 | my $self = shift; |
|---|
| 91 | while (%{$self->{worker_pids}}) { |
|---|
| 92 | if (my $pid = wait) { |
|---|
| 93 | delete $self->{worker_pids}{$pid}; |
|---|
| 94 | } |
|---|
| 95 | } |
|---|
| 96 | } |
|---|
| 97 | |
|---|
| 98 | 1; |
|---|
| 99 | |
|---|
| 100 | __END__ |
|---|
| 101 | |
|---|
| 102 | =head1 NAME |
|---|
| 103 | Parallel::Prefork - A simple prefork server framework |
|---|
| 104 | |
|---|
| 105 | =head1 SYNOPSIS |
|---|
| 106 | |
|---|
| 107 | use Parallel::Prefork; |
|---|
| 108 | |
|---|
| 109 | my $pm = Parallel::Prefork->new({ |
|---|
| 110 | max_workers => 10, |
|---|
| 111 | fork_delay => 1, |
|---|
| 112 | trap_signals => { |
|---|
| 113 | TERM => TERM, |
|---|
| 114 | HUP => TERM, |
|---|
| 115 | USR1 => undef, |
|---|
| 116 | }); |
|---|
| 117 | |
|---|
| 118 | while ($pm->signal_received ne 'TERM') { |
|---|
| 119 | load_config(); |
|---|
| 120 | $pm->start and next; |
|---|
| 121 | |
|---|
| 122 | ... do some work within the child process ... |
|---|
| 123 | |
|---|
| 124 | $pm->finish; |
|---|
| 125 | } |
|---|
| 126 | |
|---|
| 127 | $pm->wait_all_children(); |
|---|
| 128 | |
|---|
| 129 | =head1 DESCRIPTION |
|---|
| 130 | |
|---|
| 131 | C<Parallel::Prefork> is much like C<Parallel::ForkManager>, but supports graceful shutdown and run-time reconfiguration. |
|---|
| 132 | |
|---|
| 133 | =head1 METHODS |
|---|
| 134 | |
|---|
| 135 | =head2 new |
|---|
| 136 | |
|---|
| 137 | Instantiation. Takes a hashref as an argument. Recognized attributes are as follows. |
|---|
| 138 | |
|---|
| 139 | =head3 max_workers |
|---|
| 140 | |
|---|
| 141 | number of worker processes (default: 10) |
|---|
| 142 | |
|---|
| 143 | =head3 err_respawn_interval |
|---|
| 144 | |
|---|
| 145 | interval until next child process is spawned after a worker exits abnormally (default: 1) |
|---|
| 146 | |
|---|
| 147 | =head3 trap_signals |
|---|
| 148 | |
|---|
| 149 | hashref of signals to be trapped. Manager process will trap the signals listed in the keys of the hash, and send the signal specified in the associated value (if any) to all worker processes. |
|---|
| 150 | |
|---|
| 151 | =head2 start |
|---|
| 152 | |
|---|
| 153 | The main routine. Returns undef in child processes. Returns a true value within manager process upon receiving a signal specified in the C<trap_signals> hashref. |
|---|
| 154 | |
|---|
| 155 | =head2 finish |
|---|
| 156 | |
|---|
| 157 | Child processes should call this function for termination. Takes exit code as an optional argument. Only usable from child processes. |
|---|
| 158 | |
|---|
| 159 | =head2 signal_all_children |
|---|
| 160 | |
|---|
| 161 | Sends signal to all worker processes. Only usable from manager process. |
|---|
| 162 | |
|---|
| 163 | =head2 wait_all_children |
|---|
| 164 | |
|---|
| 165 | Blocks until all worker processes exit. Only usable from manager process. |
|---|
| 166 | |
|---|
| 167 | =head1 LICENSE |
|---|
| 168 | |
|---|
| 169 | This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. |
|---|
| 170 | |
|---|
| 171 | See http://www.perl.com/perl/misc/Artistic.html |
|---|
| 172 | |
|---|
| 173 | =cut |
|---|