blib/lib/Sub/Timebound.pm

filter: Statementstmt 100% 50 / 50 Branchbran 100% 6 / 6 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 6 / 6 Podpod 0.0% 0 / 1 totaltotal 98.4% 62 / 63 CC 4 SCAR 8.5
CC
4
Cov
100%
timeboundretry
4.0
BEGIN
1.0
BEGIN
1.0
CRAP 2.3
↑ Summary
1 package Sub::Timebound;
2  
3 1 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 require Exporter;
8 1 ▶use AutoLoader qw(AUTOLOAD);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 our @ISA = qw(Exporter);
11  
12 # Items to export into callers namespace by default. Note: do not export
13 # names by default without a very good reason. Use EXPORT_OK instead.
14 # Do not simply export all your public functions/methods/constants.
15  
16 # This allows declaration use Sub::Timebound ':all';
17 # If you do not need this, moving things directly into @EXPORT or @EXPORT_OK
18 # will save memory.
19 1 our %EXPORT_TAGS = ( 'all' => [ qw(
20
21 ) ] );
22  
23 1 our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
24  
25 1 our @EXPORT = qw(timeboundretry);
26  
27 1 our $VERSION = '1.01';
28  
29 # Preloaded methods go here.
30  
31 sub timeboundretry
32 3 ▶{
Subroutine timeboundretry: called CC=4 SCAR=13.9
Pod undocumented
33 # Allocated Time, Attempts, Wait Time between attempts, Coderef to execute, Params
34 3 my $allocated = int(shift);
35 3 my $attempts = int(shift);
36 3 my $wait = int(shift);
37 3 my $coderef = shift;
38 3 my @params = @_;
39  
40 3 my $ret = {
41 value => undef,
42 status => 1, ### Assume Success
43 };
44  
45 3 my $count = 1;
46  
47 3 my $ref_string = "Did not complete in allocated $allocated Seconds\n";
48  
49 AGAIN: {
50  
51 7 eval {
52 7 ▶ local $SIG{'ALRM'} = sub { die $ref_string };
Subroutine __ANON__: called CC=1 SCAR=0.0
53 7 alarm($allocated);
54 7 $ret->{value} = $coderef->(@params);
55 1 $ret->{status} = 1; ### We execute this means all is well
56 1 alarm(0); ### Reset alarm signal upon success
57 };
58  
59 7 alarm(0); ### Reset alarm signal
60  
61 7 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 6 1 7
62 6 $ret->{status} = 0; ### Inform the caller that function call did not succeed
63 ### Now all we know is that eval block failed.
64 ### We still should determine if it died due to timeout or the called function misbehaved
65 6 print "ERROR $@\n";
66  
67 6 ▶ if ("$@" eq $ref_string) {
Branch: 1 branch
BranchTrueFalseTotal
if ("$@" eq $ref_string) { } 3 3 6
68 3 print "... Error happened due to timeout\n";
69 } else {
70 3 print "... Error happened due to function misbehavior\n";
71 }
72  
73 6 $count++;
74 6 ▶ if ($count > $attempts) {
Branch: 1 branch
BranchTrueFalseTotal
if ($count > $attempts) { } 2 4 6
75 2 print "Exceeded count limit ($attempts)....exiting\n";
76 2 return $ret;
77 } else {
78 4 print "Retry after $wait seconds...\n";
79 4 sleep $wait;
80 4 goto AGAIN;
81 }
82 }
83  
84 }
85 1 return $ret;
86 }
87  
88 # Autoload methods go after =cut, and are processed by the autosplit program.
89  
90 1 1;
91 __END__
↑ Summary