ProcessPool/Process.pm

filter: Statementstmt 90.9% 80 / 88 Branchbran 75.0% 15 / 20 Conditioncond 72.7% 8 / 11 MC/DCmcdc 57.1% 4 / 7 Subroutinesub 83.3% 20 / 24 Podpod 0.0% 0 / 7 totaltotal 80.8% 127 / 157 SCAR 26.7
CC
14
Cov
87%
DESTROY
7.5
__ANON__
3.0
is_running
2.0
CRAP 14.5
« ProcessPool/Util.pm ↑ Summary ProcessPool.pm »
1 package AnyEvent::ProcessPool::Process;
2 # ABSTRACT: Manages an individual worker process
3 3 $AnyEvent::ProcessPool::Process::VERSION = '0.06';
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 3 ▶use Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use AnyEvent;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use AnyEvent::Open3::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use AnyEvent::ProcessPool::Task;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use AnyEvent::ProcessPool::Util 'next_id';
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use String::Escape 'backslash';
Subroutine BEGIN: called CC=1 SCAR=0.0
13 3 ▶use Try::Catch;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 3 my $perl = $Config{perlpath};
16 3 my $ext = $Config{_exe};
17 3 ▶$perl .= $ext if $^O ne 'VMS' && $perl !~ /$ext$/i;
Branch: 1 branch
BranchTrueFalseTotal
if $^O ne "VMS" and not $perl =~ /$ext$/i 0 3 3
Condition33%1/3
$^O ne "VMS" and not $perl =~ /$ext$/i
!ll&&!rl&&r
030
MC/DC0%0/2
$^O ne "VMS" and not $perl =~ /$ext$/i
$^O ne "VMS" not $perl =~ /$ext$/i
Truth table33%1/3
A and not(B)
A= $^O ne "VMS"B= not $perl =~ /$ext$/i
ABresult
0X0
100
111
18 3 my @inc = map { sprintf('-I%s', backslash($_)) } @_, @INC;
19 3 my $cmd = join ' ', @inc, q(-MAnyEvent::ProcessPool::Worker -e 'AnyEvent::ProcessPool::Worker::run()');
20  
21 sub new {
22 48 ▶ my ($class, %param) = @_;
Subroutine new: called CC=2 SCAR=6.9
Pod undocumented
23 48 ▶ my $include = $param{include} || [];
Condition100%2/2
$param{'include'} || []
l!l
147
MC/DC100%1/1
$param{'include'} || []
$param{'include'}
Truth table100%2/2
A
A= $param{'include'}
Aresult
11
00
24  
25 return bless {
26 id => next_id,
27 limit => $param{limit},
28 48 include => join(' ', map { sprintf('-I%s', backslash($_)) } @$include),
29 started => undef,
30 process => undef,
31 ps => undef,
32 pending => [],
33 }, $class;
34 }
35  
36 sub DESTROY {
37 31 ▶ my $self = shift;
Subroutine DESTROY: called CC=5 SCAR=20.1
38 31 ▶ $self->{ps}->close if $self->{ps};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"ps"} 0 31 31
39 31 ▶ if (ref $self->{pending}) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->{"pending"}) 31 0 31
40 31 foreach my $cv (@{$self->{pending}}) {
41 0 ▶ if ($cv) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cv) 0 0 0
42 0 $cv->croak('AnyEvent::ProcessPool::Process went out of scope with pending tasks');
43 }
44 }
45 }
46 }
47  
48 sub pid {
49 4 ▶ my $self = shift;
Subroutine pid: called CC=2 SCAR=6.9
Pod undocumented
50 4 ▶ return $self->{ps}->pid if $self->is_running;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_running 3 1 4
51 }
52  
53 sub is_running {
54 41 ▶ my $self = shift;
Subroutine is_running: called CC=2 SCAR=7.1
Pod undocumented
55 return defined $self->{started}
56 41 ▶ && $self->{started}->ready;
Condition66%2/3
defined $self->{'started'} && $self->{'started'}->ready
!ll&&!rl&&r
28013
MC/DC50%1/2
defined $self->{'started'} && $self->{'started'}->ready
defined $self->{'started'} $self->{'started'}->ready
Truth table66%2/3
A && B
A= defined $self->{'started'}B= $self->{'started'}->ready
ABresult
0X0
100
111
57 }
58  
59 sub await {
60 33 ▶ my $self = shift;
Subroutine await: called CC=2 SCAR=6.9
Pod undocumented
61 33 ▶ $self->start unless $self->is_running;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_running 25 8 33
62 33 $self->{started}->recv;
63 }
64  
65 sub stop {
66 47 ▶ my $self = shift;
Subroutine stop: called CC=2 SCAR=6.9
Pod undocumented
67 47 ▶ if (defined $self->{process}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"process"}) 19 28 47
68 19 $self->{ps}->close;
69 19 undef $self->{started};
70 19 undef $self->{process};
71 19 undef $self->{ps};
72 }
73 }
74  
75 sub start {
76 25 ▶ my $self = shift;
Subroutine start: called CC=1 SCAR=0.1
Pod undocumented
77 25 $self->{started} = AE::cv;
78 $self->{process} = AnyEvent::Open3::Simple->new(
79 on_start => sub{
80 25 ▶ $self->{started}->send;
Subroutine __ANON__: called CC=1 SCAR=0.0
81 },
82 on_stdout => sub{
83 30 ▶ my ($ps, $line) = @_;
Subroutine __ANON__: called CC=3 SCAR=11.0
84 30 my $task = AnyEvent::ProcessPool::Task->decode($line);
85 30 my $cv = shift @{$self->{pending}};
86 30 $cv->send($task);
87  
88 30 ▶ if ($self->{limit} && $ps->user->{reqs} <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"limit"} and $ps->user->{"reqs"} <= 0) 6 24 30
Condition100%3/3
$self->{"limit"} and $ps->user->{"reqs"} <= 0
!ll&&!rl&&r
1596
MC/DC100%2/2
$self->{"limit"} and $ps->user->{"reqs"} <= 0
$self->{"limit"} $ps->user->{"reqs"} <= 0
Truth table100%3/3
A and B
A= $self->{"limit"}B= $ps->user->{"reqs"} <= 0
ABresult
0X0
100
111
89 6 $self->stop;
90 }
91 },
92 on_stderr => sub{
93 0 ▶ warn "AnyEvent::ProcessPool::Worker: $_[1]\n";
Subroutine __ANON__: not called CC=1 SCAR=6.9
94 },
95 on_error => sub{
96 0 ▶ die "error launching worker process: $_[0]";
Subroutine __ANON__: not called CC=1 SCAR=6.9
97 },
98 on_signal => sub{
99 0 ▶ warn "worker terminated in response to signal: $_[1]";
Subroutine __ANON__: not called CC=1 SCAR=6.9
100 0 $self->stop;
101 },
102 on_fail => sub{
103 0 ▶ warn "worker terminated with non-zero exit status: $_[1]";
Subroutine __ANON__: not called CC=1 SCAR=6.9
104 0 $self->stop;
105 },
106 25 );
107  
108 $self->{process}->run("$perl $self->{include} $cmd", sub{
109 25 ▶ my $ps = shift;
Subroutine __ANON__: called CC=2 SCAR=6.9
110 25 ▶ $ps->user({reqs => $self->{limit}}) if $self->{limit};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"limit"} 9 16 25
111 25 $self->{ps} = $ps;
112 25 });
113 }
114  
115 sub run {
116 30 ▶ my ($self, $task) = @_;
Subroutine run: called CC=2 SCAR=6.9
Pod undocumented
117 30 $self->await;
118  
119 30 my $cv = AE::cv;
120 30 push @{$self->{pending}}, $cv;
121  
122 30 $self->{ps}->say($task->encode);
123 30 ▶ --$self->{ps}->user->{reqs} if $self->{limit};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"limit"} 15 15 30
124  
125 30 return $cv;
126 }
127  
128 3 1;
129  
130 __END__
« ProcessPool/Util.pm ↑ Summary ProcessPool.pm »