blib/lib/Daemon/DaemonizeLight.pm

filter: Statementstmt 27.5% 22 / 80 Branchbran 10.0% 2 / 20 Conditioncond 6.6% 1 / 15 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 36.3% 4 / 11 Podpod 12.5% 1 / 8 totaltotal 21.1% 30 / 142 SCAR 50.5
CC
17
Cov
22%
check_process
42.0
stop
42.0
start
12.0
CRAP 155.5
↑ Summary /usr/local/bin/cpancover »
1 package Daemon::DaemonizeLight;
2  
3 2 require Exporter;
4 2 @ISA = qw(Exporter);
5 2 @EXPORT_OK = qw(daemonize);
6 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use FindBin;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Proc::ProcessTableLight 'process_table';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 =head1 NAME
11  
12 Daemon::DaemonizeLight - New variant of Daemon::Daemonize based on nohup unix command
13  
14 =head1 SYNOPSIS
15  
16 use strict;
17 use Daemon::DaemonizeLight 'daemonize';
18  
19 daemonize();
20  
21 ... something useful code
22  
23 =head1 DESCRIPTION
24  
25 This module provides create simple daemon from your perl script.
26  
27 Only add the daemonize procedure at the beginning of your script.
28  
29 It provide 'start' 'stop' 'restart' arguments for you script in command string.
30  
31 =cut
32  
33 sub check_process{
34 0 ▶ my ($pid)=@_;
Subroutine check_process: not called CC=6 SCAR=37.4
Pod undocumented
35
36 0 my $t=process_table();
37
38 0 my $exists=0;
39 0 my $script=$FindBin::RealScript;
40 0 my $i=0;
41 0 foreach my $p ( @{$t} ){
42 0 ▶ $exists=1 if (!$pid || $p->{PID}==$pid) && $p->{PID}!=$$ && $p->{COMMAND}=~/$script/;
Branch: 1 branch
BranchTrueFalseTotal
if !$pid || $p->{"PID"} == $pid and $p->{"PID"} != $$ and $p->{"COMMAND"} =~ /$script/ 0 0 0
Condition0%0/9
!$pid || $p->{"PID"} == $pid and $p->{"PID"} != $$ and $p->{"COMMAND"} =~ /$script/
!ll&&!rl&&r
000
!$pid || $p->{"PID"} == $pid and $p->{"PID"} != $$
!ll&&!rl&&r
000
not $pid or $p->{"PID"} == $pid
l!l&&r!l&&!r
000
MC/DC0%0/4
!$pid || $p->{"PID"} == $pid and $p->{"PID"} != $$ and $p->{"COMMAND"} =~ /$script/
not $pid $p->{"PID"} == $pid $p->{"PID"} != $$ $p->{"COMMAND"} =~ /$script/
Truth table0%0/7
not(A) or B and C and D
A= not $pidB= $p->{"PID"} == $pidC= $p->{"PID"} != $$D= $p->{"COMMAND"} =~ /$script/
ABCDresult
00XX0
1X0X0
010X0
1X100
01100
1X111
01111
43 }
44
45 0 return $exists;
46 }
47  
48 sub stop{
49 0 ▶ my ($tmp)=@_;
Subroutine stop: not called CC=6 SCAR=37.4
Pod undocumented
50
51 0 my $pid=read_pid($tmp);
52
53 0 ▶ if ($pid){
Branch: 1 branch
BranchTrueFalseTotal
if ($pid) { } 0 0 0
54 0 ▶ if (check_process($pid)){
Branch: 1 branch
BranchTrueFalseTotal
if (check_process($pid)) { } 0 0 0
55 0 print "Try to killing process $FindBin::RealScript : $pid\n";
56 0 kill 'KILL', $pid;
57 0 my $i=0;
58 0 ▶ while(check_process($pid) && $i<3){
Branch: 1 branch
BranchTrueFalseTotal
if (check_process($pid) and $i < 3) 0 0 0
Condition0%0/3
check_process($pid) and $i < 3
!ll&&!rl&&r
000
MC/DC0%0/2
check_process($pid) and $i < 3
check_process($pid) $i < 3
Truth table0%0/3
A and B
A= check_process($pid)B= $i < 3
ABresult
0X0
100
111
59 0 $i++;
60 0 sleep(1);
61 0 print ".";
62 }
63 0 ▶ if (check_process($pid)){
Branch: 1 branch
BranchTrueFalseTotal
if (check_process($pid)) { } 0 0 0
64 0 print "Kill process $FindBin::RealScript : $pid FAIL\n";
65 } else {
66 0 print "Kill process $FindBin::RealScript : $pid SUCCESS\n";
67 }
68 } else {
69
70 }
71 } else {
72 0 print "Nothing to kill\n";
73 }
74 }
75  
76 sub start{
77 0 ▶ my ($tmp)=@_;
Subroutine start: not called CC=3 SCAR=24.8
Pod undocumented
78 0 ▶ if (check_process()){
Branch: 1 branch
BranchTrueFalseTotal
if (check_process()) { } 0 0 0
79 0 print "Process $FindBin::RealScript already exists\n";
80 } else {
81 0 my $script = $FindBin::Bin.'/'.$FindBin::RealScript;
82 0 my $clear_script = $tmp.'/'.clear_ext($FindBin::RealScript);
83
84 0 print "Starting the process $FindBin::RealScript\n";
85
86 0 my $cmd="nohup perl $script pid 1>$clear_script.log 2>$clear_script.err &";
87 0 `$cmd`;
88
89 0 sleep(1);
90 0 ▶ if (check_process()){
Branch: 1 branch
BranchTrueFalseTotal
if (check_process()) { } 0 0 0
91 0 print "Starting process $FindBin::RealScript SUCCESS\n";
92 } else {
93 0 print "Starting process $FindBin::RealScript FAILED\n";
94 }
95 }
96 }
97  
98 sub restart{
99 0 ▶ my ($tmp)=@_;
Subroutine restart: not called CC=1 SCAR=6.9
Pod undocumented
100 0 stop($tmp);
101 0 start($tmp);
102 }
103  
104 sub pid{
105 0 ▶ my ($tmp)=@_;
Subroutine pid: not called CC=1 SCAR=6.9
Pod undocumented
106
107 0 my $script=clear_ext($FindBin::RealScript);
108 0 open(F, ">$tmp/$script.pid");
109 0 print F $$;
110 0 close(F);
111 }
112  
113 sub read_pid{
114 0 ▶ my ($tmp)=@_;
Subroutine read_pid: not called CC=1 SCAR=6.9
Pod undocumented
115
116 0 my $script=clear_ext($FindBin::RealScript);
117 0 open(F, "$tmp/$script.pid");
118 0 my $pid=<F>;
119 0 $pid=~s/[\n\r]//gs;
120 0 close(F);
121
122 0 return $pid;
123 }
124  
125 sub clear_ext{
126 0 ▶ my ($script)=@_;
Subroutine clear_ext: not called CC=1 SCAR=6.9
Pod undocumented
127
128 0 $script=~s/\..{1,3}$//gs;
129
130 0 return $script;
131 }
132  
133 sub daemonize{
134 1 ▶ my (%params)=@_;
Subroutine daemonize: called CC=5 SCAR=19.9
Pod documented
135
136 1 my $tmp=$params{tmp};
137 1 ▶ $tmp=$FindBin::Bin if (!$tmp || !(-d $tmp));
Branch: 1 branch
BranchTrueFalseTotal
unless $tmp and -d $tmp 1 0 1
Condition33%1/3
$tmp and -d $tmp
!ll&&!rl&&r
100
MC/DC0%0/2
$tmp and -d $tmp
$tmp -d $tmp
Truth table33%1/3
A and B
A= $tmpB= -d $tmp
ABresult
0X0
100
111
138
139 1 my $in=$ARGV[0];
140
141 1 my $funcs_in={
142 'start'=>\&start,
143 'stop'=>\&stop,
144 'restart'=>\&restart,
145 'pid'=>\&pid
146 };
147  
148 1 ▶ if (!$funcs_in->{$in}){
Branch: 1 branch
BranchTrueFalseTotal
if (not $funcs_in->{$in}) { } 1 0 1
149 1 delete $funcs_in->{pid};
150 1 die 'Dont`t know input parameter '."'$in'\nTry to ".join(",", sort(keys(%{$funcs_in})))."\n";
151 } else {
152 0 &{$funcs_in->{$in}}($tmp);
153
154 0 ▶ if ($in ne 'pid'){
Branch: 1 branch
BranchTrueFalseTotal
if ($in ne "pid") 0 0 0
155 0 die "EXIT\n";
156 }
157 }
158 }
159  
160 =head2 daemonize(%params)
161  
162 %options - only one parameter yet tmp=>'/some/tmp/dir', if not exists then tmp dir takes from directory where your script placed.
163 =cut
164  
165 =head1 SEE ALSO
166  
167 L<Daemon::Daemonize>
168  
169 =head1 AUTHOR
170  
171 Bulichev Evgeniy, <F<bes@cpan.org>>.
172  
173 =head1 COPYRIGHT
174  
175 Copyright (c) 2017 Bulichev Evgeniy. All rights reserved.
176 This module is free software; you can redistribute it and/or modify it
177 under the same terms as Perl itself.
178  
179 =cut
180  
181  
182 2 1;
↑ Summary /usr/local/bin/cpancover »