lib/ControlFreak/Service.pm

filter: Statementstmt 80.3% 347 / 432 Branchbran 57.9% 95 / 164 Conditioncond 55.3% 36 / 65 MC/DCmcdc 18.9% 7 / 37 Subroutinesub 77.0% 67 / 87 Podpod 58.3% 28 / 48 totaltotal 69.6% 580 / 833 CC 111 SCAR 17.4
CC
111
Cov
72%
_check_stopping_state
20.0
proxy_as_text
20.0
setup_environment
20.0
CRAP 5.7
« lib/ControlFreak/Proxy.pm ↑ Summary lib/ControlFreak/Logger.pm »
1 package ControlFreak::Service;
2 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 8 ▶use AnyEvent '5.202';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 8 ▶use AnyEvent::Util();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 8 ▶use AnyEvent::Handle();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 8 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 8 ▶use ControlFreak::Util();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 8 ▶use Data::Dumper();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 8 ▶use JSON::XS;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 8 ▶use Params::Util qw{ _NUMBER _STRING _IDENTIFIER _ARRAY _POSINT };
Subroutine BEGIN: called CC=1 SCAR=0.0
13 8 ▶use POSIX qw{ SIGTERM SIGKILL };
Subroutine BEGIN: called CC=1 SCAR=0.0
14 8 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 8 ▶use constant DEFAULT_STARTWAIT_SECS => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 8 ▶use constant DEFAULT_STOPWAIT_SECS => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 8 ▶use constant DEFAULT_MAX_RETRIES => 8;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 8 ▶use constant BASE_BACKOFF_DELAY => 0.3;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 8 ▶use Object::Tiny qw{
Subroutine BEGIN: called CC=1 SCAR=0.0
22 name
23 desc
24 proxy
25  
26 state
27 pid
28 start_time
29 stop_time
30 running_cmd
31  
32 cmd
33 env
34 cwd
35 tags
36 tie_stdin_to
37 ignore_stderr
38 ignore_stdout
39 startwait_secs
40 stopwait_secs
41 respawn_on_fail
42 respawn_on_stop
43 respawn_max_retries
44 no_new_session
45 user
46 group
47 priority
48 8 };
49  
50 =pod
51  
52 =head1 NAME
53  
54 ControlFreak::Service - Object representation of a service.
55  
56 =head1 SYNOPSIS
57  
58 my $mc = ControlFreak::Service->new(
59 name => "memcached",
60 desc => "you should have this one...",
61 ignore_stderr => 1,
62 cmd => "/usr/bin/memcached",
63 );
64  
65 my $fcgisock = $ctrl->socketmap->{fcgi};
66 my $web = ControlFreak::Service->new(
67 name => "fcgi",
68 desc => "I talk http",
69 tie_stdin_to => $fcgisock,
70 cmd => "/usr/bin/plackup -a MyApp -s FCGI",
71 );
72 $web->up;
73 $web->start;
74 $web->stop;
75  
76 ## A service can mutate
77 $web->add_tag('prod');
78  
79 ## all set_* accessors are callable from Commands
80 $web->set_cmd("/usr/bin/plackup -a MyNewApp");
81 $web->set_ignore_stderr(0);
82 # ...
83  
84 $web->running_cmd;
85  
86 # make the service a proxied service
87 $web->assign_proxy($proxy);
88  
89 =head1 DESCRIPTION
90  
91 This allows manipulation of a service and its state.
92  
93 =head1 METHODS
94  
95 =head2 new(%param)
96  
97 constructor.
98  
99 =cut
100  
101 26 ▶sub new {
Subroutine new: called CC=9 SCAR=22.8
Pod documented
102 26 my $svc = shift->SUPER::new(@_);
103 26 my %param = @_;
104  
105 ## validate the service name
106 26 ▶ unless (_IDENTIFIER($svc->name)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (&_IDENTIFIER(scalar $svc->name)) 2 24 26
107 2 return;
108 }
109  
110 ## sensible default
111 $svc->{respawn_on_fail} = 1
112 24 ▶ unless exists $svc->{respawn_on_fail};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $svc->{"respawn_on_fail"} 24 0 24
113  
114 $svc->{respawn_max_retries} = DEFAULT_MAX_RETRIES
115 24 ▶ unless defined $svc->{respawn_max_retries};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $svc->{"respawn_max_retries"} 24 0 24
116  
117 $svc->{startwait_secs} = DEFAULT_STARTWAIT_SECS
118 24 ▶ unless defined $svc->{startwait_secs};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $svc->{"startwait_secs"} 23 1 24
119  
120 $svc->{stopwait_secs} = DEFAULT_STOPWAIT_SECS
121 24 ▶ unless defined $svc->{stopwait_secs};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $svc->{"stopwait_secs"} 24 0 24
122  
123 $svc->{ctrl} = $param{ctrl}
124 24 ▶ or croak "Service requires a controller";
Branch: 1 branch
BranchTrueFalseTotal
$svc->{"ctrl"} = $param{"ctrl"} or croak("Service requires a controller") 24 0 24
125  
126 24 ▶ $svc->{tags} ||= {};
Condition50%1/2
$svc->{"tags"} ||= {}
l!l
024
MC/DC0%0/1
$svc->{"tags"} ||= {}
$svc->{"tags"}
Truth table50%1/2
A
A= $svc->{"tags"}
Aresult
11
00
127 24 ▶ $svc->{env} ||= {};
Condition50%1/2
$svc->{"env"} ||= {}
l!l
024
MC/DC0%0/1
$svc->{"env"} ||= {}
$svc->{"env"}
Truth table50%1/2
A
A= $svc->{"env"}
Aresult
11
00
128  
129 24 return $svc;
130 }
131  
132 2 ▶sub _err { ControlFreak::Util::error(@_) }
Subroutine _err: called CC=1 SCAR=0.0
133  
134 =head2 is_fail
135  
136 Returns true if the state is 'failed'
137  
138 =cut
139  
140 61 ▶sub is_fail {
Subroutine is_fail: called CC=2 SCAR=7.2
Pod documented
141 61 ▶ my $state = shift->state || "";
Condition50%1/2
(shift())->state || ''
l!l
610
MC/DC0%0/1
(shift())->state || ''
(shift())->state
Truth table50%1/2
A
A= (shift())->state
Aresult
11
00
142 61 return $state eq 'fail';
143 }
144  
145 =head2 is_backoff
146  
147 Returns true if the state is 'backoff'
148  
149 =cut
150  
151 142 ▶sub is_backoff {
Subroutine is_backoff: called CC=2 SCAR=6.9
Pod documented
152 142 ▶ my $state = shift->state || "";
Condition100%2/2
(shift())->state || ''
l!l
1402
MC/DC100%1/1
(shift())->state || ''
(shift())->state
Truth table100%2/2
A
A= (shift())->state
Aresult
11
00
153 142 return $state eq 'backoff';
154 }
155  
156 =head2 is_fatal
157  
158 Returns true if the state is 'fatal'
159  
160 =cut
161  
162 25 ▶sub is_fatal {
Subroutine is_fatal: called CC=2 SCAR=7.2
Pod documented
163 25 ▶ my $state = shift->state || "";
Condition50%1/2
(shift())->state || ''
l!l
250
MC/DC0%0/1
(shift())->state || ''
(shift())->state
Truth table50%1/2
A
A= (shift())->state
Aresult
11
00
164 25 return $state eq 'fatal';
165 }
166  
167 =head2 is_running
168  
169 Returns true if the state is 'runnnig'
170  
171 =cut
172  
173 53 ▶sub is_running {
Subroutine is_running: called CC=2 SCAR=7.2
Pod documented
174 53 ▶ my $state = shift->state || "";
Condition50%1/2
(shift())->state || ''
l!l
530
MC/DC0%0/1
(shift())->state || ''
(shift())->state
Truth table50%1/2
A
A= (shift())->state
Aresult
11
00
175 53 return $state eq 'running';
176 }
177  
178 =head2 is_starting
179  
180 Returns true if the state is 'starting'
181  
182 =cut
183  
184 72 ▶sub is_starting {
Subroutine is_starting: called CC=2 SCAR=7.2
Pod documented
185 72 ▶ my $state = shift->state || "";
Condition50%1/2
(shift())->state || ''
l!l
720
MC/DC0%0/1
(shift())->state || ''
(shift())->state
Truth table50%1/2
A
A= (shift())->state
Aresult
11
00
186 72 return $state eq 'starting';
187 }
188  
189 =head2 is_stopping
190  
191 Returns true if the state is 'stopping'
192  
193 =cut
194  
195 3 ▶sub is_stopping {
Subroutine is_stopping: called CC=2 SCAR=7.2
Pod documented
196 3 ▶ my $state = shift->state || "";
Condition50%1/2
(shift())->state || ''
l!l
30
MC/DC0%0/1
(shift())->state || ''
(shift())->state
Truth table50%1/2
A
A= (shift())->state
Aresult
11
00
197 3 return $state eq 'stopping';
198 }
199  
200 =head2 is_stopped
201  
202 Returns true is service is stopped
203  
204 =cut
205  
206 49 ▶sub is_stopped {
Subroutine is_stopped: called CC=2 SCAR=7.2
Pod documented
207 49 ▶ my $state = shift->state || "";
Condition50%1/2
(shift())->state || ''
l!l
490
MC/DC0%0/1
(shift())->state || ''
(shift())->state
Truth table50%1/2
A
A= (shift())->state
Aresult
11
00
208 49 return $state eq 'stopped';
209 }
210  
211 =head2 is_up
212  
213 Returns true is service is up
214  
215 =cut
216  
217 86 ▶sub is_up {
Subroutine is_up: called CC=6 SCAR=18.0
Pod documented
218 86 my $svc = shift;
219 86 ▶ my $state = $svc->state || "";
Condition100%2/2
$svc->state || ''
l!l
833
MC/DC100%1/1
$svc->state || ''
$svc->state
Truth table100%2/2
A
A= $svc->state
Aresult
11
00
220 86 ▶ return 0 unless $state =~ /^(?:running|starting|stopping)$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $state =~ /^(?:running|starting|stopping)$/ 63 23 86
221  
222 23 ▶ unless ($svc->{proxy}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($svc->{"proxy"}) 20 3 23
223 ## just in case, verify...
224 20 ▶ return 0 unless defined $svc->{child_cv};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $svc->{"child_cv"} 0 20 20
225 20 ▶ return 0 unless defined $svc->pid;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $svc->pid 0 20 20
226 }
227 23 return 1;
228 }
229  
230 =head2 is_down
231  
232 Returns true unless service is up
233  
234 =cut
235  
236 37 ▶sub is_down {
Subroutine is_down: called CC=1 SCAR=0.0
Pod documented
237 37 return !shift->is_up;
238 }
239  
240 =head2 fail_reason
241  
242 Returns a string with the reason of the failure, or undef.
243  
244 =cut
245  
246 7 ▶sub fail_reason {
Subroutine fail_reason: called CC=2 SCAR=6.9
Pod documented
247 7 my $svc = shift;
248 7 ▶ return unless $svc->is_fail;
Branch: 1 branch
BranchTrueFalseTotal
unless $svc->is_fail 1 6 7
249 6 return ControlFreak::Util::exit_reason( $svc->{exit_status} );
250 }
251  
252 =head2 stop(%param)
253  
254 Initiates service shutdown.
255  
256 params are:
257  
258 =over 4
259  
260 =item * ok_cb
261  
262 A callback called when shutdown has been initiated successfuly. Note that it
263 doesn't mean that the service is successfuly stopped, just that nothing
264 prevented the shutdown sequence.
265  
266 Optional.
267  
268 =item * err_cb
269  
270 Called with a text reason when the stop request couldn't be initiated properly.
271  
272 Optional.
273  
274 =back
275  
276 =cut
277  
278 15 ▶sub stop {
Subroutine stop: called CC=12 SCAR=26.6
Pod documented
279 15 my $svc = shift;
280 15 my %param = @_;
281 15 ▶ my $err = $param{err_cb} ||= sub {};
Subroutine __ANON__: not called CC=1 SCAR=0.2
Condition66%2/3
$param{"err_cb"} ||= sub { }
l!l&&r!l&&!r
2130
MC/DC0%0/2
$param{"err_cb"} ||= sub { }
$param{"err_cb"} sub { }
Truth table66%2/3
A ||= B
A= $param{"err_cb"}B= sub { }
ABresult
1X1
011
000
282 15 ▶ my $ok = $param{ok_cb} ||= sub {};
Subroutine __ANON__: called CC=1 SCAR=0.2
Condition66%2/3
$param{"ok_cb"} ||= sub { }
l!l&&r!l&&!r
2130
MC/DC0%0/2
$param{"ok_cb"} ||= sub { }
$param{"ok_cb"} sub { }
Truth table66%2/3
A ||= B
A= $param{"ok_cb"}B= sub { }
ABresult
1X1
011
000
283  
284 15 ▶ my $svcname = $svc->name || "unnamed service";
Condition50%1/2
$svc->name || 'unnamed service'
l!l
150
MC/DC0%0/1
$svc->name || 'unnamed service'
$svc->name
Truth table50%1/2
A
A= $svc->name
Aresult
11
00
285  
286 15 ▶ if ($svc->is_backoff) {
Branch: 1 branch
BranchTrueFalseTotal
if ($svc->is_backoff) 1 14 15
287 ## stop retrying.
288 1 $svc->{backoff_cv} = undef;
289 1 $svc->{start_cv} = undef;
290 1 $svc->{backoff_retry} = undef;
291 1 $svc->{state} = 'stopped';
292 1 $svc->{wants_down} = 1;
293 1 $svc->{stop_time} = time;
294 1 return;
295 }
296  
297 14 ▶ return $svc->_err(%param, "Service '$svcname' is already down")
Branch: 1 branch
BranchTrueFalseTotal
if $svc->is_down 1 13 14
298 if $svc->is_down;
299  
300 13 ▶ return $svc->_err(%param, "Service '$svcname' lost its pid")
Branch: 1 branch
BranchTrueFalseTotal
unless $svc->pid 0 13 13
301 unless $svc->pid;
302  
303 13 $svc->{ctrl}->log->info("Stopping service '$svcname'");
304  
305 ## there is a slight race condition here, since the child
306 ## might have died just before we send the TERM signal, but
307 ## we trust the kernel not to reallocate the pid in the meantime
308 13 $svc->{stop_time} = time;
309 13 $svc->{start_time} = undef;
310 13 $svc->{state} = 'stopping';
311 13 $svc->{wants_down} = 1;
312  
313 13 $svc->{on_stop_cb} = $param{on_stop};
314 13 my $stopwait_secs = $svc->stopwait_secs;
315 $svc->{stop_cv} =
316 0 ▶ AE::timer $stopwait_secs, 0, sub { $svc->_check_stopping_state };
Subroutine __ANON__: not called CC=1 SCAR=1.2
317  
318 13 ▶ if (my $proxy = $svc->{proxy}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $proxy = $svc->{'proxy'}) { } 1 12 13
319 1 $proxy->stop_service(%param, service => $svc);
320 }
321 else {
322 12 my $pid = $svc->pid;
323 12 ▶ if (! $pid) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $pid) { } 0 12 12
324 0 my $msg = "Please retry in a bit, no pid yet";
325 0 ▶ if (! $svc->is_starting) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($svc->is_starting) 0 0 0
326 0 $msg = "Something weird is going on. pid missing";
327 }
328 0 return $svc->_err(%param, $msg);
329 }
330 else {
331 ## check that we've created a session
332 12 my $has_new_session = !$svc->no_new_session;
333 12 ▶ if ($has_new_session) {
Branch: 1 branch
BranchTrueFalseTotal
if ($has_new_session) { } 12 0 12
334 12 ▶ if (getpgrp($pid) == getpgrp(0)) {
Branch: 1 branch
BranchTrueFalseTotal
if (getpgrp $pid == getpgrp 0) { } 0 12 12
335 ## don't commit suicide, thank you.
336 0 kill SIGTERM, $pid;
337 }
338 else {
339 12 kill -(SIGTERM), getpgrp($pid);
340 }
341 }
342 else {
343 ## ok, only kill that one pid
344 0 kill SIGTERM, $pid;
345 }
346 }
347 }
348 13 $ok->();
349 13 return 1;
350 }
351  
352 =head2 start(%param)
353  
354 Initiates service startup and returns immediately.
355  
356 params are:
357  
358 =over 4
359  
360 =item * ok_cb
361  
362 A callback called when startup has been initiated successfuly. Note that it
363 doesn't mean that the service is successfuly running, just that nothing
364 prevented the startup.
365  
366 Optional.
367  
368 =item * err_cb
369  
370 A callback called when an error occured during startup (For instance
371 if the service is already started), the reason for the failure is
372 passed as the first argument of the callback if is known.
373  
374 Optional.
375  
376 =back
377  
378 =cut
379  
380 41 ▶sub start {
Subroutine start: called CC=8 SCAR=20.9
Pod documented
381 41 my $svc = shift;
382 41 my %param = @_;
383 41 ▶ my $err = $param{err_cb} ||= sub {};
Subroutine __ANON__: not called CC=1 SCAR=0.2
Condition66%2/3
$param{"err_cb"} ||= sub { }
l!l&&r!l&&!r
3380
MC/DC0%0/2
$param{"err_cb"} ||= sub { }
$param{"err_cb"} sub { }
Truth table66%2/3
A ||= B
A= $param{"err_cb"}B= sub { }
ABresult
1X1
011
000
384 41 ▶ my $ok = $param{ok_cb} ||= sub {};
Subroutine __ANON__: called CC=1 SCAR=0.2
Condition66%2/3
$param{"ok_cb"} ||= sub { }
l!l&&r!l&&!r
2390
MC/DC0%0/2
$param{"ok_cb"} ||= sub { }
$param{"ok_cb"} sub { }
Truth table66%2/3
A ||= B
A= $param{"ok_cb"}B= sub { }
ABresult
1X1
011
000
385  
386 41 ▶ my $svcname = $svc->name || "unnamed service";
Condition50%1/2
$svc->name || 'unnamed service'
l!l
410
MC/DC0%0/1
$svc->name || 'unnamed service'
$svc->name
Truth table50%1/2
A
A= $svc->name
Aresult
11
00
387  
388 41 ▶ return $svc->_err(%param, "Service '$svcname' is already up")
Branch: 1 branch
BranchTrueFalseTotal
if $svc->is_up 0 41 41
389 if $svc->is_up;
390  
391 41 my $cmd = $svc->cmd;
392 41 ▶ return $svc->_err(%param, "Service '$svcname' has no known command")
Branch: 1 branch
BranchTrueFalseTotal
unless $cmd 1 40 41
393 unless $cmd;
394  
395 40 $svc->{restart_cv} = undef;
396 40 $svc->{start_time} = time;
397 40 $svc->{stop_time} = undef;
398 40 $svc->{wants_down} = undef;
399 40 $svc->{normal_exit} = undef;
400 40 ▶ $svc->{backoff_retry} = undef unless $svc->is_backoff;
Branch: 1 branch
BranchTrueFalseTotal
unless $svc->is_backoff 31 9 40
401 40 $svc->{state} = 'starting';
402  
403 40 $svc->set_check_running_state_timer;
404 40 ▶ if (my $proxy = $svc->{proxy}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $proxy = $svc->{'proxy'}) { } 5 35 40
405 5 $proxy->start_service(%param, service => $svc);
406 }
407 else {
408 35 $svc->_run_cmd;
409 }
410  
411 40 $ok->();
412 40 return 1;
413 }
414  
415 ## set a timer to verify service is up, the timer can be reset
416 ## a second time by the proxy when it gets the pid, it depends
417 ## which event happens first
418 44 ▶sub set_check_running_state_timer {
Subroutine set_check_running_state_timer: called CC=1 SCAR=0.0
Pod undocumented
419 44 my $svc = shift;
420 44 my $startwait_secs = $svc->startwait_secs;
421 44 $svc->{ctrl}->log->debug("setting timer for $startwait_secs");
422 $svc->{start_cv} =
423 44 ▶ AE::timer $startwait_secs, 0, sub { $svc->_check_running_state };
Subroutine __ANON__: called CC=1 SCAR=0.0
424 44 return;
425 }
426  
427 =head2 has_stopped($reason)
428  
429 Called when a third party knows that a service has stopped. It marks the
430 service has stopped, no matter what the current status is.
431  
432 =cut
433  
434 ## FIXME name
435 3 ▶sub has_stopped {
Subroutine has_stopped: called CC=3 SCAR=11.0
Pod documented
436 3 my $svc = shift;
437 3 ▶ my $reason = shift || "";
Condition50%1/2
shift() || ''
l!l
30
MC/DC0%0/1
shift() || ''
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
438 3 ▶ return if $svc->is_down;
Branch: 1 branch
BranchTrueFalseTotal
if $svc->is_down 2 1 3
439  
440 1 my $name = $svc->name;
441 1 $reason = "'$name' has stopped: $reason";
442 1 $svc->{state} = 'fail';
443 1 $svc->{stop_time} = time;
444 1 $svc->{normal_exit} = undef;
445 1 $svc->{child_cv} = undef;
446 1 $svc->{ctrl}->log->info($reason);
447 1 return 1;
448 }
449  
450 0 ▶sub _check_stopping_state {
Subroutine _check_stopping_state: not called CC=4 SCAR=30.0
451 0 my $svc = shift;
452 0 my $on_stop = $svc->{on_stop_cb};
453 0 $svc->{stop_cv} = undef;
454 0 $svc->{on_stop_cb} = undef;
455 0 ▶ if ($svc->is_stopped) {
Branch: 1 branch
BranchTrueFalseTotal
if ($svc->is_stopped) 0 0 0
456 0 ▶ $on_stop->($svc) if $on_stop;
Branch: 1 branch
BranchTrueFalseTotal
if $on_stop 0 0 0
457 0 return;
458 }
459  
460 0 my $wait = $svc->stopwait_secs;
461 0 my $name = $svc->name;
462 0 ▶ if ($svc->pid) {
Branch: 1 branch
BranchTrueFalseTotal
if ($svc->pid) { } 0 0 0
463 $svc->{ctrl}->log->warn(
464 0 "service $name still running after $wait, killing."
465 );
466 0 $svc->kill;
467 }
468 else {
469 0 $svc->{ctrl}->log->error( "service $name not stopped but, not pid?");
470 0 $svc->{state} = 'fail';
471 }
472 0 return;
473 }
474  
475 =head2 kill
476  
477 Kills the service. This is the brutal way of getting rid of service's process
478 it will result in the program being uncleanly exited which will be reported
479 later in the status of the service. This command is used when a service
480 hasn't terminated after C<stopwait_secs>.
481  
482 =cut
483  
484 0 ▶sub kill {
Subroutine kill: not called CC=2 SCAR=17.9
Pod documented
485 0 my $svc = shift;
486 0 my $pid = $svc->pid;
487 0 ▶ unless ($pid) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pid) 0 0 0
488 0 my $name = $svc->name;
489 0 $svc->{ctrl}->log->error( "cannot kill $name without pid" );
490 }
491 0 kill -(SIGKILL), getpgrp($pid);
492 }
493  
494 17 ▶sub _check_running_state {
Subroutine _check_running_state: called CC=4 SCAR=14.4
495 17 my $svc = shift;
496 17 $svc->{start_cv} = undef;
497 17 $svc->{ctrl}->log->debug("state is " . $svc->state);
498 17 ▶ return unless $svc->is_starting;
Branch: 1 branch
BranchTrueFalseTotal
unless $svc->is_starting 0 17 17
499 17 ▶ if (! $svc->pid) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($svc->pid) 1 16 17
500 1 ▶ if (my $proxy = $svc->{proxy}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $proxy = $svc->{"proxy"}) 1 0 1
501 $svc->{ctrl}->log->warn(
502 1 "increase startwait_secs, proxy didn't have time to start svc"
503 );
504 1 return;
505 }
506 0 $svc->{ctrl}->log->error("smth went terribly wrong");
507 0 $svc->{state} = 'fail';
508 0 return;
509 }
510 16 my $name = $svc->name;
511 16 $svc->{ctrl}->log->debug("Now setting '$name' service as running");
512 16 $svc->{state} = 'running';
513 16 $svc->{backoff_retry} = undef;
514 }
515  
516 8 ▶sub _backoff_restart {
Subroutine _backoff_restart: called CC=2 SCAR=7.0
517 8 my $svc = shift;
518 8 $svc->{backoff_cv} = undef;
519 8 ▶ return unless $svc->is_backoff;
Branch: 1 branch
BranchTrueFalseTotal
unless $svc->is_backoff 0 8 8
520 8 my $n = $svc->{backoff_retry} + 1;
521 8 my $s = $svc->name;
522 8 $svc->{ctrl}->log->info("restarting $s [attempt: $n]");
523 8 $svc->start;
524 8 return;
525 }
526  
527 12 ▶sub _exponential_backoff_delay {
Subroutine _exponential_backoff_delay: called CC=2 SCAR=7.0
528 12 my $svc = shift;
529 12 ▶ my $retry = shift || 1;
Condition50%1/2
shift() || 1
l!l
120
MC/DC0%0/1
shift() || 1
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
530 12 my $max_retries = $svc->respawn_max_retries;
531 12 my $factor = int(rand (2 * $retry - 1) + 1);
532 12 return $factor * BASE_BACKOFF_DELAY;
533 }
534  
535 =head2 up(%param)
536  
537 XXX up the service (do nothing if already up)
538  
539 =cut
540  
541 1 ▶sub up {
Subroutine up: called CC=2 SCAR=7.1
Pod documented
542 1 my $svc = shift;
543 1 ▶ return if $svc->is_up;
Branch: 1 branch
BranchTrueFalseTotal
if $svc->is_up 0 1 1
544 1 return $svc->start(@_);
545 }
546  
547 =head2 up(%param)
548  
549 XXX down the service (do nothing if already down)
550  
551 =cut
552  
553 2 ▶sub down {
Subroutine down: called CC=3 SCAR=11.0
Pod documented
554 2 my $svc = shift;
555 2 my %param = @_;
556 2 ▶ if ($svc->is_down) {
Branch: 1 branch
BranchTrueFalseTotal
if ($svc->is_down) 1 1 2
557 1 ▶ $param{on_stop}->() if $param{on_stop};
Branch: 1 branch
BranchTrueFalseTotal
if $param{"on_stop"} 0 1 1
558 1 return;
559 }
560 1 return $svc->stop(@_);
561 }
562  
563 =head2 restart(%param)
564  
565 Restarts the service. i.e. stops it (if up), then starts it.
566  
567 =cut
568  
569 2 ▶sub restart {
Subroutine restart: called CC=5 SCAR=17.7
Pod documented
570 2 my $svc = shift;
571 2 my %param = @_;
572 2 ▶ my $err = $param{err_cb} ||= sub {};
Subroutine __ANON__: not called CC=1 SCAR=1.2
Condition33%1/3
$param{"err_cb"} ||= sub { }
l!l&&r!l&&!r
200
MC/DC0%0/2
$param{"err_cb"} ||= sub { }
$param{"err_cb"} sub { }
Truth table33%1/3
A ||= B
A= $param{"err_cb"}B= sub { }
ABresult
1X1
011
000
573 2 ▶ my $ok = $param{ok_cb} ||= sub {};
Subroutine __ANON__: not called CC=1 SCAR=1.2
Condition33%1/3
$param{"ok_cb"} ||= sub { }
l!l&&r!l&&!r
200
MC/DC0%0/2
$param{"ok_cb"} ||= sub { }
$param{"ok_cb"} sub { }
Truth table33%1/3
A ||= B
A= $param{"ok_cb"}B= sub { }
ABresult
1X1
011
000
574 2 my $fail = 0;
575 0 ▶ $svc->down(%param, ok_cb => $ok, err_cb => sub { $fail++ });
Subroutine __ANON__: not called CC=1 SCAR=1.2
576 2 ▶ return $err->() if $fail;
Branch: 1 branch
BranchTrueFalseTotal
if $fail 0 2 2
577 2 ▶ my $stopwait_secs = $svc->stopwait_secs || DEFAULT_STARTWAIT_SECS;
Condition50%1/2
$svc->stopwait_secs || 1
l!l
20
MC/DC0%0/1
$svc->stopwait_secs || 1
$svc->stopwait_secs
Truth table50%1/2
A
A= $svc->stopwait_secs
Aresult
11
00
578 2 my $delay = $stopwait_secs / 10;
579 2 my $tries = 0;
580 1 ▶ $svc->{restart_cv} = AE::timer 0.15, $delay, sub {
Subroutine __ANON__: called CC=3 SCAR=12.3
581 1 $tries++;
582 1 ▶ if ($tries > 150) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tries > 150) 0 1 1
583 0 $err->();
584 0 return;
585 }
586 1 ▶ return if $svc->is_up;
Branch: 1 branch
BranchTrueFalseTotal
if $svc->is_up 0 1 1
587 1 $svc->{restart_cv} = undef;
588 1 return $svc->up(%param);
589 2 };
590 2 return;
591 }
592  
593 =head2 proxy_as_text
594  
595 A descriptive text representing service's proxy.
596  
597 =cut
598  
599 0 ▶sub proxy_as_text {
Subroutine proxy_as_text: not called CC=4 SCAR=30.0
Pod documented
600 0 my $svc = shift;
601 0 my $proxy = $svc->{proxy};
602 0 ▶ return "" unless $proxy;
Branch: 1 branch
BranchTrueFalseTotal
unless $proxy 0 0 0
603 0 ▶ my $name = $proxy->name || "";
Condition0%0/2
$proxy->name || ''
l!l
00
MC/DC0%0/1
$proxy->name || ''
$proxy->name
Truth table0%0/2
A
A= $proxy->name
Aresult
11
00
604 0 ▶ my $status = $proxy->is_running ? "" : "!";
Branch: 1 branch
BranchTrueFalseTotal
$proxy->is_running ? : 0 0 0
605 0 return "$name$status";
606 }
607  
608 =head2 status_as_text
609  
610 Returns a text describing the service state.
611 It consists in tab separated list of fields:
612  
613 =over 2
614  
615 =item * name
616  
617 =item * state
618  
619 =item * pid
620  
621 =item * start_time
622  
623 =item * stop_time
624  
625 =item * proxy, prefixed with '!' if down
626  
627 =item * fail_reason
628  
629 =item * running_cmd
630  
631 =back
632  
633 =cut
634  
635 0 ▶sub status_as_text {
Subroutine status_as_text: not called CC=2 SCAR=17.9
Pod documented
636 0 my $svc = shift;
637 0 ▶ return join "\t", map { $svc->$_ || "" }
Condition0%0/2
$svc->$_ || ''
l!l
00
MC/DC0%0/1
$svc->$_ || ''
$svc->$_
Truth table0%0/2
A
A= $svc->$_
Aresult
11
00
638 qw/name state pid start_time stop_time proxy_as_text
639 fail_reason running_cmd/;
640 }
641  
642 =head2 desc_as_text
643  
644 Returns a text describing the service and how to access it.
645 It consists in tab separated list of fields:
646  
647 =over 2
648  
649 =item * name
650  
651 =item * tags
652  
653 =item * desc
654  
655 =item * proxy
656  
657 =item * cmd
658  
659 =back
660  
661 =cut
662  
663 0 ▶sub desc_as_text {
Subroutine desc_as_text: not called CC=2 SCAR=17.9
Pod documented
664 0 my $svc = shift;
665 0 ▶ return join "\t", map { $svc->$_ || "" }
Condition0%0/2
$svc->$_ || ''
l!l
00
MC/DC0%0/1
$svc->$_ || ''
$svc->$_
Truth table0%0/2
A
A= $svc->$_
Aresult
11
00
666 qw/name tags_as_text desc proxy_as_text cmd/;
667 }
668  
669 =head2 assign_proxy($proxy)
670  
671 =cut
672  
673 3 ▶sub assign_proxy {
Subroutine assign_proxy: called CC=1 SCAR=0.0
Pod documented
674 3 my $svc = shift;
675 3 $svc->{proxy} = shift;
676 3 return 1;
677 }
678  
679 =head2 assign_pid($pid)
680  
681 =cut
682  
683 4 ▶sub assign_pid {
Subroutine assign_pid: called CC=1 SCAR=0.0
Pod documented
684 4 my $svc = shift;
685 4 my $pid = shift;
686 4 $svc->{pid} = $pid;
687 4 return;
688 }
689  
690 56 ▶sub _set {
Subroutine _set: called CC=6 SCAR=17.9
691 56 my $svc = shift;
692 56 my ($attr, $value) = @_;
693  
694 56 my $old = $svc->$attr;
695  
696 56 ▶ my $v = defined $value ? $value : "~";
Branch: 1 branch
BranchTrueFalseTotal
defined $value ? : 56 0 56
697 56 local $Data::Dumper::Indent = 0;
698 56 local $Data::Dumper::Terse = 1;
699 56 ▶ if (ref $v) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $v) 9 47 56
700 9 $v = Data::Dumper::Dumper($v);
701 }
702 56 ▶ if ($old) {
Branch: 1 branch
BranchTrueFalseTotal
if ($old) { } 30 26 56
703 30 ▶ my $oldv = defined $old ? $old : "~";
Branch: 1 branch
BranchTrueFalseTotal
defined $old ? : 30 0 30
704 30 ▶ $oldv = Data::Dumper::Dumper($oldv) if ref $oldv;
Branch: 1 branch
BranchTrueFalseTotal
if ref $oldv 5 25 30
705 30 $svc->{ctrl}->log->debug( "Changing $attr from '$oldv' to '$v'" );
706 }
707 else {
708 26 $svc->{ctrl}->log->debug( "Setting $attr to '$v'" );
709 }
710 56 $svc->{$attr} = $value;
711 56 return 1;
712 }
713  
714 5 ▶sub unset {
Subroutine unset: called CC=1 SCAR=0.0
Pod undocumented
715 5 my $svc = shift;
716 5 my $attr = shift;
717 5 $svc->{$attr} = undef;
718 5 return 1;
719 }
720  
721 =head2 tags
722  
723 Returns a hashref of tags
724  
725 =head2 tags_as_text
726  
727 Returns tag as a descriptive text.
728  
729 =head2 tag_list
730  
731 Returns a reference to a list of tags
732  
733 =cut
734  
735 0 ▶sub tags_as_text {
Subroutine tags_as_text: not called CC=1 SCAR=6.9
Pod documented
736 0 my $svc = shift;
737 0 return join ", ", @{ $svc->tag_list };
738  
739 }
740  
741 3 ▶sub tag_list {
Subroutine tag_list: called CC=1 SCAR=0.0
Pod documented
742 3 my $svc = shift;
743 3 return [ keys %{ $svc->tags } ];
744 }
745  
746 31 ▶sub set_cmd {
Subroutine set_cmd: called CC=3 SCAR=11.0
Pod undocumented
747 31 ▶ my $value = (ref $_[1] ? _ARRAY($_[1]) : _STRING($_[1])) or return;
Branch: 2 branches
BranchTrueFalseTotal
my $value = ref $_[1] ? &_ARRAY($_[1]) : &_STRING($_[1]) or return 28 3 31
ref $_[1] ? : 6 25 31
748 28 shift->_set('cmd', $value);
749 }
750  
751 14 ▶sub set_cmd_from_con {
Subroutine set_cmd_from_con: called CC=3 SCAR=11.0
Pod undocumented
752 14 my $svc = shift;
753 14 my $value = shift;
754 14 ▶ return $svc->unset('cmd') unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 5 9 14
755 9 ▶ if ($value =~ /^\[/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ /^\[/) 4 5 9
756 4 ▶ $value = try { decode_json($value) }
Subroutine __ANON__: called CC=1 SCAR=0.0
757 3 ▶ catch {
Subroutine __ANON__: called CC=1 SCAR=0.0
758 3 my $error = $_;
759 3 $svc->{ctrl}->log->error("Invalid JSON: $error");
760 3 return;
761 4 };
762 }
763 9 return $svc->set_cmd($value);
764 }
765  
766 0 ▶sub set_desc {
Subroutine set_desc: not called CC=2 SCAR=17.9
Pod undocumented
767 0 ▶ my $value = _STRING($_[1]) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $value = &_STRING($_[1]) or return 0 0 0
768 0 $value =~ s/[\n\r\t\0]+//g; ## desc should be one line
769 0 shift->_set('desc', $value);
770 }
771  
772 3 ▶sub set_tags {
Subroutine set_tags: called CC=2 SCAR=7.0
Pod undocumented
773 3 ▶ my $value = _STRING($_[1]) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $value = &_STRING($_[1]) or return 3 0 3
774 3 $value =~ s/\s+//g; ## no space in tags thanks
775 3 my %hash_value = map { $_ => 1 } split (',', $value);
776 3 shift->_set('tags', \%hash_value);
777 }
778  
779 0 ▶sub set_add_env {
Subroutine set_add_env: not called CC=2 SCAR=17.9
Pod undocumented
780 0 my $svc = shift;
781 0 ▶ my $value = _STRING($_[0]) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $value = &_STRING($_[0]) or return 0 0 0
782 0 my ($key, $val) = split /=/, $value, 2;
783 0 $svc->{ctrl}->log->debug( "Setting ENV{$key} to '$val'" );
784 0 $svc->add_env($key, $val);
785 }
786  
787 =head2 add_env($key => $value)
788  
789 Adds an environment key, value pair to the service
790  
791 =cut
792  
793 2 ▶sub add_env {
Subroutine add_env: called CC=1 SCAR=0.0
Pod documented
794 2 my $svc = shift;
795 2 my ($key, $value) = @_;
796 2 $svc->env->{$key} = $value;
797 2 return 1;
798 }
799  
800 =head2 clear_env()
801  
802 Resets service environment to empty.
803  
804 =cut
805  
806 1 ▶sub clear_env {
Subroutine clear_env: called CC=1 SCAR=0.0
Pod documented
807 1 my $svc = shift;
808 1 $svc->{env} = {};
809 }
810  
811 5 ▶sub set_stopwait_secs {
Subroutine set_stopwait_secs: called CC=2 SCAR=7.2
Pod undocumented
812 5 ▶ my $value = _NUMBER($_[1]) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $value = &_NUMBER($_[1]) or return 5 0 5
813 5 shift->_set('stopwait_secs', $value);
814 }
815  
816 8 ▶sub set_startwait_secs {
Subroutine set_startwait_secs: called CC=2 SCAR=7.2
Pod undocumented
817 8 ▶ my $value = _NUMBER($_[1]) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $value = &_NUMBER($_[1]) or return 8 0 8
818 8 shift->_set('startwait_secs', $value);
819 }
820  
821 2 ▶sub set_tie_stdin_to {
Subroutine set_tie_stdin_to: called CC=2 SCAR=7.2
Pod undocumented
822 2 ▶ my $value = _STRING($_[1]) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $value = &_STRING($_[1]) or return 2 0 2
823 2 shift->_set('tie_stdin_to', $value);
824 }
825  
826 0 ▶sub set_ignore_stderr {
Subroutine set_ignore_stderr: not called CC=2 SCAR=17.9
Pod undocumented
827 0 my $value = _STRING($_[1]);
828 0 ▶ return unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 0 0
829 0 shift->_set('ignore_stderr', $value);
830 }
831  
832 0 ▶sub set_ignore_stdout {
Subroutine set_ignore_stdout: not called CC=2 SCAR=17.9
Pod undocumented
833 0 my $value = _STRING($_[1]);
834 0 ▶ return unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 0 0
835 0 shift->_set('ignore_stdout', $value);
836 }
837  
838 4 ▶sub set_respawn_on_fail {
Subroutine set_respawn_on_fail: called CC=2 SCAR=7.1
Pod undocumented
839 4 my $value = _STRING($_[1]);
840 4 ▶ return unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 4 4
841 4 shift->_set('respawn_on_fail', $value);
842 }
843  
844 4 ▶sub set_respawn_on_stop {
Subroutine set_respawn_on_stop: called CC=2 SCAR=7.1
Pod undocumented
845 4 my $value = _STRING($_[1]);
846 4 ▶ return unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 4 4
847 4 shift->_set('respawn_on_stop', $value);
848 }
849  
850 2 ▶sub set_respawn_max_retries {
Subroutine set_respawn_max_retries: called CC=2 SCAR=7.1
Pod undocumented
851 2 my $value = _POSINT($_[1]);
852 2 ▶ return unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 2 2
853 2 shift->_set('respawn_max_retries', $value);
854 }
855  
856 0 ▶sub set_no_new_session {
Subroutine set_no_new_session: not called CC=2 SCAR=17.9
Pod undocumented
857 0 my $value = _STRING($_[1]);
858 0 ▶ return unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 0 0
859 0 shift->_set('no_new_session', $value);
860 }
861  
862 35 ▶sub _run_cmd {
Subroutine _run_cmd: called CC=7 SCAR=20.0
863 35 my $svc = shift;
864 35 my $ctrl = $svc->{ctrl};
865 35 my $svcname = $svc->name;
866 35 $ctrl->log->info( sprintf "starting %s", $svcname );
867  
868 35 my %stds = (
869 "<" => "/dev/null",
870 ">" => "/dev/null",
871 "2>" => "/dev/null",
872 );
873 35 ▶ if (my $sockname = $svc->tie_stdin_to) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $sockname = $svc->tie_stdin_to) 7 28 35
874 7 my $socket = $ctrl->socket($sockname);
875 7 ▶ if ($socket) {
Branch: 1 branch
BranchTrueFalseTotal
if ($socket) 7 0 7
876 7 ▶ if ($socket->is_bound) {
Branch: 1 branch
BranchTrueFalseTotal
if ($socket->is_bound) { } 7 0 7
877 7 $ctrl->log->debug(
878 "Socket '$sockname' piped to stdin for '$svcname'"
879 );
880 7 $stds{"<"} = $socket->fh;
881 }
882 else {
883 ## That's a bit annoying should we try to connect?
884 ## XXX probably
885 0 $ctrl->log->error(
886 "Socket '$sockname' not bound. Fatal '$svcname'"
887 )
888 }
889 }
890 }
891  
892 ## what happens when the config changes?
893 ## watcher *won't* get redefined leading to configuration
894 ## not being takin into account until restart of the svc.
895 ## should we have a watcher reloading function? that will
896 35 ▶ if (my $logger = $ctrl->log) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $logger = $ctrl->log) 35 0 35
897 ## XXX verify leaks
898 35 ▶ unless ($svc->ignore_stdout) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($svc->ignore_stdout) 35 0 35
899 35 $stds{">"} = $logger->svc_watcher(out => $svc);
900 }
901 35 ▶ unless ($svc->ignore_stderr ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($svc->ignore_stderr) 35 0 35
902 35 $stds{"2>"} = $logger->svc_watcher(err => $svc);
903 }
904 }
905 $svc->{child_cv} = AnyEvent::Util::run_cmd(
906 $svc->cmd,
907 close_all => 1,
908 0 ▶ on_prepare => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
909 0 $svc->prepare_child;
910 },
911 '$$' => \$svc->{pid},
912 35 %stds,
913 );
914 33 ▶ $svc->{child_cv}->cb( sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
915 33 my $es = shift()->recv;
916 33 $svc->acknowledge_exit($es);
917 35 });
918 35 return 1;
919 }
920  
921 0 ▶sub prepare_child {
Subroutine prepare_child: not called CC=3 SCAR=24.8
Pod undocumented
922 0 my $svc = shift;
923 0 ▶ unless ($svc->no_new_session) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($svc->no_new_session) 0 0 0
924 0 my $sessid = POSIX::setsid();
925 0 ▶ $svc->{ctrl}->log->error("cannot create new session for service")
Branch: 1 branch
BranchTrueFalseTotal
unless $sessid 0 0 0
926 unless $sessid;
927 }
928 0 $svc->setup_environment;
929 0 return;
930 }
931  
932 =head2 setup_environment
933  
934 Executed in the child before exec, to take service's configured C<env> and
935 populate C<%ENV> with it.
936  
937 =cut
938  
939 0 ▶sub setup_environment {
Subroutine setup_environment: not called CC=4 SCAR=30.0
Pod documented
940 0 my $svc = shift;
941 0 my $env = $svc->env;
942 0 ▶ return unless $env;
Branch: 1 branch
BranchTrueFalseTotal
unless $env 0 0 0
943 0 ▶ return unless ref $env eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $env eq "HASH" 0 0 0
944 0 ▶ while (my ($k, $v) = each %$env) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$env) 0 0 0
945 0 $ENV{$k} = $v;
946 }
947 0 $ENV{CONTROL_FREAK_ENABLED} = 1;
948 0 $ENV{CONTROL_FREAK_SERVICE} = $svc->name;
949 0 return 1;
950 }
951  
952 37 ▶sub acknowledge_exit {
Subroutine acknowledge_exit: called CC=6 SCAR=17.9
Pod undocumented
953 37 my $svc = shift;
954 37 my $es = shift;
955  
956 37 my $ctrl = $svc->{ctrl};
957 37 my $name = $svc->name;
958 37 my $on_stop = $svc->{on_stop_cb};
959  
960 ## reset timers, set basic new state
961 37 $svc->{on_stop_cb} = undef;
962 37 $svc->{stop_cv} = undef;
963 37 $svc->{start_cv} = undef;
964 37 $svc->{child_cv} = undef;
965 37 $svc->{pid} = undef;
966 37 $svc->{exit_status} = $es;
967  
968 37 ▶ if (POSIX::WIFEXITED($es) && !POSIX::WEXITSTATUS($es)) {
Branch: 2 branches
BranchTrueFalseTotal
if (POSIX::WIFEXITED($es) and not POSIX::WEXITSTATUS($es)) { } 2 35 37
elsif (POSIX::WIFSIGNALED($es) and POSIX::WTERMSIG($es) == 15) { } 14 21 35
Condition100%3/3
POSIX::WIFEXITED($es) and not POSIX::WEXITSTATUS($es)
!ll&&!rl&&r
16192
MC/DC100%2/2
POSIX::WIFEXITED($es) and not POSIX::WEXITSTATUS($es)
POSIX::WIFEXITED($es) not POSIX::WEXITSTATUS($es)
Truth table100%3/3
A and not(B)
A= POSIX::WIFEXITED($es)B= not POSIX::WEXITSTATUS($es)
ABresult
0X0
100
111
969 2 $ctrl->log->info("child $name exited");
970 2 $svc->{normal_exit} = 1;
971 }
972 ▶ elsif (POSIX::WIFSIGNALED($es) && POSIX::WTERMSIG($es) == SIGTERM) {
Condition100%3/3
POSIX::WIFSIGNALED($es) and POSIX::WTERMSIG($es) == 15
!ll&&!rl&&r
19214
MC/DC100%2/2
POSIX::WIFSIGNALED($es) and POSIX::WTERMSIG($es) == 15
POSIX::WIFSIGNALED($es) POSIX::WTERMSIG($es) == 15
Truth table100%3/3
A and B
A= POSIX::WIFSIGNALED($es)B= POSIX::WTERMSIG($es) == 15
ABresult
0X0
100
111
973 14 $ctrl->log->info("child $name gracefully killed");
974 }
975 else {
976 21 return $svc->deal_with_failure;
977 }
978 16 $svc->{state} = 'stopped';
979 16 ▶ $on_stop->() if $on_stop;
Branch: 1 branch
BranchTrueFalseTotal
if $on_stop 1 15 16
980 16 $svc->optionally_respawn;
981 }
982  
983 ## What to do when process doesn't exit cleanly
984 21 ▶sub deal_with_failure {
Subroutine deal_with_failure: called CC=6 SCAR=18.0
Pod undocumented
985 21 my $svc = shift;
986  
987 21 my $es = $svc->{exit_status};
988 21 my $r = ControlFreak::Util::exit_reason( $es );
989 21 $svc->{ctrl}->log->error("child terminated abnormally $es: $r");
990  
991 ## If we don't respawn on fail... just fail
992 21 ▶ if (! $svc->respawn_on_fail) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($svc->respawn_on_fail) 4 17 21
993 4 $svc->{state} = 'fail';
994 4 return;
995 }
996  
997 ## If we wanted the service down. Keep it that way.
998 17 ▶ if ($svc->{wants_down}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($svc->{"wants_down"}) 0 17 17
999 0 $svc->{state} = 'fail';
1000 0 return;
1001 }
1002  
1003 ## If the service failed while starting, enter backoff loop
1004 17 ▶ if ($svc->is_starting) {
Branch: 1 branch
BranchTrueFalseTotal
if ($svc->is_starting) { } 14 3 17
1005 14 ▶ my $n = ++$svc->{backoff_retry} || 1;
Condition50%1/2
++$svc->{'backoff_retry'} || 1
l!l
140
MC/DC0%0/1
++$svc->{'backoff_retry'} || 1
++$svc->{'backoff_retry'}
Truth table50%1/2
A
A= ++$svc->{'backoff_retry'}
Aresult
11
00
1006 14 ▶ if ($n >= $svc->{respawn_max_retries}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n >= $svc->{"respawn_max_retries"}) 2 12 14
1007 ## Exhausted options: bail
1008 2 $svc->{state} = 'fatal';
1009 2 $svc->{backoff_cv} = undef;
1010 2 return;
1011 }
1012 12 $svc->{state} = "backoff";
1013 12 my $backoff_delay = $svc->_exponential_backoff_delay($n);
1014 12 $svc->{backoff_retry} = $n;
1015 $svc->{backoff_cv} = AE::timer $backoff_delay, 0,
1016 12 ▶ sub { $svc->_backoff_restart };
Subroutine __ANON__: called CC=1 SCAR=0.0
1017 }
1018 ## Otherwise, just restart the failed service
1019 else {
1020 3 $svc->{state} = 'fail';
1021 3 $svc->start;
1022 }
1023  
1024 15 return;
1025 }
1026  
1027 16 ▶sub optionally_respawn {
Subroutine optionally_respawn: called CC=5 SCAR=16.2
Pod undocumented
1028 16 my $svc = shift;
1029 16 ▶ return unless $svc->is_stopped;
Branch: 1 branch
BranchTrueFalseTotal
unless $svc->is_stopped 0 16 16
1030 16 ▶ return unless $svc->respawn_on_stop;
Branch: 1 branch
BranchTrueFalseTotal
unless $svc->respawn_on_stop 11 5 16
1031 return if !$svc->{normal_exit} # abnormal exits are not our business
1032 5 ▶ or $svc->{wants_down}; # we really want it down
Branch: 1 branch
BranchTrueFalseTotal
if not $svc->{"normal_exit"} or $svc->{"wants_down"} 4 1 5
Condition66%2/3
not $svc->{"normal_exit"} or $svc->{"wants_down"}
l!l&&r!l&&!r
401
MC/DC50%1/2
not $svc->{"normal_exit"} or $svc->{"wants_down"}
not $svc->{"normal_exit"} $svc->{"wants_down"}
Truth table66%2/3
not(A) or B
A= not $svc->{"normal_exit"}B= $svc->{"wants_down"}
ABresult
1X1
011
000
1033 1 $svc->start;
1034 1 return;
1035 }
1036  
1037 =head1 AUTHOR
1038  
1039 Yann Kerherve E<lt>yannk@cpan.orgE<gt>
1040  
1041 =head1 LICENSE
1042  
1043 This library is free software; you can redistribute it and/or modify
1044 it under the same terms as Perl itself.
1045  
1046 =head1 SEE ALSO
1047  
1048 L<ControlFreak>
1049  
1050 =cut
1051  
1052 8 1;
« lib/ControlFreak/Proxy.pm ↑ Summary lib/ControlFreak/Logger.pm »