blib/lib/Process/MaxSize.pm

filter: Statementstmt 55.1% 27 / 49 Branchbran 35.7% 5 / 14 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 62.5% 5 / 8 Podpod 0.0% 0 / 3 totaltotal 50.0% 37 / 74 CC 8 SCAR 16.7
CC
8
Cov
51%
check
12.0
new
6.0
__ANON__
6.0
CRAP 5.3
↑ Summary
1 ###########################################
2 package Process::MaxSize;
3 ###########################################
4  
5 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Log::Log4perl qw(:easy);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Cwd;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 our $VERSION = "0.04";
11  
12 ###########################################
13 0 ▶sub new {
Subroutine new: not called CC=2 SCAR=17.9
Pod undocumented
14 ###########################################
15 0 my($class, %options) = @_;
16  
17 0 my @argv = @ARGV;
18  
19 my $self = {
20 max_size => "10m",
21 0 ▶ restart => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
22 0 INFO "Restarting $0 @ARGV";
23 0 ▶ exec $0, @argv or
Branch: 1 branch
BranchTrueFalseTotal
exec $0, @argv or LOGDIE("exec failed: $!") 0 0 0
24 LOGDIE "exec failed: $!";
25 },
26 0 sleep => 2,
27 cwd => cwd(),
28 %options,
29 };
30  
31 0 ▶ if($self->{max_size} =~ /m$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"max_size"} =~ /m$/) 0 0 0
32 0 $self->{max_size} =~ s/\D//g;
33 0 $self->{max_size} *= 1024;
34 }
35  
36 0 bless $self, $class;
37 }
38  
39 ###########################################
40 0 ▶sub check {
Subroutine check: not called CC=3 SCAR=24.8
Pod undocumented
41 ###########################################
42 0 my($self) = @_;
43  
44 0 my $process_size = $self->process_size();
45  
46 0 DEBUG "Checking size: ${process_size}kB (max $self->{max_size}kB)";
47  
48 0 ▶ if($process_size > $self->{max_size}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($process_size > $self->{"max_size"}) 0 0 0
49 0 WARN "Max size reached ",
50 "(${process_size}kB/$self->{max_size}kB, restarting";
51  
52 # Sleep the specified number of seconds
53 0 ▶ if($self->{sleep}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"sleep"}) 0 0 0
54 0 INFO "Sleeping $self->{sleep} seconds.";
55 0 sleep($self->{sleep});
56 }
57  
58 # Try to go back to the start directory
59 0 INFO "Changing directory to $self->{cwd}";
60 0 chdir $self->{cwd};
61  
62 0 $self->{restart}->();
63 # Doesn't return
64 }
65  
66 0 return $process_size;
67 }
68  
69 ###########################################
70 1 ▶sub process_size {
Subroutine process_size: called CC=4 SCAR=13.9
Pod undocumented
71 ###########################################
72 1 my($self) = @_;
73  
74 # Doesn't work on freebsd
75 #use Proc::ProcessTable;
76 #my $t = Proc::ProcessTable->new();
77 #my($p) = grep { $_->pid == $$ } @{$t->table};
78 #return $p->size();
79
80 1 my $size;
81  
82 1 my $ps = "ps";
83 1 my $solaris_ps = "/usr/ucb/ps";
84  
85 1 ▶ if( -f $solaris_ps ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $solaris_ps) 0 1 1
86 0 $ps = $solaris_ps;
87 }
88  
89 1 open PIPE, "$ps wwaxo 'pid,rss' |";
90 1 ▶ while(<PIPE>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline PIPE)) 12 1 13
91 12 ▶ next unless /^\s*$$\s/;
Branch: 1 branch
BranchTrueFalseTotal
unless /^\s*$$\s/ 11 1 12
92 1 s/^\s+//g;
93 1 chomp;
94 1 $size = (split(' ', $_))[1];
95 }
96 1 close PIPE;
97  
98 1 return $size;
99 }
100  
101 1 1;
102  
103 __END__
↑ Summary