blib/lib/Catmandu/Fix/Bind/timeout.pm

filter: Statementstmt 90.0% 45 / 50 Branchbran 56.2% 9 / 16 Conditioncond 50.0% 1 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 90.0% 9 / 10 Podpod 0.0% 0 / 4 totaltotal 77.1% 64 / 83 CC 12 SCAR 18.4
CC
12
Cov
81%
unit
15.0
result
3.0
inline_replace
3.0
CRAP 6.3
« blib/lib/Catmandu/Validator.pm ↑ Summary blib/lib/Catmandu/Cmd/convert.pm »
1 package Catmandu::Fix::Bind::timeout;
2  
3 1 ▶use Catmandu::Sane;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 1 our $VERSION = '1.0201_02';
6  
7 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Clone ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Time::HiRes;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 1 with 'Catmandu::Fix::Bind';
13  
14 1 has time => (is => 'ro');
15 0 ▶has units => (is => 'ro', default => sub {'SECONDS'});
Subroutine __ANON__: not called CC=1 SCAR=1.2
16 1 has sleep => (is => 'rw');
17  
18 sub unit {
19 13 ▶ my ($self, $data) = @_;
Subroutine unit: called CC=7 SCAR=27.0
Pod undocumented
20  
21 13 my $sleep = $self->time;
22 13 ▶ my $units = $self->units // 'SECONDS';
Condition50%1/2
$self->units // "SECONDS"
l!l
130
MC/DC0%0/1
$self->units // "SECONDS"
$self->units
Truth table50%1/2
A
A= $self->units
Aresult
11
00
23  
24 13 ▶ if ($units =~ /^MICROSECOND(S)?$/i) {
Branch: 5 branches
BranchTrueFalseTotal
if ($units =~ /^MICROSECOND(S)?$/i) { } 0 13 13
elsif ($units =~ /^MILLISECOND(S)$/i) { } 0 13 13
elsif ($units =~ /^SECOND(S)?$/i) { } 13 0 13
elsif ($units =~ /^MINUTE(S)?$/i) { } 0 0 0
elsif ($units =~ /^HOUR(S)?$/i) { } 0 0 0
25 0 $sleep /= 1000000;
26 }
27 elsif ($units =~ /^MILLISECOND(S)$/i) {
28 0 $sleep /= 1000;
29 }
30 elsif ($units =~ /^SECOND(S)?$/i) {
31  
32 # ok
33 }
34 elsif ($units =~ /^MINUTE(S)?$/i) {
35 0 $sleep *= 60;
36 }
37 elsif ($units =~ /^HOUR(S)?$/i) {
38 0 $sleep *= 3600;
39 }
40 else {
41 # ok - use seconds
42 }
43  
44 13 $self->sleep($sleep);
45  
46 13 [$data, Clone::clone($data)];
47 }
48  
49 sub bind {
50 15 ▶ my ($self, $mvar, $func) = @_;
Subroutine bind: called CC=2 SCAR=6.9
Pod undocumented
51  
52 15 my $sleep = $self->sleep();
53  
54 15 ▶ if ($sleep >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sleep >= 0) 13 2 15
55 13 my $start = [Time::HiRes::gettimeofday];
56  
57 13 $mvar->[0] = $func->($mvar->[0]);
58  
59 12 $sleep -= Time::HiRes::tv_interval($start);
60  
61 12 $self->sleep($sleep);
62 }
63  
64 14 $mvar;
65 }
66  
67 sub result {
68 12 ▶ my ($self, $mvar) = @_;
Subroutine result: called CC=3 SCAR=11.0
Pod undocumented
69  
70 12 ▶ if ($self->sleep < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->sleep < 0) 3 9 12
71 3 $self->log->warn("timeout after > "
72 . $self->time . " "
73 . $self->units . " : "
74 . (-1 * $self->sleep)
75 . " extra time");
76 3 inline_replace($mvar->[0], $mvar->[1]);
77 }
78  
79 12 ▶ $self->sleep < 0 ? $mvar->[1] : $mvar->[0];
Branch: 1 branch
BranchTrueFalseTotal
$self->sleep < 0 ? : 3 9 12
80 }
81  
82 sub inline_replace {
83 3 ▶ my ($old, $new) = @_;
Subroutine inline_replace: called CC=3 SCAR=11.0
Pod undocumented
84  
85 3 for my $key (keys %$old) {
86 4 delete $old->{$key};
87 }
88  
89 3 for my $key (keys %$new) {
90 3 $old->{$key} = $new->{$key};
91 }
92 }
93  
94 1 1;
95  
96 __END__
97  
98 =pod
99  
100 =head1 NAME
101  
102 Catmandu::Fix::Bind::timeout - run fixes that should run within a time limit
103  
104 =head1 SYNOPSIS
105  
106 # The following bind will run fix1(), fix2(), ... fixN() only if the
107 # action can be done in 5 seconds
108 do timeout(time => 5, units => seconds)
109 fix1()
110 fix2()
111 fix3()
112 .
113 .
114 .
115 fixN()
116 end
117  
118 next_fix()
119  
120 =head1 DESCRIPTION
121  
122 The timeout binder will run the supplied block only when all the fixes can be
123 run within a time limit. All fixes (except side-effects) are ignored when the
124 block can't be executed within the time limit.
125  
126 =head1 CONFIGURATION
127  
128 =head2 timeout(time => VALUE , units => MICROSECOND|MILLISECONDS|SECONDS|MINUTES|HOURS)
129  
130 Set a timeout to VALUE. This timeout doesn't prevent a fix script to run longer than the
131 specified value, but it does prevent fixes to have any effect when the timeout has been reached.
132  
133 # This script will run 10 seconds
134 do timeout(time => 5, seconds)
135 reject() # This will be ignored
136 sleep(10,seconds)
137 add_field(foo,bar) # This will be ignored
138 end
139
140 At timeout a log message of level WARN will be generated.
141  
142 =head1 SEE ALSO
143  
144 L<Catmandu::Fix::Bind>
145  
146 =cut
« blib/lib/Catmandu/Validator.pm ↑ Summary blib/lib/Catmandu/Cmd/convert.pm »