MCP/Master.pm

filter: Statementstmt 75.6% 112 / 148 Branchbran 45.8% 11 / 24 Conditioncond 33.3% 2 / 6 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 83.3% 25 / 30 Podpod 90.0% 9 / 10 totaltotal 71.9% 159 / 221 CC 18 SCAR 16.3
CC
18
Cov
70%
run_due_tests
12.4
handle_dead_children
12.0
__ANON__
6.0
CRAP 5.1
« MCP/Net/Reset/PM211MIP.pm ↑ Summary MCP/State/Details.pm »
1 ## no critic (RequireUseStrict)
2 package Tapper::MCP::Master;
3 7 our $AUTHORITY = 'cpan:TAPPER';
4 # ABSTRACT: Wait for new testruns and start a new child when needed
5 7 $Tapper::MCP::Master::VERSION = '5.0.9';
6 7 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 7 ▶ use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 7 ▶ use parent "Tapper::MCP";
Subroutine BEGIN: called CC=1 SCAR=0.0
9 7 ▶ use Devel::Backtrace;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 7 ▶ use File::Path;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 7 ▶ use Log::Log4perl;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 7 ▶ use POSIX ":sys_wait_h";
Subroutine BEGIN: called CC=1 SCAR=0.0
13 7 ▶ use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 7 ▶ use UNIVERSAL;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 7 ▶ use constant HARNESS_ACTIVE => $ENV{HARNESS_ACTIVE};
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 7 ▶ use Tapper::Cmd::Testrun;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 7 ▶ use Tapper::MCP::Child;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 7 ▶ use Tapper::MCP::Net;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 7 ▶ use Tapper::MCP::Scheduler::Controller;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 7 ▶ use Tapper::Model 'model';
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23  
24 7 has dead_child => (is => 'rw', default => 0);
25  
26  
27 3 ▶ has child => (is => 'rw', isa => 'HashRef', default => sub {{}});
Subroutine __ANON__: called CC=1 SCAR=0.0
28  
29  
30 7 has scheduler => (is => 'rw', isa => 'Tapper::MCP::Scheduler::Controller');
31  
32  
33 sub BUILD
34 3 ▶{
Subroutine BUILD: called CC=1 SCAR=0.0
Pod undocumented
35 3 my $self = shift;
36 3 $self->scheduler(Tapper::MCP::Scheduler::Controller->new());
37 }
38  
39  
40  
41 sub cleanup_output_dir
42 1 ▶{
Subroutine cleanup_output_dir: called CC=1 SCAR=0.0
Pod documented
43 1 my ($self, $testrun_id) = @_;
44 1 my $path = $self->cfg->{paths}{output_dir}."/$testrun_id";
45 1 File::Path::rmtree($path);
46 }
47  
48  
49 sub set_interrupt_handlers
50 2 ▶ {
Subroutine set_interrupt_handlers: called CC=1 SCAR=1.7
Pod documented
51 2 my ($self) = @_;
52 2 ▶ $SIG{CHLD} = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
53 2 $self->dead_child($self->dead_child + 1);
54 2 $self->log->debug("SIGCHLD - counter: ".$self->dead_child);
55 2 };
56  
57 # give me a stack trace when ^C
58 0 ▶ $SIG{INT} = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
59 0 $SIG{INT} = 'IGNORE'; # make handler reentrant, don't handle signal twice
60  
61 # stop all children
62 0 $SIG{CHLD} = 'IGNORE';
63 0 foreach my $pid (keys %{$self->child}) {
64 0 kill 15, $pid;
65 }
66  
67 0 my $backtrace = Devel::Backtrace->new(-start=>2, -format => '%I. %s');
68 0 print $backtrace;
69  
70 0 exit -1;
71 2 };
72 2 return 0;
73 }
74  
75  
76 sub handle_dead_children
77 0 ▶ {
Subroutine handle_dead_children: not called CC=3 SCAR=24.8
Pod documented
78 0 my ($self) = @_;
79 0 ▶ CHILD: while ($self->dead_child) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->dead_child) 0 0 0
80 0 $self->log->debug("Number of dead testrun processes is ".$self->dead_child);
81 0 my $dead_pid = waitpid(-1, WNOHANG); # don't use wait(); qx() sends a SIGCHLD and increases $self->deadchild, but wait() for the return value and thus our wait would block
82  
83 0 $self->dead_child($self->dead_child - 1);
84 0 ▶ next if not $self->child->{$dead_pid}; # sig raised by qx()
Branch: 1 branch
BranchTrueFalseTotal
unless $self->child->{$dead_pid} 0 0 0
85  
86 0 my $job = $self->child->{$dead_pid}->{job};
87 0 my $host = $job->host->name;
88 0 my $testrun = $job->testrun->id;
89 0 $self->log->debug("Testrun $testrun (pid $dead_pid) on host '$host' finished as dead process");
90 0 $self->scheduler->mark_job_as_finished( $job );
91 0 delete $self->child->{$dead_pid};
92 }
93 0 $self->log->debug("handled dead processes - SIGCHLD - counter: ".$self->dead_child);
94 }
95  
96  
97  
98 sub notify_event
99 1 ▶ {
Subroutine notify_event: called CC=1 SCAR=0.0
Pod documented
100 1 my ($self, $event, $message) = @_;
101 try
102 1 ▶ {
Subroutine __ANON__: called CC=1 SCAR=0.0
103 1 my $new_event = model('TestrunDB')->resultset('NotificationEvent')->new({type => $event,
104 message => $message,
105 });
106 1 $new_event->insert();
107 0 ▶ } catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
108 0 $self->log->error("Unable notify user of event $event: $_");
109 1 };
110  
111 1 return;
112 }
113  
114  
115  
116 sub run_due_tests
117 2 ▶ {
Subroutine run_due_tests: called CC=11 SCAR=25.2
Pod documented
118 2 my ($self, $job, $revive) = @_;
119 2 $self->log->debug('run_due_test');
120  
121 2 ▶ return 1 if not $job->host;
Branch: 1 branch
BranchTrueFalseTotal
unless $job->host 0 2 2
122  
123 2 my $system = $job->host->name;
124 2 my $id = $job->testrun->id;
125  
126 2 $self->log->info("start testrun $id on $system");
127 # check if this system is already active, just for error handling
128  
129 2 ▶ $self->scheduler->mark_job_as_running($job) unless $revive;
Branch: 1 branch
BranchTrueFalseTotal
unless $revive 2 0 2
130  
131 2 my $pid = fork();
132 2 ▶ die "fork failed: $!" if (not defined $pid);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pid 0 2 2
133  
134 # hello child
135 2 ▶ if ($pid == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pid == 0) { } 1 1 2
136  
137 # don't leave creating output dir to later workers (Installer/PRC)
138 1 $self->makedir($self->cfg->{paths}{output_dir}."/$id");
139  
140 1 my $child = Tapper::MCP::Child->new( $id );
141 1 my $retval;
142 1 eval {
143 1 $retval = $child->runtest_handling( $revive );
144 };
145 1 ▶ $retval = $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 1 1
146  
147 1 $self->notify_event('testrun_finished', {testrun_id => $id});
148 1 $child->testrun_post_process();
149 1 $self->cleanup_output_dir($id);
150 1 ▶ if ( ($retval or $child->rerun) and $job->testrun->rerun_on_error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($retval || $child->rerun and $job->testrun->rerun_on_error) 1 0 1
Condition33%2/6
$retval || $child->rerun and $job->testrun->rerun_on_error
!ll&&!rl&&r
001
$retval or $child->rerun
l!l&&r!l&&!r
010
MC/DC0%0/3
$retval || $child->rerun and $job->testrun->rerun_on_error
$retval $child->rerun $job->testrun->rerun_on_error
Truth table20%1/5
A or B and C
A= $retvalB= $child->rerunC= $job->testrun->rerun_on_error
ABCresult
00X0
1X00
0100
1X11
0111
151 1 my $cmd = Tapper::Cmd::Testrun->new();
152 1 my $new_id;
153 1 eval {
154 1 my $or_new_testrun = $cmd->rerun($id, {rerun_on_error => $job->testrun->rerun_on_error - 1});
155 1 $new_id = $or_new_testrun->id;
156 };
157 1 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 1 0 1
158 1 $self->log->error($@);
159 } else {
160 0 $self->log->debug("Restarted testrun $id with new id $new_id because ".
161 "an error occurred and rerun_on_error was ".
162 $job->testrun->rerun_on_error);
163 }
164 }
165 1 ▶ if ($retval) {
Branch: 1 branch
BranchTrueFalseTotal
if ($retval) { } 0 1 1
166 0 $self->log->error("Testrun $id ($system) error occurred: $retval");
167 } else {
168 1 $self->log->debug("Testrun $id (pid $$) on host '$system' finished successfully");
169 }
170 1 exit 0;
171 } else {
172  
173 1 $self->child->{"$pid"}->{test_run} = $id;
174 1 $self->child->{"$pid"}->{job} = $job;
175 }
176 1 return 0;
177  
178 }
179  
180  
181  
182 sub runloop
183 2 ▶ {
Subroutine runloop: called CC=4 SCAR=14.2
Pod documented
184 2 my ($self, $lastrun) = @_;
185 2 my $timeout = $lastrun + $self->cfg->{times}{poll_intervall} - time();
186 2 ▶ $timeout = 0 if $timeout < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $timeout < 0 0 2 2
187  
188 2 sleep $timeout;
189 2 ▶ $self->handle_dead_children() if $self->dead_child;
Branch: 1 branch
BranchTrueFalseTotal
if $self->dead_child 0 2 2
190  
191 2 my @jobs = $self->scheduler->get_next_job;
192 2 foreach my $job (@jobs) {
193 0 $self->run_due_tests($job);
194 }
195  
196 2 $lastrun = time();
197 2 return $lastrun;
198 }
199  
200  
201  
202 sub prepare_server
203 2 ▶ {
Subroutine prepare_server: called CC=1 SCAR=0.0
Pod documented
204 2 my ($self) = @_;
205 2 Log::Log4perl->init($self->cfg->{files}{log4perl_cfg});
206  
207 2 return 0;
208 }
209  
210  
211 sub revive_children
212 0 ▶{
Subroutine revive_children: not called CC=2 SCAR=17.9
Pod documented
213 0 my ($self) = @_;
214 0 my $jobs = model->resultset('TestrunScheduling')->running;
215 0 foreach my $job ($jobs->all) {
216 0 $self->run_due_tests($job, "revive");
217 }
218 }
219  
220  
221  
222  
223 sub run
224 0 ▶ {
Subroutine run: not called CC=1 SCAR=6.9
Pod documented
225 0 my ($self) = @_;
226 0 $self->set_interrupt_handlers();
227 0 $self->prepare_server();
228 0 $self->revive_children();
229 0 my $lastrun = time();
230 0 while (1) {
231 0 $lastrun = $self->runloop($lastrun);
232 }
233  
234 }
235  
236 7 1;
237  
238 __END__
« MCP/Net/Reset/PM211MIP.pm ↑ Summary MCP/State/Details.pm »