MMM/MirrorTask.pm

filter: Statementstmt 9.1% 25 / 272 Branchbran 0.0% 0 / 124 Conditioncond 0.0% 0 / 39 MC/DCmcdc 0.0% 0 / 25 Subroutinesub 24.2% 8 / 33 Podpod 100% 15 / 15 totaltotal 9.4% 48 / 508 CC 86 SCAR 54.3
CC
86
Cov
6%
sync
702.0
_sync_url
342.0
next_run_time
90.0
CRAP 228.8
« MMM/Report/Html.pm ↑ Summary MMM/Daemon.pm »
1 package MMM::MirrorTask;
2  
3 =head1 NAME
4  
5 MMM::MirrorTask Class to store mirror task function and data
6  
7 =cut
8  
9 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶use MMM::Sync;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 4 ▶use MMM::Utils;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 4 ▶use MMM::Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 4 ▶use MMM::Mirror;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 4 ▶use Fcntl qw(:flock);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 4 ▶use Digest::MD5;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 =head1 METHODS
19  
20 =head2 new
21  
22 =cut
23  
24 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
Pod documented
25 0 my ($class, $mmm, $name, %options) = @_;
26 0 bless(
27 {
28 mmm => $mmm,
29 name => $name,
30 options => { %options },
31 lockcount => 0,
32 },
33 $class
34 );
35 }
36  
37 0 ▶sub DESTROY {
Subroutine DESTROY: not called CC=1 SCAR=6.9
38 0 my ($self) = @_;
39 0 $self->{lockcount} = 1; # Force unlock, can't happen
40 0 $self->unlock();
41 }
42  
43 =head2 name
44  
45 Return the name of the current task
46  
47 =cut
48  
49 0 ▶sub name {
Subroutine name: not called CC=1 SCAR=6.9
Pod documented
50 $_[0]->{name}
51 0 }
52  
53 =head2 is_disable
54  
55 Return true if the current task is disable
56  
57 =cut
58  
59 0 ▶sub is_disable { yes_no($_[0]->val('disable')) }
Subroutine is_disable: not called CC=1 SCAR=6.9
Pod documented
60  
61 =head2 val( $var, $default)
62  
63 Return the configuration value for $var. Return $default if parameter
64 is not set in the config
65  
66 =cut
67  
68 0 ▶sub val {
Subroutine val: not called CC=1 SCAR=6.9
Pod documented
69 0 my ( $self, $var, $default ) = @_;
70 0 $self->{mmm}->{config}->val( $self->name, $var, $default );
71 }
72  
73 =head2 frequency
74  
75 Return the frequency value from config
76  
77 =cut
78  
79 0 ▶sub frequency {
Subroutine frequency: not called CC=1 SCAR=6.9
Pod documented
80 0 my ($self) = @_;
81 0 duration2m($self->val( 'period', PERIOD ));
82 }
83  
84 0 ▶sub _set_status_time {
Subroutine _set_status_time: not called CC=1 SCAR=6.9
85 0 my ($config, $section, $var, $val) = @_;
86 0 $config->newval($section, $var, $val);
87 0 $config->SetParameterComment(
88 $section, $var, scalar(gmtime($val))
89 );
90 }
91  
92 =head2 state_info
93  
94 Return a hashref about job status
95  
96 =cut
97  
98 0 ▶sub state_info {
Subroutine state_info: not called CC=5 SCAR=34.0
Pod documented
99 0 my ($self, $status) = @_;
100 0 ▶ $status ||= $self->_get_status();
Condition0%0/3
$status ||= $self->_get_status
l!l&&r!l&&!r
000
MC/DC0%0/2
$status ||= $self->_get_status
$status $self->_get_status
Truth table0%0/3
A ||= B
A= $statusB= $self->_get_status
ABresult
1X1
011
000
101 0 my %info = ();
102 0 foreach my $section (qw(job success failure)) {
103 0 foreach my $var ($status->Parameters($section)) {
104 0 $info{$section}{$var} = $status->val($section, $var);
105 }
106 }
107 0 ▶ if (yes_no($self->val('compute_size', 0))) {
Branch: 1 branch
BranchTrueFalseTotal
if (yes_no($self->val('compute_size', 0))) { } 0 0 0
108 0 $info{job}{size} = $status->val('job', 'size');
109 } else {
110 0 delete($info{job}{size});
111 }
112  
113 0 $info{job}{error_log} = [ grep { $_ } ($status->val('job', 'error_log')) ];
114 0 $info{job}{next_run_time} = $self->next_run_time($status);
115 0 $info{job}{is_running} = $self->is_running;
116 0 return %info;
117 }
118  
119 0 ▶sub _compute_config_sum {
Subroutine _compute_config_sum: not called CC=2 SCAR=17.9
120 0 my ($self) = @_;
121 0 my $md5 = Digest::MD5->new();
122 0 foreach (qw(url source path)) {
123 0 $md5->add("$_=");
124 0 $md5->add(join("\n", $self->val('job', $_, '')));
125 }
126 0 return $md5->hexdigest
127 }
128  
129 0 ▶sub _set_compute_config_sum {
Subroutine _set_compute_config_sum: not called CC=3 SCAR=24.8
130 0 my ($self, $status) = @_;
131 0 ▶ $status ||= $self->_get_status();
Condition0%0/3
$status ||= $self->_get_status
l!l&&r!l&&!r
000
MC/DC0%0/2
$status ||= $self->_get_status
$status $self->_get_status
Truth table0%0/3
A ||= B
A= $statusB= $self->_get_status
ABresult
1X1
011
000
132 0 my $newsum = $self->_compute_config_sum();
133 0 ▶ if ($status->val('job', 'config_sum', '') ne $newsum) {
Branch: 1 branch
BranchTrueFalseTotal
if ($status->val('job', 'config_sum', '') ne $newsum) { } 0 0 0
134 0 $status->newval('job', 'config_sum', $newsum);
135 0 return 1;
136 } else {
137 0 return 0;
138 }
139 }
140  
141 =head2 next_run_time
142  
143 Return the time (in second) when the next run should be performed
144  
145 =cut
146  
147 0 ▶sub next_run_time {
Subroutine next_run_time: not called CC=9 SCAR=45.0
Pod documented
148 0 my ($self, $status) = @_;
149 0 my @alltime = ( scalar( time() ) );
150 0 ▶ $status ||= $self->_get_status();
Condition0%0/3
$status ||= $self->_get_status
l!l&&r!l&&!r
000
MC/DC0%0/2
$status ||= $self->_get_status
$status $self->_get_status
Truth table0%0/3
A ||= B
A= $statusB= $self->_get_status
ABresult
1X1
011
000
151 0 my $last_start = 0;
152 0 ▶ if ($self->_compute_config_sum() ne $status->val('job', 'config_sum', '')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_compute_config_sum ne $status->val('job', 'config_sum', '')) { } 0 0 0
153 0 ▶ $self->_log('INFO',
Branch: 1 branch
BranchTrueFalseTotal
if $status->val("job", "start", 0) 0 0 0
154 'Config for has changed, need to be run immediately')
155 if($status->val('job', 'start', 0));
156 } else {
157 0 $last_start = $status->val( 'job' , 'start', 0 );
158 }
159  
160 0 ▶ if ( $last_start ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($last_start) 0 0 0
161 0 push( @alltime, $last_start + ( $self->frequency * 60 ) );
162 }
163  
164 0 ▶ if ( $self->val('waitafter', WAITAFTER_MINIMA)
Branch: 1 branch
BranchTrueFalseTotal
if ($self->val("waitafter", WAITAFTER_MINIMA()) and $status->val("job", "end")) 0 0 0
Condition0%0/3
$self->val("waitafter", WAITAFTER_MINIMA()) and $status->val("job", "end")
!ll&&!rl&&r
000
MC/DC0%0/2
$self->val("waitafter", WAITAFTER_MINIMA()) and $status->val("job", "end")
$self->val("waitafter", WAITAFTER_MINIMA()) $status->val("job", "end")
Truth table0%0/3
A and B
A= $self->val("waitafter", WAITAFTER_MINIMA())B= $status->val("job", "end")
ABresult
0X0
100
111
165 && $status->val( 'job' , 'end' ) )
166 {
167 0 push( @alltime,
168 $status->val( 'job', 'end' ) +
169 $self->val('waitafter', WAITAFTER_MINIMA) * 60 );
170 }
171  
172 0 ▶ if ( $self->val('waitaftersuccess')
Branch: 1 branch
BranchTrueFalseTotal
if ($self->val("waitaftersuccess") and $status->val("success", "end")) 0 0 0
Condition0%0/3
$self->val("waitaftersuccess") and $status->val("success", "end")
!ll&&!rl&&r
000
MC/DC0%0/2
$self->val("waitaftersuccess") and $status->val("success", "end")
$self->val("waitaftersuccess") $status->val("success", "end")
Truth table0%0/3
A and B
A= $self->val("waitaftersuccess")B= $status->val("success", "end")
ABresult
0X0
100
111
173 && $status->val( 'success', 'end' ) )
174 {
175 0 push( @alltime,
176 $status->val( 'job', 'success' ) +
177 $self->val('waitaftersuccess') * 60 );
178 }
179  
180 0 my ($t) = sort { $b <=> $a } @alltime;
181 # $self->_log('DEBUG', 'Next run time for %s is %d (in %d), frequency is %d',
182 # $self->{name}, $t, $t - scalar(time()),
183 # $self->frequency,
184 #);
185  
186 0 $t;
187 }
188  
189 =head2 source
190  
191 Return the source associate to the list, if any
192  
193 =cut
194  
195 0 ▶sub source {
Subroutine source: not called CC=2 SCAR=17.9
Pod documented
196 0 ▶ $_[0]->val('source') || '';
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->val("source") or "" 0 0 0
197 }
198  
199 0 ▶sub _log {
Subroutine _log: not called CC=1 SCAR=6.9
200 0 my ($self, $level, $message, @args) = @_;
201 $self->{mmm}->log(
202 0 $level,
203 sprintf('[%s] %s', $self->name, $message),
204 @args
205 );
206 }
207  
208 0 ▶sub _lockpath {
Subroutine _lockpath: not called CC=1 SCAR=6.9
209 0 my ($self) = @_;
210 0 my $lockfile = $self->name;
211 0 $lockfile =~ s:/:_:g;
212 0 join('/', ($self->{mmm}->statedir, "/$lockfile.lck"));
213 }
214  
215 0 ▶sub _statusfile {
Subroutine _statusfile: not called CC=1 SCAR=6.9
216 0 my ($self) = @_;
217 0 my $lockfile = $self->name;
218 0 $lockfile =~ s:/:_:g;
219 0 join('/', ($self->{mmm}->statedir, $lockfile));
220 }
221  
222 =head2 getlock($share)
223  
224 Try to lock the lockfile for this task, in shared mode if
225 $share is set.
226  
227 =cut
228  
229 0 ▶sub getlock {
Subroutine getlock: not called CC=8 SCAR=42.8
Pod documented
230 0 my ($self, $share) = @_;
231 0 ▶ if ($self->{lockcount}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"lockcount"}) 0 0 0
232 0 $self->{lockcount}++;
233 $self->_log(
234 'DEBUG',
235 'Lock is already done, counter is now %d', $self->{lockcount}
236 0 );
237 0 return $self->{lockcount};
238 }
239 0 $self->_log( 'DEBUG', 'Trying to acquire lock' );
240 0 $self->{lockfile} = $self->_lockpath;
241 0 ▶ if ( open( $self->{lockfh}, $share ? '<' : '>', $self->_lockpath) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (open $self->{'lockfh'}, $share ? '<' : '>', $self->_lockpath) { } 0 0 0
$share ? : 0 0 0
242 0 ▶ if ( !flock(
Branch: 2 branches
BranchTrueFalseTotal
unless (flock $self->{"lockfh"}, 4 | ($share ? 1 : 2)) 0 0 0
$share ? : 0 0 0
243 $self->{lockfh}, LOCK_NB | ( $share ? LOCK_SH : LOCK_EX ) ) ) {
244 0 ▶ if ( ( $! + 0 ) != 11 ) { # E_AGAIN, does this is really need
Branch: 1 branch
BranchTrueFalseTotal
if ($! + 0 != 11) 0 0 0
245 0 $self->_log(
246 'FATAL',
247 "Cannot lock file %s",
248 $self->_lockpath
249 );
250 0 unlink( $self->_lockpath );
251 0 close( $self->{lockfh} );
252 0 return;
253 }
254 0 $self->_log( 'DEBUG', 'is already lock' );
255 0 return;
256 }
257 0 my $fh = $self->{lockfh};
258 0 ▶ print $fh "$$\n" unless($share);
Branch: 1 branch
BranchTrueFalseTotal
unless $share 0 0 0
259 }
260 else {
261 $self->_log( 'FATAL',
262 0 'Cannot open lock file %s :%s', $self->{lockfile}, $!);
263 0 return;
264 }
265 0 ++$self->{lockcount};
266 }
267  
268 =head2 unlock
269  
270 Release the lock for the task
271  
272 =cut
273  
274 0 ▶sub unlock {
Subroutine unlock: not called CC=3 SCAR=24.8
Pod documented
275 0 my ($self) = @_;
276 0 ▶ $self->{lockfh} or return;
Branch: 1 branch
BranchTrueFalseTotal
$self->{"lockfh"} or return 0 0 0
277 0 ▶ --$self->{lockcount} and return;
Branch: 1 branch
BranchTrueFalseTotal
--$self->{"lockcount"} and return 0 0 0
278 0 unlink( $self->{lockfile} );
279 0 close( $self->{lockfh} );
280 }
281  
282 =head2 is_running
283  
284 Return true is the task is running (lock check)
285  
286 =cut
287  
288 0 ▶sub is_running {
Subroutine is_running: not called CC=6 SCAR=37.4
Pod documented
289 0 my ($self) = @_;
290 0 my @stat = stat($self->_lockpath);
291 0 ▶ if ($self->{mmm}->_task_is_registred($self->name)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"mmm"}->_task_is_registred($self->name)) 0 0 0
292 0 ▶ return $stat[9] || scalar(time);
Condition0%0/2
$stat[9] || scalar time
l!l
00
MC/DC0%0/1
$stat[9] || scalar time
$stat[9]
Truth table0%0/2
A
A= $stat[9]
Aresult
11
00
293 }
294  
295 0 ▶ if (!defined($stat[9])) { return }
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $stat[9]) { } 0 0 0
296 else {
297 0 my $res = $self->getlock(1);
298 0 ▶ if ($res) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res) { } 0 0 0
299 0 $self->unlock;
300 0 ▶ if ($res > 1) { return $stat[9]; }
Branch: 1 branch
BranchTrueFalseTotal
if ($res > 1) { } 0 0 0
301 0 else { return; }
302 0 } else { return $stat[9]; }
303 }
304 }
305  
306 0 ▶sub _get_status {
Subroutine _get_status: not called CC=3 SCAR=24.8
307 0 my ($self) = @_;
308 0 ▶ Config::IniFiles->new(
Branch: 2 branches
BranchTrueFalseTotal
"Config::IniFiles"->new(-f $self->_statusfile ? ("-file", $self->_statusfile) : ()) or "Config::IniFiles"->new 0 0 0
-f $self->_statusfile ? : 0 0 0
309 -f $self->_statusfile ? ( -file => $self->_statusfile ) : ()
310 ) || Config::IniFiles->new();
311 }
312  
313 0 ▶sub _write_status {
Subroutine _write_status: not called CC=1 SCAR=6.9
314 0 my ($self, $status) = @_;
315 0 $self->_log( 'DEBUG', 'Write status file: %s', $self->_statusfile );
316 0 $status->WriteConfig( $self->_statusfile );
317 }
318  
319 =head2 failure_count
320  
321 Return three values:
322 - the count of failure since last success
323 - the previous failure count
324 - and if this count has change between the two previous run
325 (eg if failure count is different of previous failure count)
326  
327 =cut
328  
329 0 ▶sub failure_count {
Subroutine failure_count: not called CC=6 SCAR=37.4
Pod documented
330 0 my ($self, $status) = @_;
331 my $before = defined($self->{successive_failure_before})
332 ? $self->{successive_failure_before}
333 0 ▶ : ($status ||= $self->_get_status())->val('job', 'old_failure_count', 0);
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'successive_failure_before'} ? : 0 0 0
Condition0%0/3
$status ||= $self->_get_status
l!l&&r!l&&!r
000
MC/DC0%0/2
$status ||= $self->_get_status
$status $self->_get_status
Truth table0%0/3
A ||= B
A= $statusB= $self->_get_status
ABresult
1X1
011
000
334 my $after = defined($self->{successive_failure_after})
335 ? $self->{successive_failure_after}
336 0 ▶ : ($status ||= $self->_get_status())->val('job', 'successive_failure_count', 0);
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'successive_failure_after'} ? : 0 0 0
Condition0%0/3
$status ||= $self->_get_status
l!l&&r!l&&!r
000
MC/DC0%0/2
$status ||= $self->_get_status
$status $self->_get_status
Truth table0%0/3
A ||= B
A= $statusB= $self->_get_status
ABresult
1X1
011
000
337 return(
338 0 ▶ $before, $after, defined($after) ? $after != $before : undef
Branch: 1 branch
BranchTrueFalseTotal
defined $after ? : 0 0 0
339 );
340 }
341  
342 =head2 sync
343  
344 Perform the synchronization
345  
346 =cut
347  
348 0 ▶sub sync {
Subroutine sync: not called CC=26 SCAR=65.5
Pod documented
349 0 my ($self) = @_;
350 0 $self->_log('INFO', 'Start to process' );
351 $self->_log('DEBUG', 'goes into %s%s',
352 $self->dest,
353 0 ▶ $self->{options}{dryrun} ? ' (dryrun mode)' : '',
Branch: 1 branch
BranchTrueFalseTotal
$self->{'options'}{'dryrun'} ? : 0 0 0
354 );
355  
356 0 my $oldname = $0;
357 0 $0 = 'mmm [' . $self->name . ']';
358
359 0 ▶ $self->getlock() or return;
Branch: 1 branch
BranchTrueFalseTotal
$self->getlock or return 0 0 0
360  
361 0 my $status = $self->_get_status();
362 0 $self->_set_compute_config_sum($status);
363 0 my ($ouid, $ogid) = MMM::Utils::setid( $self->val('user'), $self->val('group') );
364  
365 $self->{successive_failure_before} =
366 0 $status->val('job', 'successive_failure_count', 0);
367 0 $status->newval('job', 'old_failure_count', $self->{successive_failure_before});
368 0 $status->delval( 'job', 'command');
369 0 $status->newval('job', 'processed_count',
370 $status->val('job', 'processed_count', 0) + 1
371 );
372  
373 0 ▶ if (!defined($status->val('job', 'first_sync'))) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $status->val("job", "first_sync")) 0 0 0
374 0 _set_status_time($status, 'job', 'first_sync', scalar( time() ));
375 }
376  
377 0 my $res = 0;
378  
379 0 ▶ if (! -d $self->dest) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $self->dest) 0 0 0
380 0 push(@{ $self->{output} }, sprintf('Directory %s does not exists (%s)',
381 $self->dest,
382 $self->name,
383 ));
384 0 foreach (qw(start end)) {
385 0 _set_status_time($status, 'job', $_, scalar( time() ));
386 }
387 0 return $res;
388 }
389  
390 0 ▶ if ($self->val('pre_exec')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->val("pre_exec")) 0 0 0
391 0 my @cmd = ($self->val('pre_exec'), $self->name, $self->dest);
392 $self->_log('INFO', 'Executing PRE: %s',
393 0 join(' ', map { qq{"$_"} } (@cmd)));
394 0 ▶ if (system(@cmd) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (system(@cmd) != 0) 0 0 0
395 0 ▶ if ($? == -1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? == -1) { } 0 0 0
396 0 $self->_log('ERROR', 'failed to execute pre_exec: %s', $!);
397 } else {
398 0 $self->_log('ERROR',
399 'Pre_exec exited with value %d, abborting sync', $? >> 8);
400 }
401 0 return $res;
402 }
403 }
404  
405 0 ▶ if (my $url = $self->val('url')) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $url = $self->val('url')) { } 0 0 0
406 0 ▶ $res = $self->_sync_url(
Condition0%0/2
$self->val('password') || undef
l!l
00
MC/DC0%0/1
$self->val('password') || undef
$self->val('password')
Truth table0%0/2
A
A= $self->val('password')
Aresult
11
00
407 $status, $url,
408 password => $self->val('password') || undef,
409 use_ssh => yes_no($self->val('rsync_use_ssh')),
410 );
411 } else {
412 0 $self->_log('ERROR', 'No source or url' );
413 0 return $res
414 }
415  
416 0 ▶ $status->newval('job', 'success', $res ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
$res ? : 0 0 0
417 0 ▶ if ($res) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res) { } 0 0 0
418 $self->_log('NOTICE', 'Sync done%s from %s',
419 0 ▶ $self->{options}{dryrun} ? ' (dryrun mode)' : '',
Branch: 1 branch
BranchTrueFalseTotal
$self->{'options'}{'dryrun'} ? : 0 0 0
420 $status->val('success', 'sync_from'),
421 );
422 0 $status->newval('job', 'successive_failure_count', 0);
423 } else {
424 0 $self->_log('WARNING', 'Unable to sync');
425 0 $status->newval('job', 'successive_failure_count',
426 $status->val('job', 'successive_failure_count', 0) + 1
427 );
428 0 ▶ foreach (@{ $self->{output} ? $self->{output} : [ "No output from process" ]}) {
Branch: 1 branch
BranchTrueFalseTotal
$self->{'output'} ? : 0 0 0
429 0 $self->_log('ERROR', $_);
430 }
431 }
432 $self->{successive_failure_after} =
433 0 $status->val('job', 'successive_failure_count', 0);
434  
435 0 ▶ if ($self->val('post_exec')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->val("post_exec")) 0 0 0
436 0 $ENV{MMM_RESULT} = $res;
437 0 ▶ if ($res) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res) 0 0 0
438 0 $ENV{MMM_FROM} = $status->val('success', 'sync_from');
439 0 $ENV{MMM_MIRROR} = $status->val('job', 'try_from');
440 }
441 0 my @cmd = ($self->val('post_exec'), $self->name, $self->dest);
442 $self->_log('INFO', 'Executing POST: %s',
443 0 join(' ', map { qq{"$_"} } (@cmd)));
444 0 ▶ if (system(@cmd) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (system(@cmd) != 0) 0 0 0
445 0 ▶ if ($? == -1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? == -1) { } 0 0 0
446 0 $self->_log('WARNING', 'failed to execute post_exec: %s', $!);
447 } else {
448 0 $self->_log('WARNING',
449 'Post_exec exited with value %d, abborting sync', $? >> 8);
450 }
451 }
452 }
453  
454 0 ▶ if (yes_no($self->val('compute_size', 0)) &&
Branch: 1 branch
BranchTrueFalseTotal
if (yes_no($self->val("compute_size", 0)) and scalar time > $status->val("job", "size_time", 0) + duration2m($self->val("size_delay", SIZE_DELAY()) * 60)) 0 0 0
Condition0%0/3
yes_no($self->val("compute_size", 0)) and scalar time > $status->val("job", "size_time", 0) + duration2m($self->val("size_delay", SIZE_DELAY()) * 60)
!ll&&!rl&&r
000
MC/DC0%0/2
yes_no($self->val("compute_size", 0)) and scalar time > $status->val("job", "size_time", 0) + duration2m($self->val("size_delay", SIZE_DELAY()) * 60)
yes_no($self->val("compute_size", 0)) scalar time > $status->val("job", "size_time", 0) + duration2m($self->val("size_delay", SIZE_DELAY()) * 60)
Truth table0%0/3
A and B
A= yes_no($self->val("compute_size", 0))B= scalar time > $status->val("job", "size_time", 0) + duration2m($self->val("size_delay", SIZE_DELAY()) * 60)
ABresult
0X0
100
111
455 scalar(time) > $status->val('job', 'size_time', 0) +
456 duration2m($self->val('size_delay', SIZE_DELAY) * 60)) {
457 0 $self->du_dest($status);
458 }
459  
460 0 MMM::Utils::setid($ouid, $ogid);
461  
462 0 ▶ $self->_write_status($status) unless($self->{options}{dryrun});
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"options"}{"dryrun"} 0 0 0
463  
464 0 ▶ if ($self->{mmm}->can('send_mail')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"mmm"}->can("send_mail")) 0 0 0
465 0 ▶ if ($status->val('job', 'old_failure_count', 0) !=
Branch: 1 branch
BranchTrueFalseTotal
if ($status->val("job", "old_failure_count", 0) != $status->val("job", "successive_failure_count", 0) and grep {$status->val("job", "successive_failure_count", 0) == $_;} 0, $self->val("errors_mail", 3)) 0 0 0
Condition0%0/3
$status->val("job", "old_failure_count", 0) != $status->val("job", "successive_failure_count", 0) and grep {$status->val("job", "successive_failure_count", 0) == $_;} 0, $self->val("errors_mail", 3)
!ll&&!rl&&r
000
MC/DC0%0/2
$status->val("job", "old_failure_count", 0) != $status->val("job", "successive_failure_count", 0) and grep {$status->val("job", "successive_failure_count", 0) == $_;} 0, $self->val("errors_mail", 3)
$status->val("job", "old_failure_count", 0) != $status->val("job", "successive_failure_count", 0) grep {$status->val("job", "successive_failure_count", 0) == $_;} 0, $self->val("errors_mail", 3)
Truth table0%0/3
A and B
A= $status->val("job", "old_failure_count", 0) != $status->val("job", "successive_failure_count", 0)B= grep {$status->val("job", "successive_failure_count", 0) == $_;} 0, $self->val("errors_mail", 3)
ABresult
0X0
100
111
466 $status->val('job', 'successive_failure_count', 0) &&
467 0 grep { $status->val('job', 'successive_failure_count', 0) == $_ }
468 (0, $self->val('errors_mail', 3))
469 ) {
470  
471 0 $self->{mmm}->body_queue($self, $self, $self->state_info($status));
472 0 $self->{mmm}->send_mail();
473 }
474 }
475  
476 0 $self->unlock();
477 0 $0 = $oldname;
478 0 $res
479 }
480  
481 0 ▶sub _sync_url {
Subroutine _sync_url: not called CC=18 SCAR=58.3
482 0 my ($self, $status, $based_url, %options) = @_;
483  
484 0 my $url = $based_url;
485 0 ▶ $url =~ m:/$: or $url .= '/';
Branch: 1 branch
BranchTrueFalseTotal
$url =~ m[/$] or $url .= '/' 0 0 0
486 0 ▶ $url .= '/' . $self->val('subdir') if ($self->val('subdir'));
Branch: 1 branch
BranchTrueFalseTotal
if $self->val("subdir") 0 0 0
487 0 ▶ $url =~ m:/$: or $url .= '/';
Branch: 1 branch
BranchTrueFalseTotal
$url =~ m[/$] or $url .= '/' 0 0 0
488  
489 0 $self->_log('DEBUG', 'Try from mirror %s', $url);
490 0 _set_status_time($status, 'job', 'start', scalar( time() ));
491  
492 0 foreach my $val (
493 'bwlimit', # bandwidth limit in k
494 'timeout', # timeout
495 'rsync_opts', # specifics rsync options
496 'rsync_defaults', # defaults rsync options
497 'exclude', # excluded files/dir
498 'tempdir',
499 'partialdir',
500 ) {
501 0 ▶ if (my $v = $self->val($val)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $v = $self->val($val)) 0 0 0
502 0 $options{$val} = $v;
503 }
504 }
505 0 foreach my $val (
506 'delete-after', # deleting after ?
507 'delete', # delete removed files ?
508 'delete-excluded', # deleting excluded files ?
509 ) {
510 0 $options{$val} = yes_no($self->val($val));
511 }
512 0 my $sync = MMM::Sync->new(
513 $url,
514 $self->dest,
515 %options,
516 );
517  
518 0 ▶ if (my $m = MMM::Mirror->new(url => $url)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $m = "MMM::Mirror"->new("url", $url)) 0 0 0
519 0 $status->newval('job', 'try_from', $m->host);
520 }
521  
522 0 my $sync_res;
523 0 my $max_try = $self->val( 'max_try', MAX_TRY );
524 0 $self->_log('DEBUG', 'running %s', join(' ', $sync->buildcmd()));
525 0 foreach my $trycount (1 .. $max_try) {
526 0 $sync->reset_output;
527 0 ▶ if ( $self->{options}{dryrun} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'options'}{'dryrun'}) { } 0 0 0
528 0 $sync_res = 0;
529 0 sleep(10);
530 } else {
531 0 $sync_res = $sync->sync();
532 }
533 $self->_log($sync_res ? 'WARNING' : 'DEBUG',
534 'Try %d/%d, res: %d from mirror %s',
535 $trycount, $max_try, $sync_res,
536 $sync->{source}, # TODO: kill intrusive var access
537 0 ▶ );
Branch: 1 branch
BranchTrueFalseTotal
$sync_res ? : 0 0 0
538 0 $self->{output} = $sync->get_output;
539 0 ▶ if ($sync_res != 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sync_res != 1) 0 0 0
540 last
541 0 }
542 }
543  
544 0 $status->newval( 'job', 'command', join(' ', $sync->buildcmd) );
545  
546 0 _set_status_time($status, 'job', 'end', scalar( time() ) );
547  
548 0 ▶ my $concerned_section = $sync_res ? 'failure' : 'success';
Branch: 1 branch
BranchTrueFalseTotal
$sync_res ? : 0 0 0
549  
550 0 foreach (qw(start end)) {
551 0 _set_status_time($status, $concerned_section, $_,
552 $status->val('job', $_)
553 );
554 }
555 0 $status->newval($concerned_section, 'url', $url);
556 0 $status->newval($concerned_section, 'try_from', $status->val('job', 'try_from'));
557  
558 0 ▶ if ($sync_res == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sync_res == 0) { } 0 0 0
559 0 $status->delval( 'job', 'error_log' );
560 0 $status->newval('success', 'sync_from', $status->val('job', 'try_from'));
561 } else {
562 0 ▶ if (@{ $self->{output} || []}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->{"output"} or [];}) 0 0 0
Condition0%0/2
$self->{'output'} || []
l!l
00
MC/DC0%0/1
$self->{'output'} || []
$self->{'output'}
Truth table0%0/2
A
A= $self->{'output'}
Aresult
11
00
563 $status->newval('job', 'error_log',
564 0 (@{ $self->{output}} > 10)
565 0 ? (@{$self->{output}}[-9 .. -1], '...')
566 0 ▶ : (@{$self->{output}})
Branch: 1 branch
BranchTrueFalseTotal
@{$self->{'output'};} > 10 ? : 0 0 0
567 );
568 }
569 0 return 0;
570 }
571 }
572  
573 =head2 dest
574  
575 The destination directory for this mirror
576  
577 =cut
578  
579 0 ▶sub dest {
Subroutine dest: not called CC=1 SCAR=6.9
Pod documented
580 0 my ($self) = @_;
581 0 return $self->val('path', $self->name);
582 }
583  
584 =head2 du_dest
585  
586 Perform du over the destination directory and store the result into status file
587  
588 =cut
589  
590 0 ▶sub du_dest {
Subroutine du_dest: not called CC=7 SCAR=40.3
Pod documented
591 0 my ($self, $status) = @_;
592 0 ▶ $status ||= $self->_get_status();
Condition0%0/3
$status ||= $self->_get_status
l!l&&r!l&&!r
000
MC/DC0%0/2
$status ||= $self->_get_status
$status $self->_get_status
Truth table0%0/3
A ||= B
A= $statusB= $self->_get_status
ABresult
1X1
011
000
593 0 $self->_log('DEBUG', 'Calculating size of %s', $self->dest);
594 0 ▶ $self->getlock() or return;
Branch: 1 branch
BranchTrueFalseTotal
$self->getlock or return 0 0 0
595 0 ▶ if (! -d $self->dest) { return }
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $self->dest) 0 0 0
596 0 ▶ open(my $handle, sprintf('\\du -s %s |', $self->dest)) or return;
Branch: 1 branch
BranchTrueFalseTotal
open my $handle, sprintf("\\du -s %s |", $self->dest) or return 0 0 0
597 0 my $line = <$handle>;
598 0 ▶ if ($line && $line =~ /^(\d+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line and $line =~ /^(\d+)/) 0 0 0
Condition0%0/3
$line and $line =~ /^(\d+)/
!ll&&!rl&&r
000
MC/DC0%0/2
$line and $line =~ /^(\d+)/
$line $line =~ /^(\d+)/
Truth table0%0/3
A and B
A= $lineB= $line =~ /^(\d+)/
ABresult
0X0
100
111
599 0 $status->newval('job', 'size', $1);
600 0 $status->newval('job', 'size_time', scalar(time));
601 }
602 0 close($handle);
603 0 $self->_write_status($status);
604 0 $self->unlock;
605 0 return 1;
606 }
607  
608 =head1 STATUS FILE STRUCTURE
609  
610 =head2 job SECTION
611  
612 =over 4
613  
614 =item successed 1 if last run was ok
615  
616 =item size The size of the tree
617  
618 =item size_time Last time the size were checked
619  
620 =item first_sync
621  
622 The first time job is take into account
623  
624 =back
625  
626 =head2 success SECTION
627  
628 =item start
629  
630 =item end
631  
632 =head2 failure SECTION
633  
634 =cut
635  
636 4 1;
« MMM/Report/Html.pm ↑ Summary MMM/Daemon.pm »