File Coverage

blib/lib/App/Monitor/Simple.pm
Criterion Covered Total %
statement 34 36 94.4
branch 6 8 75.0
condition 11 12 91.6
subroutine 7 7 100.0
pod 1 2 50.0
total 59 65 90.7


line stmt bran cond sub pod time code
1             package App::Monitor::Simple;
2              
3 2     2   75481 use strict;
  2         5  
  2         77  
4 2     2   10 use warnings;
  2         4  
  2         92  
5             our $VERSION = '0.01';
6              
7 2     2   11 use base 'Exporter';
  2         16  
  2         278  
8             our @EXPORT_OK = qw/run/;
9              
10 2     2   2238 use IO::CaptureOutput qw/capture/;
  2         74840  
  2         721  
11              
12             sub run {
13 3     3 1 8499 my ($args) = @_;
14              
15             #
16 3         13 my $command = delete $args->{command};
17 3   100     31 my $retry = delete $args->{retry} || 0;
18 3   100     28 my $interval = delete $args->{interval} || 5;
19 3   50     18 my $quiet = delete $args->{quiet} || 0;
20              
21 3         7 my $ret = -1;
22 3         8 my $count = 0;
23 3         5 while(1) {
24 5         16 $count++;
25 5         112 $ret = rsystem($command, $quiet);
26 5 100 100     116 if ($ret == 0 || $retry < $count) {
27 3         21 last;
28             }
29 2         10000421 sleep $interval;
30             }
31              
32 3 100 100     71 if($count == 1 && $ret == 0) {
    50          
33             # normal
34 1         23 return 0;
35             } elsif($count < $retry) {
36             # warnings
37             # Now thinking...
38 0         0 return 0;
39             } else {
40             # failure
41 2         43 return -1;
42             }
43             }
44              
45             sub rsystem {
46 5     5 0 18 my ($command, $quiet) = @_;
47 5 50       24 if($quiet) {
48 5         11 my ($status, $stdout, $stderr);
49 5     5   75 capture sub { $status = system($command) } => \$stdout, \$stderr;
  5         481121  
50 5         5465 return $status;
51             } else {
52 0           return system($command);
53             }
54             }
55              
56             1;
57             __END__