| 1 | package Gearman::Glutch; | ||||||||||||||||||||||||||||
| 2 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 3 | ▶ | use utf8; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 3 | ▶ | use parent qw/Class::Accessor::Fast/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 3 | ▶ | use 5.010001; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 3 | our $VERSION = '0.02'; | |||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 3 | ▶ | use POSIX ":sys_wait_h"; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 3 | ▶ | use Gearman::Worker; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 3 | ▶ | use Gearman::Server; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 3 | ▶ | use Danga::Socket; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 3 | __PACKAGE__->mk_accessors(qw/server port max_workers max_reqs_per_child on_spawn_child on_complete/); | |||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 1 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=3 SCAR=12.0
Pod
documented
| |||||||||||||||||||||||||||||
| 19 | 1 | my $class = shift; | |||||||||||||||||||||||||||
| 20 | 0 | ▶ | my %args = @_==1 ? %{$_[0]} : @_; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 21 | 1 | ▶ | my $port = $args{port} || Carp::croak "Missing mandatory parameter: port"; | ||||||||||||||||||||||||||
|
Condition50%1/2
$args{'port'} || Carp::croak('Missing mandatory parameter: port')
MC/DC0%0/1
$args{'port'} || Carp::croak('Missing mandatory parameter: port')
$args{'port'}
Truth table50%1/2
A
A= $args{'port'}
| |||||||||||||||||||||||||||||
| 22 | 1 | return bless { | |||||||||||||||||||||||||||
| 23 | server => Gearman::Server->new(), | ||||||||||||||||||||||||||||
| 24 | functions => [], | ||||||||||||||||||||||||||||
| 25 | max_workers => 1, | ||||||||||||||||||||||||||||
| 26 | %args | ||||||||||||||||||||||||||||
| 27 | }, $class; | ||||||||||||||||||||||||||||
| 28 | } | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 1 | ▶ | sub register_function { | ||||||||||||||||||||||||||
|
Subroutine
register_function: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 31 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 32 | 1 | push @{$self->{functions}}, \@_; | |||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 1 | ▶ | sub run { | ||||||||||||||||||||||||||
|
Subroutine
run: called CC=2 SCAR=11.3
Pod
documented
| |||||||||||||||||||||||||||||
| 36 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 1 | local $SIG{PIPE} = 'IGNORE'; # handled manually | |||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 1 | my $ssock = $self->server->create_listening_sock($self->port); | |||||||||||||||||||||||||||
| 41 | 1 | $self->{ssock} = $ssock; | |||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | # great graceful shutdown support | ||||||||||||||||||||||||||||
| 44 | 1 | $Gearmand::graceful_shutdown = 0; | |||||||||||||||||||||||||||
| 45 | 0 | ▶ | local *Gearmand::shutdown_graceful = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 46 | 0 | ▶ | return if $Gearmand::graceful_shutdown; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 47 | 0 | my $ofds = Danga::Socket->OtherFds; | |||||||||||||||||||||||||||
| 48 | 0 | delete $ofds->{fileno($ssock)}; | |||||||||||||||||||||||||||
| 49 | 0 | $ssock->close; | |||||||||||||||||||||||||||
| 50 | 0 | $Gearmand::graceful_shutdown = 1; | |||||||||||||||||||||||||||
| 51 | 0 | ▶ | exit 0 unless $self->server->jobs_outstanding; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 52 | 1 | }; | |||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | # spawn workers | ||||||||||||||||||||||||||||
| 55 | 1 | for my $i (1..$self->max_workers) { | |||||||||||||||||||||||||||
| 56 | 1 | $self->spawn_worker(); | |||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 0 | ▶ | local $SIG{CHLD} = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||
| 60 | 0 | my $kid; | |||||||||||||||||||||||||||
| 61 | 0 | ▶ | do { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 62 | 0 | $kid = waitpid(-1, WNOHANG); | |||||||||||||||||||||||||||
| 63 | 0 | ▶ | if ($kid) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 64 | 0 | delete $self->{pids}->{$kid}; | |||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | } while $kid > 0; | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 0 | ▶ | while (0+(keys %{$self->{pids}}) < $self->max_workers) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 69 | 0 | $self->spawn_worker(); | |||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||
| 71 | 1 | }; | |||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 1 | ▶ | $SIG{INT} = $SIG{TERM} = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 74 | 1 | $self->shutdown(); | |||||||||||||||||||||||||||
| 75 | 1 | exit 0; | |||||||||||||||||||||||||||
| 76 | 1 | }; | |||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | # run main loop | ||||||||||||||||||||||||||||
| 79 | 1 | Danga::Socket->EventLoop(); | |||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 1 | ▶ | sub spawn_worker { | ||||||||||||||||||||||||||
|
Subroutine
spawn_worker: called CC=2 SCAR=13.7
Pod
undocumented
| |||||||||||||||||||||||||||||
| 83 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 0 | ▶ | my $pid = $self->server->start_worker(sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 86 | 0 | close $self->{ssock}; | |||||||||||||||||||||||||||
| 87 | 0 | $self->{child}++; | |||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 0 | my $worker = Gearman::Worker->new(); | |||||||||||||||||||||||||||
| 90 | 0 | for my $func (@{$self->{functions}}) { | |||||||||||||||||||||||||||
| 91 | 0 | $worker->register_function(@$func); | |||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | 0 | my $i=0; | |||||||||||||||||||||||||||
| 94 | $worker->work( | ||||||||||||||||||||||||||||
| 95 | 0 | ▶ | stop_if => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 96 | 0 | ▶ | if (defined($self->max_reqs_per_child) && $i >= $self->max_reqs_per_child) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $self->max_reqs_per_child and $i >= $self->max_reqs_per_child
MC/DC0%0/2
defined $self->max_reqs_per_child and $i >= $self->max_reqs_per_child
defined $self->max_reqs_per_child
$i >= $self->max_reqs_per_child
Truth table0%0/3
A and B
A= defined $self->max_reqs_per_childB= $i >= $self->max_reqs_per_child
| |||||||||||||||||||||||||||||
| 97 | 0 | 1; | |||||||||||||||||||||||||||
| 98 | } else { | ||||||||||||||||||||||||||||
| 99 | 0 | 0 | |||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | }, | ||||||||||||||||||||||||||||
| 102 | 0 | ▶ | on_complete => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 103 | 0 | ▶ | $self->on_complete->(@_) if $self->on_complete; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 104 | 0 | ++$i; | |||||||||||||||||||||||||||
| 105 | }, | ||||||||||||||||||||||||||||
| 106 | 0 | ); | |||||||||||||||||||||||||||
| 107 | 0 | exit 0; | |||||||||||||||||||||||||||
| 108 | 1 | }); | |||||||||||||||||||||||||||
| 109 | 1 | ▶ | $self->on_spawn_child->($pid) if $self->on_spawn_child; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 110 | 1 | $self->{pids}->{$pid}++; | |||||||||||||||||||||||||||
| 111 | 1 | return $pid; | |||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 0 | ▶ | sub DESTROY { | ||||||||||||||||||||||||||
|
Subroutine
DESTROY: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 115 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 116 | 0 | ▶ | return if $self->{child}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 117 | 0 | $self->shutdown(); | |||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | 1 | ▶ | sub shutdown :method { | ||||||||||||||||||||||||||
|
Subroutine
shutdown: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 121 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | 1 | $self->signal_all_children('TERM'); | |||||||||||||||||||||||||||
| 124 | 1 | $self->_wait_all_children(); | |||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | 1 | ▶ | sub signal_all_children { | ||||||||||||||||||||||||||
|
Subroutine
signal_all_children: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 128 | 1 | my ( $self, $sig ) = @_; | |||||||||||||||||||||||||||
| 129 | 1 | for my $pid ( sort keys %{ $self->{pids} } ) { | |||||||||||||||||||||||||||
| 130 | 1 | kill $sig, $pid; | |||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | 1 | ▶ | sub _wait_all_children { | ||||||||||||||||||||||||||
|
Subroutine
_wait_all_children: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||
| 135 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | 1 | my $kid; | |||||||||||||||||||||||||||
| 138 | 1 | ▶ | do { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 139 | 1 | $kid = waitpid(-1, WNOHANG); | |||||||||||||||||||||||||||
| 140 | } while $kid > 0; | ||||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | 3 | 1; | |||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | __END__ | ||||||||||||||||||||||||||||