blib/lib/Daemon/Easy.pm

filter: Statementstmt 32.9% 26 / 79 Branchbran 7.5% 3 / 40 Conditioncond 33.3% 4 / 12 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 50.0% 6 / 12 Podpod 0.0% 0 / 4 totaltotal 25.3% 39 / 154 CC 26 SCAR 37.2
CC
26
Cov
25%
_run
90.0
start
72.0
status
12.0
CRAP 41.3
↑ Summary
1 package Daemon::Easy;
2  
3 1 ▶use 5.008008;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use POSIX;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 our $VERSION = '0.02';
9  
10  
11 1 ▶sub import{
Subroutine import: called CC=8 SCAR=23.9
12 1 my $class = shift;
13 0 ▶ my %args = ( (@_ and ref($_[0])) ? %{$_[0]} : @_ ) or ();
Branch: 2 branches
BranchTrueFalseTotal
my(%args) = @_ && ref $_[0] ? %{$_[0];} : @_ or () 0 1 1
@_ && ref $_[0] ? : 0 1 1
Condition33%1/3
@_ and ref $_[0]
!ll&&!rl&&r
100
MC/DC0%0/2
@_ and ref $_[0]
@_ ref $_[0]
Truth table33%1/3
A and B
A= @_B= ref $_[0]
ABresult
0X0
100
111
14  
15 1 ▶ $args{sleep} = 5 unless defined $args{sleep};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"sleep"} 1 0 1
16 1 ▶ $args{pidfile} ||= "$0.pid";
Condition50%1/2
$args{"pidfile"} ||= "$0.pid"
l!l
01
MC/DC0%0/1
$args{"pidfile"} ||= "$0.pid"
$args{"pidfile"}
Truth table50%1/2
A
A= $args{"pidfile"}
Aresult
11
00
17 1 ▶ $args{stopfile} ||= "$0.stop";
Condition50%1/2
$args{"stopfile"} ||= "$0.stop"
l!l
01
MC/DC0%0/1
$args{"stopfile"} ||= "$0.stop"
$args{"stopfile"}
Truth table50%1/2
A
A= $args{"stopfile"}
Aresult
11
00
18 1 ▶ $args{callback} ||= 'worker';
Condition50%1/2
$args{"callback"} ||= "worker"
l!l
01
MC/DC0%0/1
$args{"callback"} ||= "worker"
$args{"callback"}
Truth table50%1/2
A
A= $args{"callback"}
Aresult
11
00
19  
20 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 $args{worker} = \&{caller()."::$args{callback}"};
22 1 ▶ *{caller()."::run"} = sub{
Subroutine __ANON__: not called CC=1 SCAR=2.6
23 0 unshift @_, \%args;
24 0 goto &_run;
25 1 };
26 }
27  
28 0 ▶sub _run{
Subroutine _run: not called CC=9 SCAR=45.0
29 0 my ($args,$cmd) = @_;
30 0 ▶ $cmd ||= $ARGV[0];
Condition0%0/3
$cmd ||= $ARGV[0]
l!l&&r!l&&!r
000
MC/DC0%0/2
$cmd ||= $ARGV[0]
$cmd $ARGV[0]
Truth table0%0/3
A ||= B
A= $cmdB= $ARGV[0]
ABresult
1X1
011
000
31
32 0 $cmd = lc $cmd;
33 0 ▶ if( $cmd eq 'start' ){
Branch: 4 branches
BranchTrueFalseTotal
if ($cmd eq 'start') { } 0 0 0
elsif ($cmd eq 'stop') { } 0 0 0
elsif ($cmd eq 'status') { } 0 0 0
elsif ($cmd eq 'restart') { } 0 0 0
34 0 ▶ if(my $status = status($args)){
Branch: 1 branch
BranchTrueFalseTotal
if (my $status = status($args)) 0 0 0
35 0 print "the daemon is running with pid: $status\n";
36 0 exit;
37 }
38 0 start($args);
39 }elsif( $cmd eq 'stop' ){
40 0 stop($args);
41 }elsif( $cmd eq 'status' ){
42 0 ▶ if ( status($args) ){
Branch: 1 branch
BranchTrueFalseTotal
if (status($args)) { } 0 0 0
43 0 print "the daemon is running with pid: ".status($args)."\n";
44 }else{
45 0 print "the daemon stopped\n";
46 }
47 }elsif( $cmd eq 'restart' ){
48 0 stop($args);
49 0 ▶ sleep(3) while(status($args));
Branch: 1 branch
BranchTrueFalseTotal
status($args) and do { sleep 3 } 0 0 0
50 0 start($args);
51 }else{
52 0 usage();
53 }
54 }
55  
56 0 ▶sub usage{
Subroutine usage: not called CC=1 SCAR=6.9
Pod undocumented
57 0 print "usage:\n\t $0 [start stop restart status]\n" ;
58 0 exit;
59 }
60  
61 0 ▶sub start{
Subroutine start: not called CC=8 SCAR=42.8
Pod undocumented
62 0 my $args = shift;
63 0 my $pid = fork();
64 0 ▶ die "cant fork, $!\n" unless defined $pid;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pid 0 0 0
65
66 0 ▶ if($pid){ # parent, remember the child pid, and exit
Branch: 1 branch
BranchTrueFalseTotal
if ($pid) 0 0 0
67 0 ▶ open PID,">$args->{pidfile}" or die "cant open $args->{pidfile}, $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open PID, qq[>$args->{"pidfile"}] or die qq[cant open $args->{"pidfile"}, $!\n] 0 0 0
68 0 print PID $pid;
69 0 close PID;
70 0 exit(0);
71 }
72  
73 0 POSIX::setsid();
74
75 0 ▶ unlink ($args->{stopfile}) if( -e $args->{stopfile} );
Branch: 1 branch
BranchTrueFalseTotal
if -e $args->{"stopfile"} 0 0 0
76
77 0 while(1){
78 0 eval { $args->{worker}->(); };
79 0 ▶ if($@){
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
80 0 print $@;
81 0 last;
82 }
83 0 ▶ sleep($args->{sleep}) if $args->{sleep};
Branch: 1 branch
BranchTrueFalseTotal
if $args->{"sleep"} 0 0 0
84 0 ▶ if(-e $args->{stopfile} ){
Branch: 1 branch
BranchTrueFalseTotal
if (-e $args->{"stopfile"}) 0 0 0
85 0 unlink($args->{stopfile});
86 0 last;
87 }
88 }
89  
90 0 unlink ($args->{pidfile});
91 }
92  
93 0 ▶sub stop{
Subroutine stop: not called CC=2 SCAR=17.9
Pod undocumented
94 0 my $args = shift;
95 0 ▶ open FH, ">$args->{stopfile}" or die "cant create $args->{stopfile}, $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open FH, qq[>$args->{"stopfile"}] or die qq[cant create $args->{"stopfile"}, $!\n] 0 0 0
96 0 close FH;
97 }
98  
99 # check status, 0 if stoped, pid if running
100 0 ▶sub status{
Subroutine status: not called CC=3 SCAR=24.8
Pod undocumented
101 0 my $args = shift;
102 0 ▶ if(-e $args->{pidfile} ){
Branch: 1 branch
BranchTrueFalseTotal
if (-e $args->{'pidfile'}) { } 0 0 0
103 0 ▶ open PID,$args->{pidfile} or die "cant open $args->{pidfile}, $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open PID, $args->{"pidfile"} or die qq[cant open $args->{"pidfile"}, $!\n] 0 0 0
104 0 my $pid=<PID>; chomp $pid;
105 0 close PID;
106 0 return $pid;
107 }else{
108 0 return 0;
109 }
110 }
111  
112 1 1;
113 __END__
↑ Summary