| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Parallel::Boss; | 
| 2 | 3 |  |  | 3 |  | 303413 | use 5.012; | 
|  | 3 |  |  |  |  | 11 |  | 
| 3 | 3 |  |  | 3 |  | 21 | use strict; | 
|  | 3 |  |  |  |  | 7 |  | 
|  | 3 |  |  |  |  | 88 |  | 
| 4 | 3 |  |  | 3 |  | 12 | use warnings; | 
|  | 3 |  |  |  |  | 21 |  | 
|  | 3 |  |  |  |  | 2723 |  | 
| 5 |  |  |  |  |  |  | our $VERSION = "0.03"; | 
| 6 |  |  |  |  |  |  | my $XS_VERSION=$VERSION; | 
| 7 |  |  |  |  |  |  | $VERSION = eval $VERSION; | 
| 8 |  |  |  |  |  |  |  | 
| 9 |  |  |  |  |  |  | require XSLoader; | 
| 10 |  |  |  |  |  |  | XSLoader::load("Parallel::Boss", $XS_VERSION); | 
| 11 |  |  |  |  |  |  |  | 
| 12 |  |  |  |  |  |  | =head1 NAME | 
| 13 |  |  |  |  |  |  |  | 
| 14 |  |  |  |  |  |  | Parallel::Boss - manage worker processes | 
| 15 |  |  |  |  |  |  |  | 
| 16 |  |  |  |  |  |  | =head1 VERSION | 
| 17 |  |  |  |  |  |  |  | 
| 18 |  |  |  |  |  |  | This document describes Parallel::Boss version 0.03 | 
| 19 |  |  |  |  |  |  |  | 
| 20 |  |  |  |  |  |  | =head1 SYNOPSIS | 
| 21 |  |  |  |  |  |  |  | 
| 22 |  |  |  |  |  |  | use Parallel::Boss; | 
| 23 |  |  |  |  |  |  |  | 
| 24 |  |  |  |  |  |  | my $worker = sub { | 
| 25 |  |  |  |  |  |  | my ( @args ) = @_; | 
| 26 |  |  |  |  |  |  | # pretend to be working | 
| 27 |  |  |  |  |  |  | ...; | 
| 28 |  |  |  |  |  |  | }; | 
| 29 |  |  |  |  |  |  |  | 
| 30 |  |  |  |  |  |  | Parallel::Boss->run( | 
| 31 |  |  |  |  |  |  | num_workers  => 4, | 
| 32 |  |  |  |  |  |  | args         => \@args, | 
| 33 |  |  |  |  |  |  | exit_timeout => 15, | 
| 34 |  |  |  |  |  |  | worker       => $worker, | 
| 35 |  |  |  |  |  |  | ); | 
| 36 |  |  |  |  |  |  |  | 
| 37 |  |  |  |  |  |  | =head1 DESCRIPTION | 
| 38 |  |  |  |  |  |  |  | 
| 39 |  |  |  |  |  |  | Module running specified number of worker processes. | 
| 40 |  |  |  |  |  |  |  | 
| 41 |  |  |  |  |  |  | =head1 METHODS | 
| 42 |  |  |  |  |  |  |  | 
| 43 |  |  |  |  |  |  | =cut | 
| 44 |  |  |  |  |  |  |  | 
| 45 |  |  |  |  |  |  | =head2 run | 
| 46 |  |  |  |  |  |  |  | 
| 47 |  |  |  |  |  |  | $class->run(%params) | 
| 48 |  |  |  |  |  |  |  | 
| 49 |  |  |  |  |  |  | start specified number of workers and supervise them. If any of the workers | 
| 50 |  |  |  |  |  |  | exits, a new one will be started as a replacement. If parent process receives | 
| 51 |  |  |  |  |  |  | HUP signal, then it sends HUP signal to every worker process and restarts | 
| 52 |  |  |  |  |  |  | workers if they exit. If parent process receives INT, QUIT, or TERM, it sends | 
| 53 |  |  |  |  |  |  | TERM to all workers, waits for up to I seconds till they all | 
| 54 |  |  |  |  |  |  | exit, and sends KILL to those workers that are still running, after all workers | 
| 55 |  |  |  |  |  |  | exited the run method returns. Each worker process runs watchdog thread that | 
| 56 |  |  |  |  |  |  | detects if the parent process has died and terminates the worker by sending it | 
| 57 |  |  |  |  |  |  | first SIGTERM and then calling _exit(2) after I seconds if the | 
| 58 |  |  |  |  |  |  | worker is still running. | 
| 59 |  |  |  |  |  |  |  | 
| 60 |  |  |  |  |  |  | The following parameters are accepted: | 
| 61 |  |  |  |  |  |  |  | 
| 62 |  |  |  |  |  |  | =over 4 | 
| 63 |  |  |  |  |  |  |  | 
| 64 |  |  |  |  |  |  | =item B | 
| 65 |  |  |  |  |  |  |  | 
| 66 |  |  |  |  |  |  | number of workers to start | 
| 67 |  |  |  |  |  |  |  | 
| 68 |  |  |  |  |  |  | =item B | 
| 69 |  |  |  |  |  |  |  | 
| 70 |  |  |  |  |  |  | reference to array of arguments that should be passed to worker subroutine | 
| 71 |  |  |  |  |  |  |  | 
| 72 |  |  |  |  |  |  | =item B | 
| 73 |  |  |  |  |  |  |  | 
| 74 |  |  |  |  |  |  | when parent process signalled to exit it first sends to all workers SIGTERM. If | 
| 75 |  |  |  |  |  |  | exit_timeout is set and greater than zero then after exit_timeout workers that | 
| 76 |  |  |  |  |  |  | are still running are sent SIGKILL. | 
| 77 |  |  |  |  |  |  |  | 
| 78 |  |  |  |  |  |  | =item B | 
| 79 |  |  |  |  |  |  |  | 
| 80 |  |  |  |  |  |  | subroutine that will be executed by every worker. If it returns, the worker | 
| 81 |  |  |  |  |  |  | process exits. The I array is passed to subroutine as the list of | 
| 82 |  |  |  |  |  |  | arguments. | 
| 83 |  |  |  |  |  |  |  | 
| 84 |  |  |  |  |  |  | =back | 
| 85 |  |  |  |  |  |  |  | 
| 86 |  |  |  |  |  |  | =cut | 
| 87 |  |  |  |  |  |  |  | 
| 88 |  |  |  |  |  |  | sub run { | 
| 89 | 1 |  |  | 1 | 1 | 17754 | my ( $class, %args ) = @_; | 
| 90 |  |  |  |  |  |  |  | 
| 91 | 1 |  |  |  |  | 17 | my $self = bless \%args, $class; | 
| 92 |  |  |  |  |  |  |  | 
| 93 | 1 | 50 |  |  |  | 163 | pipe( my $rd, my $wr ) or die "Couldn't create a pipe"; | 
| 94 | 1 |  |  |  |  | 24 | $self->{_rd} = $rd; | 
| 95 | 1 |  |  |  |  | 10 | $self->{_wr} = $wr; | 
| 96 |  |  |  |  |  |  |  | 
| 97 |  |  |  |  |  |  | local $SIG{QUIT} = local $SIG{INT} = local $SIG{TERM} = sub { | 
| 98 | 1 |  |  | 1 |  | 13 | $self->{_finish} = 1; | 
| 99 | 1 |  |  |  |  | 74 | $self->{_wr}->close; | 
| 100 | 1 |  |  |  |  | 87 | $self->_kill_children("TERM"); | 
| 101 | 1 | 50 |  |  |  | 1218 | alarm $self->{exit_timeout} if $self->{exit_timeout}; | 
| 102 | 1 |  |  |  |  | 77 | }; | 
| 103 | 1 |  |  | 1 |  | 18 | local $SIG{HUP} = sub { $self->_kill_children("HUP"); }; | 
|  | 1 |  |  |  |  | 28 |  | 
| 104 |  |  |  |  |  |  | local $SIG{ALRM} = sub { | 
| 105 | 0 | 0 |  | 0 |  | 0 | $self->_kill_children("KILL") if $self->{_finish}; | 
| 106 | 1 |  |  |  |  | 7 | }; | 
| 107 |  |  |  |  |  |  |  | 
| 108 | 1 |  |  |  |  | 15 | for ( 1 .. $self->{num_workers} ) { | 
| 109 | 4 |  |  |  |  | 27 | $self->_spawn; | 
| 110 |  |  |  |  |  |  | } | 
| 111 |  |  |  |  |  |  |  | 
| 112 | 1 |  |  |  |  | 19 | while (1) { | 
| 113 | 9 |  |  |  |  | 611753 | my $pid = wait; | 
| 114 | 9 | 50 |  |  |  | 202 | delete $self->{_workers}{$pid} or next; | 
| 115 | 9 | 100 |  |  |  | 34 | if ($self->{_finish}) { | 
| 116 | 4 | 100 |  |  |  | 8 | last unless keys %{ $self->{_workers} }; | 
|  | 4 |  |  |  |  | 126 |  | 
| 117 |  |  |  |  |  |  | } else { | 
| 118 | 5 |  |  |  |  | 23 | $self->_spawn; | 
| 119 |  |  |  |  |  |  | } | 
| 120 |  |  |  |  |  |  | } | 
| 121 |  |  |  |  |  |  | } | 
| 122 |  |  |  |  |  |  |  | 
| 123 |  |  |  |  |  |  | sub _spawn { | 
| 124 | 9 |  |  | 9 |  | 18 | my ($self) = @_; | 
| 125 | 9 |  |  |  |  | 5684 | my $pid = fork; | 
| 126 | 9 | 50 |  |  |  | 138 | if ( not defined $pid ) { | 
| 127 | 0 |  |  |  |  | 0 | $self->_kill_children("KILL"); | 
| 128 | 0 |  |  |  |  | 0 | die "Couldn't fork, exiting: $!"; | 
| 129 |  |  |  |  |  |  | } | 
| 130 |  |  |  |  |  |  |  | 
| 131 | 9 | 50 |  |  |  | 150 | if ($pid) { | 
| 132 | 9 |  |  |  |  | 430 | $self->{_workers}{$pid} = 1; | 
| 133 |  |  |  |  |  |  | } | 
| 134 |  |  |  |  |  |  | else { | 
| 135 | 0 |  |  |  |  | 0 | eval { | 
| 136 | 0 |  |  |  |  | 0 | $self->{_wr}->close; | 
| 137 | 0 |  |  |  |  | 0 | $SIG{$_} = 'DEFAULT' for qw(QUIT HUP INT TERM ALRM); | 
| 138 | 0 |  | 0 |  |  | 0 | _start_watchdog( $self->{_rd}->fileno, ($self->{exit_timeout} // 0 )); | 
| 139 | 0 |  |  |  |  | 0 | $self->{worker}->( $self, @{ $self->{args} } ); | 
|  | 0 |  |  |  |  | 0 |  | 
| 140 |  |  |  |  |  |  | }; | 
| 141 | 0 |  |  |  |  | 0 | exit 0; | 
| 142 |  |  |  |  |  |  | } | 
| 143 |  |  |  |  |  |  | } | 
| 144 |  |  |  |  |  |  |  | 
| 145 |  |  |  |  |  |  | sub _kill_children { | 
| 146 | 2 |  |  | 2 |  | 12 | my ($self, $sig) = @_; | 
| 147 |  |  |  |  |  |  |  | 
| 148 | 2 |  |  |  |  | 6 | kill $sig => keys %{ $self->{_workers} }; | 
|  | 2 |  |  |  |  | 1484 |  | 
| 149 |  |  |  |  |  |  | } | 
| 150 |  |  |  |  |  |  |  | 
| 151 |  |  |  |  |  |  | 1; | 
| 152 |  |  |  |  |  |  |  | 
| 153 |  |  |  |  |  |  | __END__ |