MMM/Daemon.pm

filter: Statementstmt 20.9% 28 / 134 Branchbran 0.0% 0 / 52 Conditioncond 0.0% 0 / 11 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 40.9% 9 / 22 Podpod 100% 3 / 3 totaltotal 17.6% 40 / 227 CC 34 SCAR 39.0
CC
34
Cov
14%
run
132.0
_start_pending_queue
72.0
_create_pid_file
56.0
CRAP 49.4
« MMM/MirrorTask.pm ↑ Summary MMM/Sync/Ftp.pm »
1 package MMM::Daemon;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use Sys::Syslog;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use MMM::Utils;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use MMM::Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Fcntl qw(:flock);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use POSIX qw(:sys_wait_h);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 2 ▶use base qw(MMM);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use base qw(MMM::Report::Mail);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 =head1 NAME
15  
16 MMM::Daemon
17  
18 =head1 SYNOPSIS
19  
20 use MMM::Daemon;
21 my $mmm = MMM::Daemon->new() or die "Cannot find MMM installation";
22 $mmm->run();
23  
24 =head1 DESCRIPTION
25  
26 A daemon for mmm system
27  
28 =head1 METHODS
29  
30 =cut
31  
32 0 ▶sub new {
Subroutine new: not called CC=3 SCAR=24.8
Pod documented
33 0 my ($class, @args) = @_;
34 0 ▶ my $mmm = $class->SUPER::new(@args) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $mmm = $class->SUPER::new(@args) or return 0 0 0
35  
36 0 ▶ if (!$mmm->{nofork}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($mmm->{"nofork"}) 0 0 0
37 0 Sys::Syslog::openlog('mmm', 'pid', $mmm->configval('default', 'syslog_facilities', 'daemon'));
38 0 $mmm->{use_syslog} = 1;
39 }
40  
41 $mmm
42 0 }
43  
44 0 ▶sub _create_pid_file {
Subroutine _create_pid_file: not called CC=7 SCAR=40.3
45 0 my ($self) = @_;
46 0 ▶ if (my $pidf = $self->configval('default', 'pidfile', PIDFILE)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $pidf = $self->configval('default', 'pidfile', PIDFILE())) { } 0 0 0
47 0 ▶ if (open(my $h, '>>', $pidf)) {
Branch: 1 branch
BranchTrueFalseTotal
if (open my $h, '>>', $pidf) { } 0 0 0
48 0 autoflush $h 1;
49 0 ▶ if (flock($h, LOCK_EX | LOCK_NB)) {
Branch: 1 branch
BranchTrueFalseTotal
if (flock $h, 6) { } 0 0 0
50 0 truncate($h, 0);
51 0 print $h "$$\n";
52 0 $self->{lockfh} = $h;
53 } else {
54 0 close($h);
55 0 my $pid;
56 0 ▶ if (open($h, '<', $pidf)) {
Branch: 1 branch
BranchTrueFalseTotal
if (open $h, "<", $pidf) 0 0 0
57 0 ▶ $pid = <$h> || '';
Condition0%0/2
readline $h || ''
l!l
00
MC/DC0%0/1
readline $h || ''
readline $h
Truth table0%0/2
A
A= readline $h
Aresult
11
00
58 0 close($h);
59 }
60 0 chomp($pid);
61 0 ▶ $self->log('WARNING', 'Another mmm seems running pid `%s\'',
Condition0%0/2
$pid || 'N/A'
l!l
00
MC/DC0%0/1
$pid || 'N/A'
$pid
Truth table0%0/2
A
A= $pid
Aresult
11
00
62 $pid || 'N/A');
63 0 return 0;
64 }
65 0 return 1;
66 } else {
67 0 $self->log('WARNING', 'Cannot create pid file `%s\' %s', $pidf, $!);
68 0 return 0
69 }
70 } else {
71 0 $self->log('WARNING', 'No pid file configured');
72 0 return 0;
73 }
74  
75 0 0
76 }
77  
78 0 ▶sub _delete_pid_file {
Subroutine _delete_pid_file: not called CC=4 SCAR=30.0
79 0 my ($self) = @_;
80 0 ▶ if ($self->{lockfh}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"lockfh"}) 0 0 0
81 0 close($self->{lockfh});
82 }
83 0 ▶ if (my $pidf = $self->configval('default', 'pidfile', PIDFILE)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $pidf = $self->configval("default", "pidfile", PIDFILE())) 0 0 0
84 0 ▶ unlink($pidf) or $self->log('WARNING', 'Cannot delete pid file `%s\' %s', $pidf, $!);
Branch: 1 branch
BranchTrueFalseTotal
unlink $pidf or $self->log("WARNING", "Cannot delete pid file `%s' %s", $pidf, $!) 0 0 0
85 }
86 }
87  
88 0 ▶sub _reload_config {
Subroutine _reload_config: not called CC=2 SCAR=17.9
89 0 my ($self) = @_;
90 my $config = Config::IniFiles->new(
91 -file => $self->{configfile},
92 0 -default => 'default',
93 );
94 0 ▶ if ($config) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config) { } 0 0 0
95 0 $self->{config} = $config;
96 0 $self->_parse_config();
97 0 $self->log('NOTICE', 'Configuration reload');
98 0 return 1;
99 } else {
100 0 $self->log('ERROR', 'Failure will reloading configuration');
101 0 return 0;
102 }
103 }
104  
105 0 ▶sub run {
Subroutine run: not called CC=11 SCAR=48.8
Pod documented
106 0 my ($self) = @_;
107  
108  
109 0 ▶ if (!$self->{nofork}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"nofork"}) 0 0 0
110 0 $self->log('DEBUG', 'Going into background');
111 0 my $pid = fork;
112 0 ▶ if (!defined($pid)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $pid) 0 0 0
113 0 return;
114 }
115  
116 0 ▶ if ($pid) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pid) 0 0 0
117 0 sleep 1;
118 0 my $ret = waitpid($pid, &WNOHANG);
119 0 ▶ if ($ret) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ret) 0 0 0
120 0 die "Daemon has stopped, check log (exit with $ret status)\n";
121 }
122 0 exit(0);
123 }
124 }
125  
126 0 ▶ $self->_create_pid_file() or return;
Branch: 1 branch
BranchTrueFalseTotal
$self->_create_pid_file or return 0 0 0
127  
128 0 $0 = 'mmm (DAEMON)';
129 0 $self->log('NOTICE', 'MMM::Daemon started at pid %d', $$);
130 0 $self->{dontdie} = 1;
131 0 ▶ $SIG{'ALRM'} = sub { $self->{next_alarm} = 0; $self->_start_pending_queue() };
Subroutine __ANON__: not called CC=1 SCAR=6.9
132 0 ▶ $SIG{'TERM'} = sub { $self->{dontdie} = 0 };
Subroutine __ANON__: not called CC=1 SCAR=6.9
133 0 ▶ $SIG{'INT'} = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
134 0 $self->log('INFO', 'Ctrl+C received, stoping');
135 0 $self->{dontdie} = 0;
136 0 };
137 0 ▶ $SIG{'HUP'} = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
138 0 alarm(0);
139 0 $self->_reload_config;
140 0 $self->{next_alarm} = 0;
141 0 $self->_start_pending_queue();
142 0 };
143 0 ▶ $SIG{'CHLD'} = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
144 0 $self->log('DEBUG', 'SIG CHILD received');
145 0 $self->_reap_child();
146 0 };
147 0 $self->_start_pending_queue();
148 0 ▶ while ($self->{dontdie}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"dontdie"}) 0 0 0
149 0 $self->_reap_child();
150 0 ▶ $self->log('DEBUG', 'next alarm in %ss', ($self->{next_alarm} ? $self->{next_alarm} - scalar(time): 'N/A'));
Branch: 1 branch
BranchTrueFalseTotal
$self->{'next_alarm'} ? : 0 0 0
151 0 ▶ sleep(3600) if ($self->{dontdie});
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"dontdie"} 0 0 0
152 }
153 0 ▶ foreach (keys %{ $self->{process} || {} }) {
Condition0%0/2
$self->{'process'} || {}
l!l
00
MC/DC0%0/1
$self->{'process'} || {}
$self->{'process'}
Truth table0%0/2
A
A= $self->{'process'}
Aresult
11
00
154 0 kill 15, $_;
155 }
156 0 $self->_delete_pid_file();
157 0 1;
158 }
159  
160 0 ▶sub post_process {
Subroutine post_process: not called CC=1 SCAR=6.9
Pod documented
161 0 my ($self, $job) = @_;
162 0 $self->log('DEBUG', 'Post process %s', $job->name);
163 0 $self->_start_pending_queue();
164 }
165  
166 0 ▶sub _set_alarm {
Subroutine _set_alarm: not called CC=5 SCAR=34.0
167 0 my ($self, $when) = @_;
168 0 ▶ ($self->{next_alarm} || 0) != 0 && $when >= $self->{next_alarm} and return;
Branch: 1 branch
BranchTrueFalseTotal
($self->{"next_alarm"} || 0) != 0 and $when >= $self->{"next_alarm"} and return 0 0 0
Condition0%0/5
($self->{"next_alarm"} || 0) != 0 and $when >= $self->{"next_alarm"}
!ll&&!rl&&r
000
$self->{'next_alarm'} || 0
l!l
00
MC/DC0%0/2
($self->{"next_alarm"} || 0) != 0 and $when >= $self->{"next_alarm"}
$self->{'next_alarm'} $when >= $self->{"next_alarm"}
Truth table0%0/3
A and B
A= $self->{'next_alarm'}B= $when >= $self->{"next_alarm"}
ABresult
0X0
100
111
169 0 $self->log('DEBUG', 'Re-Alarm for %d, in %ds', $when, $when - scalar(time));
170 0 $self->{next_alarm} = $when;
171 0 my $s_alarm = $when - scalar(time);
172 0 ▶ alarm($s_alarm > 0 ? $s_alarm : 1);
Branch: 1 branch
BranchTrueFalseTotal
$s_alarm > 0 ? : 0 0 0
173 }
174  
175 0 ▶sub _start_pending_queue {
Subroutine _start_pending_queue: not called CC=8 SCAR=42.8
176 0 my ($self) = @_;
177 0 ▶ $self->{dontdie} or return;
Branch: 1 branch
BranchTrueFalseTotal
$self->{"dontdie"} or return 0 0 0
178 0 my $waitdelay = 0;
179 0 foreach my $job ($self->get_tasks_by_name($self->list_tasks)) {
180 0 ▶ $job->is_disable and next;
Branch: 1 branch
BranchTrueFalseTotal
$job->is_disable and next 0 0 0
181 0 ▶ $job->frequency or next;
Branch: 1 branch
BranchTrueFalseTotal
$job->frequency or next 0 0 0
182 0 my $next_run_time = $job->next_run_time;
183 0 ▶ if ($next_run_time <= scalar(time)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($next_run_time <= scalar time) { } 0 0 0
184 0 $self->_run_fork($job, 'NOTRAPLOG');
185 0 ▶ sleep(5) if($waitdelay); # small wait to avoid excessive load
Branch: 1 branch
BranchTrueFalseTotal
if $waitdelay 0 0 0
186 0 $waitdelay = 1;
187 } else {
188 0 ▶ $self->log('DEBUG', '$when < 0: %d %s', $next_run_time, $job->name) if ($next_run_time <= 0);
Branch: 1 branch
BranchTrueFalseTotal
if $next_run_time <= 0 0 0 0
189 0 $self->_set_alarm($next_run_time);
190 }
191 }
192 }
193  
194 2 1;
195  
196 __END__
« MMM/MirrorTask.pm ↑ Summary MMM/Sync/Ftp.pm »