lib/ControlFreak.pm

filter: Statementstmt 41.6% 109 / 262 Branchbran 31.6% 19 / 60 Conditioncond 10.8% 8 / 74 MC/DCmcdc 0.0% 0 / 46 Subroutinesub 35.8% 24 / 67 Podpod 54.2% 19 / 35 totaltotal 32.9% 179 / 544 CC 67 SCAR 27.2
CC
67
Cov
34%
command_pids
72.0
command_bind
42.0
_command_ctrl
30.0
CRAP 15.1
« blib/lib/ControlFreak/Commander.pm ↑ Summary lib/ControlFreak/Util.pm »
1 package ControlFreak;
2  
3 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 8 ▶use 5.008_001;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 8 our $VERSION = '1.0.0'; ## http://semver.org
6  
7 8 ▶use Object::Tiny qw{
Subroutine BEGIN: called CC=1 SCAR=0.0
8 log
9 console
10 home
11 8 };
12  
13 8 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 8 ▶use ControlFreak::Command;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 8 ▶use ControlFreak::Logger;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 8 ▶use ControlFreak::Service;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 8 ▶use ControlFreak::Proxy;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 8 ▶use File::Spec();
Subroutine BEGIN: called CC=1 SCAR=0.0
19 8 ▶use Params::Util qw{ _ARRAY _CODE };
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 8 our $CRLF = "\015\012";
22  
23 =encoding utf8
24  
25 =head1 NAME
26  
27 ControlFreak - a process supervisor
28  
29 =head1 SYNOPSIS
30  
31 ## WARNING
32 ## see L<cfkd> and L<cfkctl> manpages for how to run ControlFreak from
33 ## the shell. This is the programatic interface used by these scripts.
34  
35 $ctrl = ControlFreak->new(
36 log_config_file => $log_config_file,
37 );
38 $ctrl->run; # enter the event loop, returns only for exiting
39  
40 ## elsewhere in the eventloop
41 $ctrl->add_socket($sock);
42 $sock = $ctrl->socket($sockname);
43  
44 $svc = $ctrl->find_or_create($svcname);
45 $ctrl->add_service($svc);
46 $svc = $ctrl->service($svcname);
47  
48 @svcs = $ctrl->service_by_tag($tag);
49 @svcs = $ctrl->services;
50  
51 $ctrl->destroy($svcname);
52  
53 $ctrl->set_console($con);
54 $con = $ctrl->console;
55 $log = $ctrl->log;
56  
57 =head1 DESCRIPTION
58  
59 This is the programmer documentation. Look into L<ControlFreak/Intro.pod>
60 for user documentation.
61  
62 =head1 METHODS
63  
64 =head2 new(%param)
65  
66 =over 4
67  
68 =item * config
69  
70 The absolute path to a initial config file.
71  
72 =back
73  
74 =cut
75  
76 14 ▶sub new {
Subroutine new: called CC=3 SCAR=11.1
Pod documented
77 14 my $class = shift;
78 14 my %param = @_;
79 14 my $ctrl = $class->SUPER::new(%param);
80  
81 14 my $base = $ctrl->{base} = $param{base};
82  
83 14 $ctrl->{servicemap} = {};
84 14 $ctrl->{socketmap} = {};
85 14 $ctrl->{proxymap} = {};
86  
87 14 my $log_config_file;
88 14 my $home = $param{home};
89 $log_config_file = File::Spec->rel2abs($param{log_config_file}, $home)
90 14 ▶ if defined $param{log_config_file} && $home;
Branch: 1 branch
BranchTrueFalseTotal
if defined $param{"log_config_file"} and $home 0 14 14
Condition33%1/3
defined $param{"log_config_file"} and $home
!ll&&!rl&&r
1400
MC/DC0%0/2
defined $param{"log_config_file"} and $home
defined $param{"log_config_file"} $home
Truth table33%1/3
A and B
A= defined $param{"log_config_file"}B= $home
ABresult
0X0
100
111
91  
92 14 $ctrl->{log} = ControlFreak::Logger->new(
93 config_file => $log_config_file,
94 );
95  
96 14 return $ctrl;
97 }
98  
99 =head2 services
100  
101 Returns a list of L<ControlFreak::Service> instances known to this
102 controller.
103  
104 =cut
105  
106 5 ▶sub services {
Subroutine services: called CC=1 SCAR=0.0
Pod documented
107 5 my $ctrl = shift;
108 5 return values %{ $ctrl->{servicemap} };
109 }
110  
111 =head2 sockets
112  
113 Returns a list of L<ControlFreak::Socket> instances known to this
114 controller.
115  
116 =cut
117  
118 0 ▶sub sockets {
Subroutine sockets: not called CC=1 SCAR=6.9
Pod documented
119 0 my $ctrl = shift;
120 0 return values %{ $ctrl->{socketmap} };
121 }
122  
123 =head2 service($name)
124  
125 Returns the service of name C<$name> or nothing.
126  
127 =cut
128  
129 9 ▶sub service {
Subroutine service: called CC=2 SCAR=7.1
Pod documented
130 9 my $ctrl = shift;
131 9 ▶ my ($svcname) = shift or return;
Branch: 1 branch
BranchTrueFalseTotal
my($svcname) = shift() or return 9 0 9
132 9 return $ctrl->{servicemap}{$svcname};
133 }
134  
135 =head2 proxy($name)
136  
137 Returns the proxy of name C<$name> or nothing.
138  
139 =cut
140  
141 1 ▶sub proxy {
Subroutine proxy: called CC=2 SCAR=7.1
Pod documented
142 1 my $ctrl = shift;
143 1 ▶ my ($proxyname) = shift or return;
Branch: 1 branch
BranchTrueFalseTotal
my($proxyname) = shift() or return 1 0 1
144 1 return $ctrl->{proxymap}{$proxyname};
145 }
146  
147 =head2 set_console
148  
149 Takes a L<ControlFreak::Console> instance in parameter and sets it
150 as the console.
151  
152 =cut
153  
154 1 ▶sub set_console {
Subroutine set_console: called CC=1 SCAR=0.0
Pod documented
155 1 my $ctrl = shift;
156 1 my $con = shift;
157  
158 1 $ctrl->{console} = $con;
159 1 return;
160 }
161  
162 =head2 socket($name)
163  
164 Returns the L<ControlFreak::Socket> object of name C<$name> or returns
165 undef.
166  
167 =cut
168  
169 7 ▶sub socket {
Subroutine socket: called CC=2 SCAR=7.1
Pod documented
170 7 my $ctrl = shift;
171 7 ▶ my $name = shift || "";
Condition50%1/2
shift() || ''
l!l
70
MC/DC0%0/1
shift() || ''
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
172 7 return $ctrl->{socketmap}->{$name};
173 }
174  
175 =head2 add_socket($socket)
176  
177 Adds the C<$socket> L<ControlFreak::Socket> object passed in parameters
178 to the list of socket this controller knows about.
179  
180 If a socket by that name already exists, it returns undef, otherwise
181 it returns a true value;
182  
183 =cut
184  
185 4 ▶sub add_socket {
Subroutine add_socket: called CC=3 SCAR=11.0
Pod documented
186 4 my $ctrl = shift;
187 4 my $socket = shift;
188  
189 4 ▶ my $name = $socket->name || "";
Condition50%1/2
$socket->name || ''
l!l
40
MC/DC0%0/1
$socket->name || ''
$socket->name
Truth table50%1/2
A
A= $socket->name
Aresult
11
00
190 4 ▶ return if $ctrl->{socketmap}->{$name};
Branch: 1 branch
BranchTrueFalseTotal
if $ctrl->{"socketmap"}{$name} 1 3 4
191 3 $ctrl->{socketmap}->{$name} = $socket;
192 3 return 1;
193 }
194  
195 =head2 remove_socket($socket_name)
196  
197 Removes the L<ControlFreak::Socket> object by the name of C<$socket_name>
198 from the list of sockets this controller knows about.
199  
200 Returns true if effectively removed.
201  
202 =cut
203  
204 2 ▶sub remove_socket {
Subroutine remove_socket: called CC=1 SCAR=0.0
Pod documented
205 2 my $ctrl = shift;
206 2 my $socket_name = shift;
207 2 return delete $ctrl->{socketmap}->{$socket_name};
208 }
209  
210 =head2 add_proxy($proxy)
211  
212 Adds the C<$proxy> L<ControlFreak::Proxy> object passed in parameters
213 to the list of proxies this controller knows about.
214  
215 If a proxy by that name already exists, it returns undef, otherwise
216 it returns a true value;
217  
218 =cut
219  
220 3 ▶sub add_proxy {
Subroutine add_proxy: called CC=3 SCAR=11.2
Pod documented
221 3 my $ctrl = shift;
222 3 my $proxy = shift;
223  
224 3 ▶ my $name = $proxy->name || "";
Condition50%1/2
$proxy->name || ''
l!l
30
MC/DC0%0/1
$proxy->name || ''
$proxy->name
Truth table50%1/2
A
A= $proxy->name
Aresult
11
00
225 3 ▶ return if $ctrl->{proxymap}->{$name};
Branch: 1 branch
BranchTrueFalseTotal
if $ctrl->{"proxymap"}{$name} 0 3 3
226 3 $ctrl->{proxymap}->{$name} = $proxy;
227 3 return 1;
228 }
229  
230 =head2 remove_proxy($proxy_name)
231  
232 Removes the L<ControlFreak::Proxy> object by the name of C<$proxy_name>
233 from the list of proxies this controller knows about.
234  
235 Returns true if effectively removed.
236  
237 =cut
238  
239 0 ▶sub remove_proxy {
Subroutine remove_proxy: not called CC=1 SCAR=6.9
Pod documented
240 0 my $ctrl = shift;
241 0 my $proxy_name = shift;
242 0 return delete $ctrl->{proxymap}->{$proxy_name};
243 }
244  
245 =head2 proxies
246  
247 Returns a list of proxy objects.
248  
249 =cut
250  
251 0 ▶sub proxies {
Subroutine proxies: not called CC=1 SCAR=6.9
Pod documented
252 0 my $ctrl = shift;
253 0 return values %{ $ctrl->{proxymap} };
254 }
255  
256 =head2 find_or_create_svc($name)
257  
258 Given a service name in parameter (a string), searches for an existing
259 defined service with that name, if not found, then a new service is
260 declared and returned.
261  
262 =cut
263  
264 35 ▶sub find_or_create_svc {
Subroutine find_or_create_svc: called CC=3 SCAR=11.0
Pod documented
265 35 my $ctrl = shift;
266 35 my $svcname = shift;
267 35 my $svc = $ctrl->{servicemap}{$svcname};
268 35 ▶ return $svc if $svc;
Branch: 1 branch
BranchTrueFalseTotal
if $svc 12 23 35
269  
270 23 $svc = ControlFreak::Service->new(
271 name => $svcname,
272 state => 'stopped',
273 ctrl => $ctrl,
274 );
275 23 ▶ return unless $svc;
Branch: 1 branch
BranchTrueFalseTotal
unless $svc 1 22 23
276  
277 22 return $ctrl->{servicemap}{$svcname} = $svc;
278 }
279  
280 =head2 find_or_create_sock($name)
281  
282 Given a socket name in parameter (a string), searches for an existing
283 defined socket with that name, if not found, then a new socket is
284 declared and returned.
285  
286 =cut
287  
288 0 ▶sub find_or_create_sock {
Subroutine find_or_create_sock: not called CC=3 SCAR=24.8
Pod documented
289 0 my $ctrl = shift;
290 0 my $sockname = shift;
291 0 my $sock = $ctrl->{socketmap}{$sockname};
292 0 ▶ return $sock if $sock;
Branch: 1 branch
BranchTrueFalseTotal
if $sock 0 0 0
293  
294 0 $sock = ControlFreak::Socket->new(
295 name => $sockname,
296 ctrl => $ctrl,
297 );
298 0 ▶ return unless $sock;
Branch: 1 branch
BranchTrueFalseTotal
unless $sock 0 0 0
299  
300 0 return $ctrl->{socketmap}{$sockname} = $sock;
301 }
302  
303 =head2 find_or_create_proxy($name)
304  
305 Given a proxy name in parameter (a string), searches for an existing
306 defined proxy with that name, if not found, then a new proxy is
307 declared and returned.
308  
309 =cut
310  
311 2 ▶sub find_or_create_proxy {
Subroutine find_or_create_proxy: called CC=3 SCAR=11.0
Pod documented
312 2 my $ctrl = shift;
313 2 my $proxyname = shift;
314 2 my $proxy = $ctrl->{proxymap}{$proxyname};
315 2 ▶ return $proxy if $proxy;
Branch: 1 branch
BranchTrueFalseTotal
if $proxy 1 1 2
316  
317 1 $proxy = ControlFreak::Proxy->new(
318 name => $proxyname,
319 ctrl => $ctrl,
320 );
321 1 ▶ return unless $proxy;
Branch: 1 branch
BranchTrueFalseTotal
unless $proxy 0 1 1
322  
323 1 return $ctrl->{proxymap}{$proxyname} = $proxy;
324 }
325  
326 =head2 logger
327  
328 Returns the logger attached to the controller.
329  
330 =cut
331  
332 =head2 services_by_tag($tag)
333  
334 Given a tag in parameter, returns a list of matching service objects.
335  
336 =cut
337  
338 3 ▶sub services_by_tag {
Subroutine services_by_tag: called CC=1 SCAR=0.0
Pod documented
339 3 my $ctrl = shift;
340 3 my $tag = shift;
341 3 return grep { $_->tags->{$tag} } $ctrl->services;
342 }
343  
344 =head2 services_from_args(%param)
345  
346 Given a list of arguments (typically from the console commands)
347 returns a list of L<ControlFreak::Service> instances.
348  
349 =over 4
350  
351 =item * args
352  
353 The list of arguments to analyze.
354  
355 =item * err
356  
357 A callback called with the parsing errors of the arguments.
358  
359 =back
360  
361 =cut
362  
363 2 ▶sub services_from_args {
Subroutine services_from_args: called CC=8 SCAR=28.2
Pod documented
364 2 my $ctrl = shift;
365 2 my %param = @_;
366  
367 2 ▶ my $err = _CODE($param{err_cb}) || sub {};
Subroutine __ANON__: not called CC=1 SCAR=0.2
Condition66%2/3
&_CODE($param{'err_cb'}) || sub { }
l!l&&r!l&&!r
110
MC/DC0%0/2
&_CODE($param{'err_cb'}) || sub { }
&_CODE($param{'err_cb'}) sub { }
Truth table66%2/3
A || B
A= &_CODE($param{'err_cb'})B= sub { }
ABresult
1X1
011
000
368 my $args = _ARRAY($param{args})
369 2 ▶ or return ();
Branch: 1 branch
BranchTrueFalseTotal
my $args = &_ARRAY($param{"args"}) or return () 2 0 2
370  
371 2 my $selector = shift @$args;
372 2 ▶ if ($selector eq 'service') {
Branch: 3 branches
BranchTrueFalseTotal
if ($selector eq 'service') { } 0 2 2
elsif ($selector eq 'tag') { } 1 1 2
elsif ($selector eq 'all') { } 1 0 1
373 0 ▶ unless (scalar @$args == 1) {
Branch: 1 branch
BranchTrueFalseTotal
unless (scalar @$args == 1) 0 0 0
374 0 $err->('service selector takes exactly 1 argument: name');
375 0 return ();
376 }
377 0 my $name = shift @$args;
378 0 my $svc = $ctrl->service($name);
379 0 ▶ return $svc ? ($svc) : ();
Branch: 1 branch
BranchTrueFalseTotal
$svc ? : 0 0 0
380 }
381 elsif ($selector eq 'tag') {
382 1 return $ctrl->services_by_tag(shift @$args);
383 }
384 elsif ($selector eq 'all') {
385 1 return $ctrl->services;
386 }
387 else {
388 0 $err->("unknown selector '$selector'");
389 }
390 0 return ();
391 }
392  
393  
394 =head2 command_*
395  
396 All accessible commands to the config and the console.
397  
398 =cut
399  
400 0 ▶sub command_start { _command_ctrl('start', @_ ) }
Subroutine command_start: not called CC=1 SCAR=6.9
Pod undocumented
401 0 ▶sub command_stop { _command_ctrl('stop', @_ ) }
Subroutine command_stop: not called CC=1 SCAR=6.9
Pod undocumented
402 0 ▶sub command_restart { _command_ctrl('restart', @_ ) }
Subroutine command_restart: not called CC=1 SCAR=6.9
Pod undocumented
403 0 ▶sub command_down { _command_ctrl('down', @_ ) }
Subroutine command_down: not called CC=1 SCAR=6.9
Pod undocumented
404 0 ▶sub command_up { _command_ctrl('up', @_ ) }
Subroutine command_up: not called CC=1 SCAR=6.9
Pod undocumented
405  
406 0 ▶sub _command_ctrl {
Subroutine _command_ctrl: not called CC=5 SCAR=34.0
407 0 my $meth = shift;
408 0 my $ctrl = shift;
409 0 my %param = @_;
410  
411 0 ▶ my $err = _CODE($param{err_cb}) || sub {};
Subroutine __ANON__: not called CC=1 SCAR=6.9
Condition0%0/3
&_CODE($param{'err_cb'}) || sub { }
l!l&&r!l&&!r
000
MC/DC0%0/2
&_CODE($param{'err_cb'}) || sub { }
&_CODE($param{'err_cb'}) sub { }
Truth table0%0/3
A || B
A= &_CODE($param{'err_cb'})B= sub { }
ABresult
1X1
011
000
412 0 ▶ my $ok = _CODE($param{ok_cb}) || sub {};
Subroutine __ANON__: not called CC=1 SCAR=6.9
Condition0%0/3
&_CODE($param{'ok_cb'}) || sub { }
l!l&&r!l&&!r
000
MC/DC0%0/2
&_CODE($param{'ok_cb'}) || sub { }
&_CODE($param{'ok_cb'}) sub { }
Truth table0%0/3
A || B
A= &_CODE($param{'ok_cb'})B= sub { }
ABresult
1X1
011
000
413 0 my @svcs = $ctrl->services_from_args(
414 %param, err_cb => $err, ok_cb => $ok,
415 );
416 0 ▶ if (! @svcs) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@svcs) 0 0 0
417 0 return $err->("Couldn't find a valid service. bailing.");
418 }
419 0 my $n = 0;
420 0 for (@svcs) {
421 0 ▶ $_->$meth(err_cb => $err, ok_cb => sub { $n++ });
Subroutine __ANON__: not called CC=1 SCAR=6.9
422 }
423 0 $ok->("done $n");
424 0 return;
425 }
426  
427 ## for now, at least this is separated.
428 ## but could we imagine a command start all running proxies as well?
429 0 ▶sub command_proxyup {
Subroutine command_proxyup: not called CC=5 SCAR=34.0
Pod undocumented
430 0 my $ctrl = shift;
431 0 my %param = @_;
432  
433 0 ▶ my $err = _CODE($param{err_cb}) || sub {};
Subroutine __ANON__: not called CC=1 SCAR=6.9
Condition0%0/3
&_CODE($param{'err_cb'}) || sub { }
l!l&&r!l&&!r
000
MC/DC0%0/2
&_CODE($param{'err_cb'}) || sub { }
&_CODE($param{'err_cb'}) sub { }
Truth table0%0/3
A || B
A= &_CODE($param{'err_cb'})B= sub { }
ABresult
1X1
011
000
434 0 ▶ my $ok = _CODE($param{ok_cb}) || sub {};
Subroutine __ANON__: not called CC=1 SCAR=6.9
Condition0%0/3
&_CODE($param{'ok_cb'}) || sub { }
l!l&&r!l&&!r
000
MC/DC0%0/2
&_CODE($param{'ok_cb'}) || sub { }
&_CODE($param{'ok_cb'}) sub { }
Truth table0%0/3
A || B
A= &_CODE($param{'ok_cb'})B= sub { }
ABresult
1X1
011
000
435  
436 0 my $proxyname = $param{args}[0];
437  
438 0 ▶ my $proxy = $ctrl->proxy($proxyname || "");
Condition0%0/2
$proxyname || ''
l!l
00
MC/DC0%0/1
$proxyname || ''
$proxyname
Truth table0%0/2
A
A= $proxyname
Aresult
11
00
439 0 ▶ if (! $proxy) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($proxy) 0 0 0
440 0 return $err->("Couldn't find a valid proxy. bailing.");
441 }
442 0 $proxy->run;
443 0 $ok->();
444 0 return;
445 }
446  
447 0 ▶sub command_proxydown {
Subroutine command_proxydown: not called CC=5 SCAR=34.0
Pod undocumented
448 0 my $ctrl = shift;
449 0 my %param = @_;
450  
451 0 ▶ my $err = _CODE($param{err_cb}) || sub {};
Subroutine __ANON__: not called CC=1 SCAR=6.9
Condition0%0/3
&_CODE($param{'err_cb'}) || sub { }
l!l&&r!l&&!r
000
MC/DC0%0/2
&_CODE($param{'err_cb'}) || sub { }
&_CODE($param{'err_cb'}) sub { }
Truth table0%0/3
A || B
A= &_CODE($param{'err_cb'})B= sub { }
ABresult
1X1
011
000
452 0 ▶ my $ok = _CODE($param{ok_cb}) || sub {};
Subroutine __ANON__: not called CC=1 SCAR=6.9
Condition0%0/3
&_CODE($param{'ok_cb'}) || sub { }
l!l&&r!l&&!r
000
MC/DC0%0/2
&_CODE($param{'ok_cb'}) || sub { }
&_CODE($param{'ok_cb'}) sub { }
Truth table0%0/3
A || B
A= &_CODE($param{'ok_cb'})B= sub { }
ABresult
1X1
011
000
453  
454 0 my $proxyname = $param{args}[0];
455  
456 0 ▶ my $proxy = $ctrl->proxy($proxyname || "");
Condition0%0/2
$proxyname || ''
l!l
00
MC/DC0%0/1
$proxyname || ''
$proxyname
Truth table0%0/2
A
A= $proxyname
Aresult
11
00
457 0 ▶ if (! $proxy) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($proxy) 0 0 0
458 0 return $err->("Couldn't find a valid proxy. bailing.");
459 }
460 0 $proxy->shutdown;
461 0 $ok->();
462 0 return;
463 }
464  
465 0 ▶sub command_list {
Subroutine command_list: not called CC=2 SCAR=17.9
Pod undocumented
466 0 my $ctrl = shift;
467 0 my %param = @_;
468 0 ▶ my $ok = _CODE($param{ok_cb}) || sub {};
Subroutine __ANON__: not called CC=1 SCAR=6.9
Condition0%0/3
&_CODE($param{'ok_cb'}) || sub { }
l!l&&r!l&&!r
000
MC/DC0%0/2
&_CODE($param{'ok_cb'}) || sub { }
&_CODE($param{'ok_cb'}) sub { }
Truth table0%0/3
A || B
A= &_CODE($param{'ok_cb'})B= sub { }
ABresult
1X1
011
000
469 0 my @out = map { $_->name } $ctrl->services;
470 0 $ok->(join "\n", @out);
471 }
472  
473 0 ▶sub command_desc {
Subroutine command_desc: not called CC=4 SCAR=30.0
Pod undocumented
474 0 my $ctrl = shift;
475 0 my %param = @_;
476  
477 0 ▶ my $ok = _CODE($param{ok_cb}) || sub {};
Subroutine __ANON__: not called CC=1 SCAR=6.9
Condition0%0/3
&_CODE($param{'ok_cb'}) || sub { }
l!l&&r!l&&!r
000
MC/DC0%0/2
&_CODE($param{'ok_cb'}) || sub { }
&_CODE($param{'ok_cb'}) sub { }
Truth table0%0/3
A || B
A= &_CODE($param{'ok_cb'})B= sub { }
ABresult
1X1
011
000
478  
479 0 ▶ my $args = $param{args} || [ 'all' ];
Condition0%0/2
$param{'args'} || ['all']
l!l
00
MC/DC0%0/1
$param{'args'} || ['all']
$param{'args'}
Truth table0%0/2
A
A= $param{'args'}
Aresult
11
00
480 0 ▶ $args = ['all'] unless @$args;
Branch: 1 branch
BranchTrueFalseTotal
unless @$args 0 0 0
481  
482 0 my @svcs = $ctrl->services_from_args(
483 %param, ok_cb => $ok,
484 );
485 0 my @out = map { $_->desc_as_text } @svcs;
486 0 $ok->(join "\n", @out);
487 }
488  
489 0 ▶sub command_version {
Subroutine command_version: not called CC=2 SCAR=17.9
Pod undocumented
490 0 my $ctrl = shift;
491 0 my %param = @_;
492 0 ▶ my $ok = _CODE($param{ok_cb}) || sub {};
Subroutine __ANON__: not called CC=1 SCAR=6.9
Condition0%0/3
&_CODE($param{'ok_cb'}) || sub { }
l!l&&r!l&&!r
000
MC/DC0%0/2
&_CODE($param{'ok_cb'}) || sub { }
&_CODE($param{'ok_cb'}) sub { }
Truth table0%0/3
A || B
A= &_CODE($param{'ok_cb'})B= sub { }
ABresult
1X1
011
000
493 0 $ok->($VERSION);
494 }
495  
496 1 ▶sub command_status {
Subroutine command_status: called CC=5 SCAR=17.3
Pod undocumented
497 1 my $ctrl = shift;
498 1 my %param = @_;
499  
500 1 ▶ my $ok = _CODE($param{ok_cb}) || sub {};
Subroutine __ANON__: not called CC=1 SCAR=1.2
Condition33%1/3
&_CODE($param{'ok_cb'}) || sub { }
l!l&&r!l&&!r
100
MC/DC0%0/2
&_CODE($param{'ok_cb'}) || sub { }
&_CODE($param{'ok_cb'}) sub { }
Truth table33%1/3
A || B
A= &_CODE($param{'ok_cb'})B= sub { }
ABresult
1X1
011
000
501  
502 1 ▶ my $args = $param{args} || [ 'all' ];
Condition50%1/2
$param{'args'} || ['all']
l!l
10
MC/DC0%0/1
$param{'args'} || ['all']
$param{'args'}
Truth table50%1/2
A
A= $param{'args'}
Aresult
11
00
503 1 ▶ $args = ['all'] unless @$args;
Branch: 1 branch
BranchTrueFalseTotal
unless @$args 1 0 1
504 1 my @svcs = $ctrl->services_from_args(%param, args => $args);
505  
506 1 my @out;
507 1 for (@svcs) {
508 0 push @out, $_->status_as_text;
509 }
510 1 $ok->(join "\n", @out);
511 }
512  
513 0 ▶sub command_pids {
Subroutine command_pids: not called CC=8 SCAR=42.8
Pod undocumented
514 0 my $ctrl = shift;
515 0 my %param = @_;
516  
517 0 ▶ my $ok = _CODE($param{ok_cb}) || sub {};
Subroutine __ANON__: not called CC=1 SCAR=6.9
Condition0%0/3
&_CODE($param{'ok_cb'}) || sub { }
l!l&&r!l&&!r
000
MC/DC0%0/2
&_CODE($param{'ok_cb'}) || sub { }
&_CODE($param{'ok_cb'}) sub { }
Truth table0%0/3
A || B
A= &_CODE($param{'ok_cb'})B= sub { }
ABresult
1X1
011
000
518  
519 0 ▶ my $args = $param{args} || [ 'all' ];
Condition0%0/2
$param{'args'} || ['all']
l!l
00
MC/DC0%0/1
$param{'args'} || ['all']
$param{'args'}
Truth table0%0/2
A
A= $param{'args'}
Aresult
11
00
520 0 ▶ $args = ['all'] unless @$args;
Branch: 1 branch
BranchTrueFalseTotal
unless @$args 0 0 0
521 0 my @svcs = $ctrl->services_from_args(%param, args => $args);
522 0 my %seen;
523 my @out;
524 0 for (@svcs) {
525 0 my $svcname = $_->name;
526 0 ▶ next if $seen{$svcname}++;
Branch: 1 branch
BranchTrueFalseTotal
if $seen{$svcname}++ 0 0 0
527 0 my @pids = ($_->pid);
528 0 ▶ if (my $proxy = $_->proxy) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $proxy = $_->proxy) 0 0 0
529 0 my $ppid = $proxy->pid;
530 0 ▶ unshift @pids, $ppid if $ppid;
Branch: 1 branch
BranchTrueFalseTotal
if $ppid 0 0 0
531 }
532 0 push @out, "$svcname: " . join (", ", @pids);
533 }
534 0 $ok->(join "\n", @out);
535 }
536  
537 0 ▶sub command_proxystatus {
Subroutine command_proxystatus: not called CC=3 SCAR=24.8
Pod undocumented
538 0 my $ctrl = shift;
539 0 my %param = @_;
540  
541 0 ▶ my $ok = _CODE($param{ok_cb}) || sub {};
Subroutine __ANON__: not called CC=1 SCAR=6.9
Condition0%0/3
&_CODE($param{'ok_cb'}) || sub { }
l!l&&r!l&&!r
000
MC/DC0%0/2
&_CODE($param{'ok_cb'}) || sub { }
&_CODE($param{'ok_cb'}) sub { }
Truth table0%0/3
A || B
A= &_CODE($param{'ok_cb'})B= sub { }
ABresult
1X1
011
000
542 0 my @proxies = $ctrl->proxies;
543 0 my @out;
544 0 for my $p ($ctrl->proxies) {
545 0 push @out, $p->status_as_text;
546 }
547 0 $ok->(join "\n", @out);
548 }
549  
550 0 ▶sub command_bind {
Subroutine command_bind: not called CC=6 SCAR=37.4
Pod undocumented
551 0 my $ctrl = shift;
552 0 my %param = @_;
553 0 ▶ my $args = $param{args} || [];
Condition0%0/2
$param{'args'} || []
l!l
00
MC/DC0%0/1
$param{'args'} || []
$param{'args'}
Truth table0%0/2
A
A= $param{'args'}
Aresult
11
00
554 0 ▶ my $err = _CODE($param{err_cb}) || sub {};
Subroutine __ANON__: not called CC=1 SCAR=6.9
Condition0%0/3
&_CODE($param{'err_cb'}) || sub { }
l!l&&r!l&&!r
000
MC/DC0%0/2
&_CODE($param{'err_cb'}) || sub { }
&_CODE($param{'err_cb'}) sub { }
Truth table0%0/3
A || B
A= &_CODE($param{'err_cb'})B= sub { }
ABresult
1X1
011
000
555 0 ▶ my $ok = _CODE($param{ok_cb}) || sub {};
Subroutine __ANON__: not called CC=1 SCAR=6.9
Condition0%0/3
&_CODE($param{'ok_cb'}) || sub { }
l!l&&r!l&&!r
000
MC/DC0%0/2
&_CODE($param{'ok_cb'}) || sub { }
&_CODE($param{'ok_cb'}) sub { }
Truth table0%0/3
A || B
A= &_CODE($param{'ok_cb'})B= sub { }
ABresult
1X1
011
000
556 0 ▶ my $sockname = shift @$args || "";
Condition0%0/2
shift @$args || ''
l!l
00
MC/DC0%0/1
shift @$args || ''
shift @$args
Truth table0%0/2
A
A= shift @$args
Aresult
11
00
557 0 my $sock = $ctrl->socket($sockname);
558 0 ▶ unless ($sock) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($sock) 0 0 0
559 0 return $err->("unknown socket '$sockname'");
560 }
561 0 $sock->bind();
562 0 $ok->();
563 0 return;
564 }
565  
566 0 ▶sub command_shutdown {
Subroutine command_shutdown: not called CC=1 SCAR=6.9
Pod undocumented
567 ## I'm tired of killing my procs.
568 ## might not stay in the future
569 0 my $ctrl = shift;
570 0 $ctrl->shutdown;
571 0 ▶ $ctrl->{exit_cv} = AE::timer 1, 0, sub { exit };
Subroutine __ANON__: not called CC=1 SCAR=6.9
572 }
573  
574 0 ▶sub command_destroy {
Subroutine command_destroy: not called CC=5 SCAR=34.0
Pod undocumented
575 0 my $ctrl = shift;
576 0 my %param = @_;
577  
578 0 ▶ my $err = _CODE($param{err_cb}) || sub {};
Subroutine __ANON__: not called CC=1 SCAR=6.9
Condition0%0/3
&_CODE($param{'err_cb'}) || sub { }
l!l&&r!l&&!r
000
MC/DC0%0/2
&_CODE($param{'err_cb'}) || sub { }
&_CODE($param{'err_cb'}) sub { }
Truth table0%0/3
A || B
A= &_CODE($param{'err_cb'})B= sub { }
ABresult
1X1
011
000
579 0 ▶ my $ok = _CODE($param{ok_cb}) || sub {};
Subroutine __ANON__: not called CC=1 SCAR=6.9
Condition0%0/3
&_CODE($param{'ok_cb'}) || sub { }
l!l&&r!l&&!r
000
MC/DC0%0/2
&_CODE($param{'ok_cb'}) || sub { }
&_CODE($param{'ok_cb'}) sub { }
Truth table0%0/3
A || B
A= &_CODE($param{'ok_cb'})B= sub { }
ABresult
1X1
011
000
580  
581 0 my @svcs = $ctrl->services_from_args(
582 %param, err_cb => $err, ok_cb => $ok,
583 );
584 0 my %errors;
585 0 for my $svc (@svcs) {
586 0 my $svcname = $svc->name;
587 $svc->down(
588 0 ▶ on_stop => sub { $ctrl->destroy($svc) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
589 0 ▶ err_cb => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
590 0 $errors{$svcname}++;
591 },
592 0 );
593 }
594 0 ▶ if (keys %errors) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %errors) { } 0 0 0
595 0 my $list = join ", ", keys %errors;
596 0 $err->("Coudn't destroy: $list");
597 }
598 else {
599 0 return $ok->()
600 }
601 0 return;
602 }
603  
604 =head2 destroy($svc)
605  
606 Removes any reference to $svc in the controller. The concerned
607 service must be down in the first place.
608  
609 =cut
610  
611 0 ▶sub destroy {
Subroutine destroy: not called CC=3 SCAR=24.8
Pod documented
612 0 my $ctrl = shift;
613 0 my $svc = shift;
614 0 my $svcname = $svc->name;
615 0 ▶ return unless $svc->is_down;
Branch: 1 branch
BranchTrueFalseTotal
unless $svc->is_down 0 0 0
616 0 ▶ if ($svc->is_backoff) {
Branch: 1 branch
BranchTrueFalseTotal
if ($svc->is_backoff) 0 0 0
617 0 $svc->stop;
618 }
619 0 $ctrl->log->info("Destroying service '$svcname'");
620 0 return delete $ctrl->{servicemap}{$svcname};
621 }
622  
623 =head2 shutdown
624  
625 Cleanly exits all running commands, close all sockets etc...
626  
627 =cut
628  
629 0 ▶sub shutdown {
Subroutine shutdown: not called CC=4 SCAR=30.0
Pod documented
630 0 my $ctrl = shift;
631  
632 0 $_->down for $ctrl->services;
633 0 $_->shutdown for $ctrl->proxies;
634 0 $_->unbind for $ctrl->sockets;
635 }
636  
637 =head1 AUTHOR
638  
639 Yann Kerherve E<lt>yannk@cpan.orgE<gt>
640  
641 =head1 LICENSE
642  
643 This library is free software; you can redistribute it and/or modify
644 it under the same terms as Perl itself.
645  
646 =head1 SEE ALSO
647  
648 I think the venerable (but hatred) daemontools is the ancestor of all
649 supervisor processes. In the same class there is also runit and monit.
650  
651 More recent modules which inspired ControlFreak are God and Supervisord
652 in Python. Surprisingly I didn't find any similar program in Perl. Some
653 ideas in ControlFreak are subtely different though.
654  
655 EDIT: I've spotted Ubic recently on CPAN
656  
657 "If you have kids you probably know what I mean";
« blib/lib/ControlFreak/Commander.pm ↑ Summary lib/ControlFreak/Util.pm »