lib/Rex/Fork/Manager.pm

filter: Statementstmt 83.9% 47 / 56 Branchbran 62.5% 10 / 16 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 90.0% 9 / 10 Podpod 0.0% 0 / 6 totaltotal 72.0% 67 / 93 CC 10 SCAR 15.1
CC
10
Cov
77%
start
12.0
wait_for
6.0
new
2.0
CRAP 4.5
« lib/Rex/Interface/Shell/Base.pm ↑ Summary lib/Rex/Cloud.pm »
1 #
2 # (c) Jan Gehring <jan.gehring@gmail.com>
3 #
4  
5 package Rex::Fork::Manager;
6  
7 56 ▶use v5.14.4;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 56 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 56 our $VERSION = '1.16.1'; # VERSION
11  
12 56 ▶use Rex::Fork::Task;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 56 ▶use Time::HiRes qw(sleep);
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 138 ▶sub new {
Subroutine new: called CC=2 SCAR=7.1
Pod undocumented
16 138 my $that = shift;
17 138 ▶ my $proto = ref($that) || $that;
Condition33%1/3
ref $that || $that
l!l&&r!l&&!r
01380
MC/DC0%0/2
ref $that || $that
ref $that $that
Truth table33%1/3
A || B
A= ref $thatB= $that
ABresult
1X1
011
000
18 138 my $self = {@_};
19  
20 138 bless( $self, $proto );
21  
22 138 $self->{'forks'} = [];
23 138 $self->{'running'} = 0;
24  
25 138 return $self;
26 }
27  
28 135 ▶sub add {
Subroutine add: called CC=2 SCAR=7.0
Pod undocumented
29 135 my ( $self, $coderef ) = @_;
30  
31 135 my $f = Rex::Fork::Task->new( coderef => $coderef );
32  
33 135 push( @{ $self->{'forks'} }, $f );
34  
35 135 $f->start;
36 106 ++$self->{'running'};
37  
38 106 ▶ if ( $self->{'running'} >= $self->{'max'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"running"} >= $self->{"max"}) 106 0 106
39 106 $self->wait_for_one;
40 }
41 }
42  
43 0 ▶sub start {
Subroutine start: not called CC=3 SCAR=24.8
Pod undocumented
44 0 my ($self) = @_;
45  
46 0 my @threads = @{ $self->{'forks'} };
47 0 ▶ for ( my $i = 0 ; $i < scalar(@threads) ; ++$i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @threads) 0 0 0
48 0 $threads[$i]->start;
49 0 ++$self->{'running'};
50 0 ▶ if ( $self->{'running'} >= $self->{'max'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"running"} >= $self->{"max"}) 0 0 0
51 0 $self->wait_for_one;
52 }
53 }
54  
55 0 $self->wait_for_all;
56 }
57  
58 106 ▶sub wait_for_one {
Subroutine wait_for_one: called CC=1 SCAR=0.0
Pod undocumented
59 106 my ($self) = @_;
60 106 $self->wait_for;
61 }
62  
63 107 ▶sub wait_for_all {
Subroutine wait_for_all: called CC=1 SCAR=0.0
Pod undocumented
64 107 my ($self) = @_;
65 107 $self->wait_for(1);
66 }
67  
68 213 ▶sub wait_for {
Subroutine wait_for: called CC=6 SCAR=17.9
Pod undocumented
69 213 my ( $self, $all ) = @_;
70 do {
71 3242 ▶ FORK: for ( my $i = 0 ; $i < scalar( @{ $self->{'forks'} } ) ; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @{$self->{"forks"};}) 3241 3136 6377
72 3241 my $thr = $self->{'forks'}->[$i];
73 3241 ▶ unless ( $thr->{'running'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($thr->{"running"}) 106 3135 3241
74 106 next FORK;
75 }
76  
77 3135 my $kid;
78 3135 $kid = $thr->wait;
79  
80 3135 ▶ if ( $kid == -1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($kid == -1) 106 3029 3135
81 106 $thr = undef;
82 106 $thr->{running} = 0;
83 106 --$self->{'running'};
84  
85 106 ▶ return 1 unless $all;
Branch: 1 branch
BranchTrueFalseTotal
unless $all 106 0 106
86 }
87 3029 sleep Rex::Config->get_waitpid_blocking_sleep_time;
88 }
89 213 ▶ } until $self->{'running'} == 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->{"running"} == 0 or do { do { FORK: for (my $i = 0; $i < scalar @{$self->{"forks"};}; ++$i) { my $thr = $self->{"forks"}[$i]; unless ($thr->{"running"}) { next FORK; } ; my $kid; $kid = $thr->wait; if ($kid == -1) { $thr = undef; $thr->{"running"} = 0; --$self->{"running"}; return 1 unless $all; } ; &Rex::Fork::Manager::sleep(scalar "Rex::Config"->get_waitpid_blocking_sleep_time); } } } 107 3029 3136
90 }
91  
92 56 1;
« lib/Rex/Interface/Shell/Base.pm ↑ Summary lib/Rex/Cloud.pm »