blib/lib/Ukigumo/Agent/Manager.pm

filter: Statementstmt 77.1% 101 / 131 Branchbran 50.0% 12 / 24 Conditioncond 38.8% 7 / 18 MC/DCmcdc 20.0% 2 / 10 Subroutinesub 82.8% 29 / 35 Podpod 0.0% 0 / 5 totaltotal 67.7% 151 / 223 SCAR 35.2
CC
21
Cov
69%
__ANON__
42.0
run_job
20.9
__ANON__
6.0
CRAP 33.7
« blib/lib/Ukigumo/Agent/Dispatcher.pm ↑ Summary blib/lib/Ukigumo/Agent.pm »
1 package Ukigumo::Agent::Manager;
2 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 7 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 7 ▶use Ukigumo::Agent::Cleaner qw/cleanup_old_branch_dir/;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 7 ▶use Ukigumo::Client;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 7 ▶use Ukigumo::Client::VC::Git;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 7 ▶use Ukigumo::Client::Executor::Perl;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 7 ▶use Ukigumo::Helper qw/normalize_path/;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 7 ▶use Ukigumo::Logger;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 7 ▶use Coro;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 7 ▶use Coro::AnyEvent;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 7 ▶use POSIX qw/SIGTERM SIGKILL/;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 7 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 7 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 7 ▶use Mouse;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 has 'config' => (
20 is => 'rw',
21 isa => 'HashRef',
22 lazy => 1,
23 0 ▶ default => sub { +{} },
Subroutine __ANON__: not called CC=1 SCAR=6.9
24 7 );
25  
26 has 'work_dir' => (
27 is => 'ro',
28 isa => 'Str',
29 lazy => 1,
30 5 ▶ default => sub { shift->config->{work_dir} // File::Spec->tmpdir },
Subroutine __ANON__: called CC=2 SCAR=9.2
Condition33%1/3
+(shift())->config->{"work_dir"} // "File::Spec"->tmpdir
l!l&&r!l&&!r
500
MC/DC0%0/2
+(shift())->config->{"work_dir"} // "File::Spec"->tmpdir
+(shift())->config->{"work_dir"} "File::Spec"->tmpdir
Truth table33%1/3
A // B
A= +(shift())->config->{"work_dir"}B= "File::Spec"->tmpdir
ABresult
1X1
011
000
31 7 );
32  
33 has 'server_url' => (
34 is => 'ro',
35 isa => 'Str',
36 lazy => 1,
37 5 ▶ default => sub { shift->config->{server_url} },
Subroutine __ANON__: called CC=1 SCAR=0.0
38 7 );
39  
40 has 'timeout' => (
41 is => 'ro',
42 isa => 'Int',
43 lazy => 1,
44 3 ▶ default => sub { shift->config->{timeout} // 0 },
Subroutine __ANON__: called CC=2 SCAR=6.9
Condition100%2/2
+(shift())->config->{"timeout"} // 0
l!l
12
MC/DC100%1/1
+(shift())->config->{"timeout"} // 0
+(shift())->config->{"timeout"}
Truth table100%2/2
A
A= +(shift())->config->{"timeout"}
Aresult
11
00
45 7 );
46  
47 has 'ignore_github_tags' => (
48 is => 'ro',
49 isa => 'Bool',
50 lazy => 1,
51 0 ▶ default => sub { shift->config->{ignore_github_tags} // 0 },
Subroutine __ANON__: not called CC=2 SCAR=17.9
Condition0%0/2
+(shift())->config->{"ignore_github_tags"} // 0
l!l
00
MC/DC0%0/1
+(shift())->config->{"ignore_github_tags"} // 0
+(shift())->config->{"ignore_github_tags"}
Truth table0%0/2
A
A= +(shift())->config->{"ignore_github_tags"}
Aresult
11
00
52 7 );
53  
54 has 'force_git_url' => (
55 is => 'ro',
56 isa => 'Bool',
57 lazy => 1,
58 0 ▶ default => sub { shift->config->{force_git_url} // 0 },
Subroutine __ANON__: not called CC=2 SCAR=17.9
Condition0%0/2
+(shift())->config->{"force_git_url"} // 0
l!l
00
MC/DC0%0/1
+(shift())->config->{"force_git_url"} // 0
+(shift())->config->{"force_git_url"}
Truth table0%0/2
A
A= +(shift())->config->{"force_git_url"}
Aresult
11
00
59 7 );
60  
61 has 'max_children' => (
62 is => 'ro',
63 isa => 'Int',
64 lazy => 1,
65 7 ▶ default => sub { shift->config->{max_children} // 1 },
Subroutine __ANON__: called CC=2 SCAR=6.9
Condition100%2/2
+(shift())->config->{"max_children"} // 1
l!l
52
MC/DC100%1/1
+(shift())->config->{"max_children"} // 1
+(shift())->config->{"max_children"}
Truth table100%2/2
A
A= +(shift())->config->{"max_children"}
Aresult
11
00
66 7 );
67  
68 has 'cleanup_cycle' => (
69 is => 'ro',
70 isa => 'Int',
71 lazy => 1,
72 2 ▶ default => sub { shift->config->{cleanup_cycle} || 0 },
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
+(shift())->config->{"cleanup_cycle"} or 0 2 0 2
73 7 );
74  
75 has 'job_queue' => (
76 is => 'ro',
77 isa => 'ArrayRef',
78 8 ▶ default => sub { +[] },
Subroutine __ANON__: called CC=1 SCAR=0.0
79 7 );
80  
81 has 'children' => (
82 is => 'rw',
83 isa => 'HashRef',
84 8 ▶ default => sub { +{} },
Subroutine __ANON__: called CC=1 SCAR=0.0
85 7 );
86  
87 has 'logger' => (
88 is => 'ro',
89 isa => 'Ukigumo::Logger',
90 lazy => 1,
91 5 ▶ default => sub { Ukigumo::Logger->new },
Subroutine __ANON__: called CC=1 SCAR=0.0
92 7 );
93  
94 7 ▶no Mouse;
Subroutine BEGIN: called CC=1 SCAR=0.0
95  
96 sub count_children {
97 17 ▶ my $self = shift;
Subroutine count_children: called CC=1 SCAR=0.0
Pod undocumented
98 17 0+(keys %{$self->children});
99 }
100  
101 sub push_job {
102 7 ▶ my ($self, $job) = @_;
Subroutine push_job: called CC=1 SCAR=0.0
Pod undocumented
103 7 push @{$self->{job_queue}}, $job;
104 }
105  
106 sub pop_job {
107 0 ▶ my ($self, $job) = @_;
Subroutine pop_job: not called CC=1 SCAR=6.9
Pod undocumented
108 0 pop @{$self->{job_queue}};
109 }
110  
111 sub run_job {
112 5 ▶ my ($self, $args) = @_;
Subroutine run_job: called CC=9 SCAR=30.4
Pod undocumented
113 5 ▶ Carp::croak("Missing args") unless $args;
Branch: 1 branch
BranchTrueFalseTotal
unless $args 0 5 5
114  
115 5 ▶ my $repository = $args->{repository} || die;
Condition50%1/2
$args->{'repository'} || die()
l!l
50
MC/DC0%0/1
$args->{'repository'} || die()
$args->{'repository'}
Truth table50%1/2
A
A= $args->{'repository'}
Aresult
11
00
116 5 ▶ my $branch = $args->{branch} || die;
Condition50%1/2
$args->{'branch'} || die()
l!l
50
MC/DC0%0/1
$args->{'branch'} || die()
$args->{'branch'}
Truth table50%1/2
A
A= $args->{'branch'}
Aresult
11
00
117  
118 5 my $vc = Ukigumo::Client::VC::Git->new(
119 branch => $branch,
120 repository => $repository,
121 );
122 my $client = Ukigumo::Client->new(
123 workdir => $self->work_dir,
124 vc => $vc,
125 executor => Ukigumo::Client::Executor::Perl->new(),
126 server_url => $self->server_url,
127 compare_url => $args->{compare_url},
128 repository_owner => $args->{repository_owner},
129 repository_name => $args->{repository_name},
130 5 );
131  
132 5 my $client_log_filename = $client->logfh->filename;
133  
134 5 my $timeout_timer;
135  
136 5 my $pid = fork();
137 5 ▶ if (!defined $pid) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $pid) 0 5 5
138 0 die "Cannot fork: $!";
139 }
140  
141 5 ▶ if ($pid) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pid) { } 3 2 5
142 3 $self->logger->infof("Spawned $pid");
143 $self->{children}->{$pid} = +{
144 child => AE::child($pid, unblock_sub {
145 0 ▶ my ($pid, $status) = @_;
Subroutine __ANON__: not called CC=6 SCAR=37.4
146  
147 0 undef $timeout_timer;
148  
149 # Process has terminated because it was timeout
150 0 ▶ if ($status == SIGTERM) {
Branch: 1 branch
BranchTrueFalseTotal
if ($status == 15) 0 0 0
151 0 Coro::AnyEvent::sleep 5;
152 0 ▶ if (kill 0, $pid) {
Branch: 1 branch
BranchTrueFalseTotal
if (kill 0, $pid) 0 0 0
153 # Process is still alive
154 0 kill SIGTERM, $pid;
155 0 Coro::AnyEvent::sleep 5;
156 0 ▶ if (kill 0, $pid) {
Branch: 1 branch
BranchTrueFalseTotal
if (kill 0, $pid) 0 0 0
157 # The last resort
158 0 kill SIGKILL, $pid;
159 }
160 }
161 0 $self->logger->warnf("[child] timeout");
162 0 eval { $client->report_timeout($client_log_filename) };
163 }
164  
165 0 $self->logger->infof("[child exit] pid: $pid, status: $status");
166 0 delete $self->{children}->{$pid};
167  
168 0 ▶ if ($self->count_children < $self->max_children && @{$self->job_queue} > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->count_children < $self->max_children and @{$self->job_queue;} > 0) { } 0 0 0
Condition0%0/3
$self->count_children < $self->max_children and @{$self->job_queue;} > 0
!ll&&!rl&&r
000
MC/DC0%0/2
$self->count_children < $self->max_children and @{$self->job_queue;} > 0
$self->count_children < $self->max_children @{$self->job_queue;} > 0
Truth table0%0/3
A and B
A= $self->count_children < $self->max_childrenB= @{$self->job_queue;} > 0
ABresult
0X0
100
111
169 0 $self->logger->infof("[child exit] run new job");
170 0 $self->run_job($self->pop_job);
171 } else {
172 0 $self->_take_a_break();
173 }
174 3 }),
175 job => $args,
176 start => time(),
177 };
178 3 my $timeout = $self->timeout;
179 3 ▶ if ($timeout > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($timeout > 0) 1 2 3
180 $timeout_timer = AE::timer $timeout, 0, sub {
181 1 ▶ kill SIGTERM, $pid;
Subroutine __ANON__: called CC=1 SCAR=0.0
182 1 };
183 }
184 } else {
185 2 eval { $client->run() };
186 2 ▶ $self->logger->warnf("[child] error: $@") if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 1 1 2
187  
188 2 ▶ if (my $cleanup_cycle = $self->cleanup_cycle) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $cleanup_cycle = $self->cleanup_cycle) 0 2 2
189 0 my $project_dir = File::Spec->catfile($client->workdir, normalize_path($client->project));
190 0 cleanup_old_branch_dir($project_dir, $cleanup_cycle);
191 }
192  
193 2 $self->logger->infof("[child] finished to work");
194 2 exit;
195 }
196 }
197  
198 sub register_job {
199 17 ▶ my ($self, $params) = @_;
Subroutine register_job: called CC=2 SCAR=6.9
Pod undocumented
200  
201 17 ▶ if ($self->count_children < $self->max_children) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->count_children < $self->max_children) { } 10 7 17
202 # run job.
203 10 $self->run_job($params);
204 } else {
205 7 $self->push_job($params);
206 }
207 }
208  
209 sub _take_a_break {
210 0 ▶ my ($self) = @_;
Subroutine _take_a_break: not called CC=1 SCAR=6.9
211 0 $self->logger->infof("[child exit] There is no jobs. sleep...");
212 }
213  
214 7 1;
« blib/lib/Ukigumo/Agent/Dispatcher.pm ↑ Summary blib/lib/Ukigumo/Agent.pm »