bin/ubic-periodic

filter: Statementstmt 28.0% 14 / 50 Branchbran 3.1% 1 / 32 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 50.0% 4 / 8 Podpod n/a totaltotal 21.1% 19 / 90 SCAR 37.6
CC
8
Cov
18%
rotate
30.0
xunlink
6.0
xrename
6.0
CRAP 42.9
« lib/Ubic/PortMap.pm ↑ Summary blib/lib/Ubic/Daemon/OS/POSIX.pm »
1 #!/usr/bin/env perl
2 package ubic_periodic;
3 1 $ubic_periodic::VERSION = '1.59';
4 # ABSTRACT: run given command every N seconds
5  
6 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9  
10 1 ▶use Getopt::Long 2.33;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Pod::Usage;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 1 ▶return 1 if caller;
Branch: 1 branch
BranchTrueFalseTotal
if caller 1 0 1
14  
15 0 my $period = 60;
16 0 my $stdout;
17 my $stderr;
18 0 my $rotate_logs;
19  
20 0 ▶GetOptions(
Branch: 1 branch
BranchTrueFalseTotal
&GetOptions("period=i", \$period, "stdout=s", \$stdout, "stderr=s", \$stderr, "rotate-logs!", \$rotate_logs) or pod2usage(2) 0 0 0
21 'period=i' => \$period,
22 'stdout=s' => \$stdout,
23 'stderr=s' => \$stderr,
24 'rotate-logs!' => \$rotate_logs,
25 ) or pod2usage(2);
26 0 ▶pod2usage(2) unless @ARGV == 1;
Branch: 1 branch
BranchTrueFalseTotal
unless @ARGV == 1 0 0 0
27  
28 0 my $command = shift @ARGV;
29  
30 sub xunlink {
31 0 ▶ my $file = shift;
Subroutine xunlink: not called CC=2 SCAR=17.9
32 0 ▶ unlink $file or die "Can't unlink '$file': $!";
Branch: 1 branch
BranchTrueFalseTotal
unlink $file or die "Can't unlink '${file}': $!" 0 0 0
33 }
34  
35 sub xrename {
36 0 ▶ my ($from, $to) = @_;
Subroutine xrename: not called CC=2 SCAR=17.9
37 0 ▶ rename $from => $to or die "Can't rename '$from' to '$to': $!";
Branch: 1 branch
BranchTrueFalseTotal
rename $from, $to or die "Can't rename '${from}' to '${to}': $!" 0 0 0
38 }
39  
40 sub touch {
41 0 ▶ my $file = shift;
Subroutine touch: not called CC=2 SCAR=17.9
42 0 ▶ open my $fh, '>', $file or die "Can't touch '$file': $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $file or die "Can't touch '${file}': $!" 0 0 0
43 }
44  
45 sub rotate {
46 0 ▶ my $file = shift;
Subroutine rotate: not called CC=5 SCAR=34.0
47 0 my $time = time;
48  
49 0 ▶ return unless -e $file;
Branch: 1 branch
BranchTrueFalseTotal
unless -e $file 0 0 0
50  
51 0 ▶ unless (-e "$file.1") {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e "$file.1") 0 0 0
52 0 touch("$file.1");
53 }
54 0 my $mtime = (stat "$file.1")[9];
55 0 ▶ if ($time - $mtime >= 86400) {
Branch: 1 branch
BranchTrueFalseTotal
if ($time - $mtime >= 86400) 0 0 0
56 # it's time to rotate!
57 0 ▶ if (-e "$file.2") {
Branch: 1 branch
BranchTrueFalseTotal
if (-e "$file.2") 0 0 0
58 0 xunlink("$file.2");
59 }
60 0 xrename("$file.1" => "$file.2");
61 0 xrename($file => "$file.1");
62 }
63 }
64  
65 0 while (1) {
66 0 my $start_time = time;
67  
68 0 ▶ if ($rotate_logs) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rotate_logs) 0 0 0
69 0 rotate($stdout);
70 0 rotate($stderr);
71 }
72  
73 # we reopen logs on every loop, so we don't have to restart periodic service on logrotate
74 0 ▶ if ($stdout) {
Branch: 1 branch
BranchTrueFalseTotal
if ($stdout) 0 0 0
75 0 ▶ open STDOUT, '>>', $stdout or die "Can't open stdout: $!";
Branch: 1 branch
BranchTrueFalseTotal
open STDOUT, ">>", $stdout or die "Can't open stdout: $!" 0 0 0
76 }
77 0 ▶ if ($stderr) {
Branch: 1 branch
BranchTrueFalseTotal
if ($stderr) 0 0 0
78 0 ▶ open STDERR, '>>', $stderr or die "Can't open stderr: $!";
Branch: 1 branch
BranchTrueFalseTotal
open STDERR, ">>", $stderr or die "Can't open stderr: $!" 0 0 0
79 }
80 0 system($command); # no, we don't check for failures
81  
82 0 my $time = time;
83 0 ▶ if ($time - $start_time < $period) {
Branch: 1 branch
BranchTrueFalseTotal
if ($time - $start_time < $period) 0 0 0
84 0 sleep $start_time + $period - $time;
85 }
86 }
87  
88 __END__
« lib/Ubic/PortMap.pm ↑ Summary blib/lib/Ubic/Daemon/OS/POSIX.pm »