Alarm.pm

filter: Statementstmt 87.2% 143 / 164 Branchbran 55.3% 31 / 56 Conditioncond 30.2% 13 / 43 MC/DCmcdc 0.0% 0 / 27 Subroutinesub 83.3% 20 / 24 Podpod 100% 5 / 5 totaltotal 66.4% 212 / 319 CC 43 SCAR 30.3
CC
43
Cov
71%
prepare
39.2
timeout
30.4
forward
5.2
CRAP 20.6
↑ Summary
1 package Catalyst::Plugin::Alarm;
2 4 ▶use base qw/Class::Accessor::Fast/;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 4 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use Time::HiRes;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 4 ▶use Catalyst::Exception ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 4 ▶use MRO::Compat;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 4 ▶use mro 'c3';
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 # Sys::SigAction doesn't help on Win32 systems
16 # because Win32 doesn't use POSIX signals
17 4 our $USE_NATIVE_SIGNALS = 0;
18 4 ▶if ( $^O eq 'MSWin32' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($^O eq "MSWin32") 0 4 4
19 0 $USE_NATIVE_SIGNALS = 1;
20 }
21  
22 4 our $VERSION = 0.05;
23 4 our $TIMEOUT = 180;
24 4 our $LOCAL_TIMEOUT = 30;
25  
26 # add a \. if we ever support HiRes alarm()
27 4 my $ALARM_RE = qr/^[\d]+$/;
28  
29 4 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
30 4 __PACKAGE__->mk_accessors(qw/ alarm /);
31 }
32  
33 2 ▶sub setup_finalize {
Subroutine setup_finalize: called CC=2 SCAR=6.9
Pod documented
34 2 my $c = shift;
35 2 my $ret = $c->next::method(@_);
36  
37 2 my %conf = %{ $c->config->{alarm} };
38  
39 2 ▶ if ( $conf{use_native_signals} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($conf{'use_native_signals'}) { } 1 1 2
40 1 $USE_NATIVE_SIGNALS = 1;
41 }
42 else {
43 1 require Sys::SigAction; # defer till runtime
44 }
45  
46 2 return $ret;
47 }
48  
49 # must call on every request
50 6 ▶sub prepare {
Subroutine prepare: called CC=17 SCAR=36.7
Pod documented
51 6 my $class = shift;
52 6 my $c = $class->next::method(@_);
53  
54 6 ▶ return $c unless exists $c->config->{alarm};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $c->config->{"alarm"} 0 6 6
55  
56 6 my %alarm;
57  
58 # copy of config for easy checking and temp overriding
59 6 my %conf = %{ $c->config->{alarm} };
60  
61 # should we override forward method?
62 6 ▶ $alarm{forward} = $conf{forward} || 0;
Condition50%1/2
$conf{'forward'} || 0
l!l
06
MC/DC0%0/1
$conf{'forward'} || 0
$conf{'forward'}
Truth table50%1/2
A
A= $conf{'forward'}
Aresult
11
00
63  
64 # check if we should override forward() based on regex
65 6 ▶ if ( exists $conf{timeout}
Branch: 1 branch
BranchTrueFalseTotal
if (exists $conf{"timeout"} and exists $conf{"override"} and $conf{"timeout"}) 0 6 6
Condition33%2/6
exists $conf{"timeout"} and exists $conf{"override"} and $conf{"timeout"}
!ll&&!rl&&r
600
exists $conf{"timeout"} and exists $conf{"override"}
!ll&&!rl&&r
060
MC/DC0%0/3
exists $conf{"timeout"} and exists $conf{"override"} and $conf{"timeout"}
exists $conf{"timeout"} exists $conf{"override"} $conf{"timeout"}
Truth table25%1/4
A and B and C
A= exists $conf{"timeout"}B= exists $conf{"override"}C= $conf{"timeout"}
ABCresult
0XX0
10X0
1100
1111
66 and exists $conf{override}
67 and $conf{timeout} )
68 {
69 0 ▶ my $re = $conf{override}->{re} || '';
Condition0%0/2
$conf{'override'}{'re'} || ''
l!l
00
MC/DC0%0/1
$conf{'override'}{'re'} || ''
$conf{'override'}{'re'}
Truth table0%0/2
A
A= $conf{'override'}{'re'}
Aresult
11
00
70 0 ▶ if ( $re && $c->req->path =~ m/$re/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($re and $c->req->path =~ /$re/) 0 0 0
Condition0%0/3
$re and $c->req->path =~ /$re/
!ll&&!rl&&r
000
MC/DC0%0/2
$re and $c->req->path =~ /$re/
$re $c->req->path =~ /$re/
Truth table0%0/3
A and B
A= $reB= $c->req->path =~ /$re/
ABresult
0X0
100
111
71 0 $alarm{override} = $c->req->path;
72 0 ▶ if ( $c->debug ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->debug) 0 0 0
73 0 $c->log->debug(
74 "found alarm override for: " . $c->req->path );
75 $c->log->debug( "setting this request global alarm to "
76 0 . $conf{override}->{timeout} );
77 }
78 0 $conf{global} = $conf{override}->{timeout};
79 }
80 }
81  
82 # special case - allow for disable global timer
83 6 ▶ if ( exists $conf{global}
Branch: 1 branch
BranchTrueFalseTotal
if (exists $conf{"global"} and $conf{"global"} != 0) 6 0 6
Condition33%1/3
exists $conf{"global"} and $conf{"global"} != 0
!ll&&!rl&&r
006
MC/DC0%0/2
exists $conf{"global"} and $conf{"global"} != 0
exists $conf{"global"} $conf{"global"} != 0
Truth table33%1/3
A and B
A= exists $conf{"global"}B= $conf{"global"} != 0
ABresult
0X0
100
111
84 && $conf{global} != 0 )
85 {
86 6 my $timeout = $conf{global};
87  
88 my $handler = $conf{handler}
89 0 ▶ || sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
90 0 Catalyst::Exception->throw("Global Alarm timeout: $timeout");
91 6 ▶ };
Condition33%1/3
$conf{'handler'} || sub { 'Catalyst::Exception'->throw("Global Alarm timeout: $timeout"); }
l!l&&r!l&&!r
600
MC/DC0%0/2
$conf{'handler'} || sub { 'Catalyst::Exception'->throw("Global Alarm timeout: $timeout"); }
$conf{'handler'} sub { 'Catalyst::Exception'->throw("Global Alarm timeout: $timeout"); }
Truth table33%1/3
A || B
A= $conf{'handler'}B= sub { 'Catalyst::Exception'->throw("Global Alarm timeout: $timeout"); }
ABresult
1X1
011
000
92  
93 6 ▶ if ( !$timeout or $timeout !~ m/$ALARM_RE/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($timeout and $timeout =~ /$ALARM_RE/) 0 6 6
Condition33%1/3
$timeout and $timeout =~ /$ALARM_RE/
!ll&&!rl&&r
006
MC/DC0%0/2
$timeout and $timeout =~ /$ALARM_RE/
$timeout $timeout =~ /$ALARM_RE/
Truth table33%1/3
A and B
A= $timeoutB= $timeout =~ /$ALARM_RE/
ABresult
0X0
100
111
94  
95 # avoid spurious warning
96 4 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
97  
98 #$timeout = '' unless defined $timeout;
99 0 Catalyst::Exception->throw(
100 "Global Alarm timeout value is invalid: $timeout");
101 }
102  
103 # configure alarm
104 6 $alarm{timeout} = $timeout;
105 6 $alarm{start} = [ Time::HiRes::gettimeofday() ];
106 6 $alarm{handler} = $handler;
107 6 $alarm{failed} = [];
108  
109 4 ▶ my $alarm_handler = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
110 4 $c->alarm->on(1);
111 4 $c->alarm->sounded( Time::HiRes::gettimeofday() );
112 4 $c->error(
113 "Global Alarm sounded at ~$timeout seconds: "
114 . Time::HiRes::tv_interval(
115 $c->alarm->start, $c->alarm->sounded
116 )
117 );
118  
119 4 push( @{ $c->alarm->{failed} }, $c->action->name );
120 4 &$handler( $c, 1 );
121 6 };
122  
123 6 ▶ if ($USE_NATIVE_SIGNALS) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Catalyst::Plugin::Alarm::USE_NATIVE_SIGNALS) { } 3 3 6
124 3 $SIG{ALRM} = $alarm_handler;
125 }
126 else {
127  
128 $alarm{sig_handler}
129 3 = Sys::SigAction::set_sig_handler( 'ALRM', $alarm_handler,
130 { safe => 1 } );
131  
132 }
133  
134 # set alarm -- see NOTE in timeout about HiRes::alarm()
135 #Time::HiRes::alarm($timeout);
136 6 CORE::alarm($timeout);
137  
138 6 ▶ $c->log->debug("global alarm set for $timeout seconds")
Branch: 1 branch
BranchTrueFalseTotal
if $c->debug 0 6 6
139 if $c->debug;
140  
141 }
142  
143 # set accessor
144 6 $c->alarm( bless \%alarm, 'Catalyst::Alarm' );
145  
146 6 return $c;
147 }
148  
149 6 ▶sub finalize {
Subroutine finalize: called CC=3 SCAR=12.0
Pod documented
150 6 my $c = shift;
151  
152 6 ▶ if ( !$c->alarm || !$c->alarm->{start} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($c->alarm and $c->alarm->{"start"}) 0 6 6
Condition33%1/3
$c->alarm and $c->alarm->{"start"}
!ll&&!rl&&r
006
MC/DC0%0/2
$c->alarm and $c->alarm->{"start"}
$c->alarm $c->alarm->{"start"}
Truth table33%1/3
A and B
A= $c->alarmB= $c->alarm->{"start"}
ABresult
0X0
100
111
153 0 $c->next::method(@_);
154 0 return 1;
155 }
156  
157 # this stuff may all be irrelevant since next::method
158 # has already been called, but for completeness' sake.
159  
160 6 $c->alarm->stop( Time::HiRes::gettimeofday() );
161 $c->alarm->total(
162 6 Time::HiRes::tv_interval( $c->alarm->{start}, $c->alarm->{stop} ) );
163  
164 # turn off alarm
165 #Time::HiRes::alarm(0);
166 6 CORE::alarm(0);
167  
168 # debugging
169 #$c->log->dumper($c->alarm);
170  
171 # SigAction reference count gets screwy sometimes (at least in tests)
172 # so just delete this explicitly since we no longer need it anyway
173 6 delete $c->alarm->{sig_handler};
174  
175 6 $c->next::method(@_);
176  
177 6 1;
178 }
179  
180 32 ▶sub forward {
Subroutine forward: called CC=4 SCAR=16.4
Pod documented
181 32 my $c = shift;
182  
183 # in Catalyst 5.8x stack is undef when we need it
184 # so prime it here.
185 32 ▶ $c->{stack} = [] unless $c->stack;
Branch: 1 branch
BranchTrueFalseTotal
unless $c->stack 0 32 32
186  
187 32 ▶ if ( $c->alarm && $c->alarm->{forward} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->alarm and $c->alarm->{"forward"}) 0 32 32
Condition33%1/3
$c->alarm and $c->alarm->{"forward"}
!ll&&!rl&&r
0320
MC/DC0%0/2
$c->alarm and $c->alarm->{"forward"}
$c->alarm $c->alarm->{"forward"}
Truth table33%1/3
A and B
A= $c->alarmB= $c->alarm->{"forward"}
ABresult
0X0
100
111
188 0 return $c->timeout(@_);
189 }
190  
191 32 return $c->dispatcher->forward( $c, @_ );
192 }
193  
194 4 ▶sub timeout {
Subroutine timeout: called CC=21 SCAR=34.1
Pod documented
195 4 my $c = shift;
196 4 my ( $timeout, @arg );
197  
198 # set a default if not configured
199 4 my $conf = {};
200 4 ▶ if ( !exists $c->config->{alarm} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $c->config->{'alarm'}) { } 0 4 4
201 0 $conf->{timeout} = $LOCAL_TIMEOUT;
202 }
203 else {
204 4 $conf = $c->config->{alarm};
205 }
206  
207 4 ▶ if ( ref $_[0] ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $_[0]) { } 2 2 4
elsif (not @_ % 2) { } 2 0 2
208 2 ▶ $timeout = $_[0]->{timeout} || $conf->{timeout};
Condition33%1/3
$_[0]{'timeout'} || $conf->{'timeout'}
l!l&&r!l&&!r
200
MC/DC0%0/2
$_[0]{'timeout'} || $conf->{'timeout'}
$_[0]{'timeout'} $conf->{'timeout'}
Truth table33%1/3
A || B
A= $_[0]{'timeout'}B= $conf->{'timeout'}
ABresult
1X1
011
000
209 2 ▶ @arg = ref $_[0]->{action} ? @{ $_[0]->{action} } : $_[0]->{action};
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0]{'action'} ? : 2 0 2
210 }
211 elsif ( !( @_ % 2 ) ) {
212 2 my %e = @_;
213 2 ▶ $timeout = $e{timeout} || $conf->{timeout};
Condition33%1/3
$e{'timeout'} || $conf->{'timeout'}
l!l&&r!l&&!r
200
MC/DC0%0/2
$e{'timeout'} || $conf->{'timeout'}
$e{'timeout'} $conf->{'timeout'}
Truth table33%1/3
A || B
A= $e{'timeout'}B= $conf->{'timeout'}
ABresult
1X1
011
000
214 2 ▶ @arg = ref $e{action} ? @{ $e{action} } : $e{action};
Branch: 1 branch
BranchTrueFalseTotal
ref $e{'action'} ? : 2 0 2
215  
216 # just in case we called as 'foo',[@args] and not as => pairs
217 2 ▶ if ( !scalar(@arg) || !defined $arg[0] ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (scalar @arg and defined $arg[0]) 0 2 2
Condition33%1/3
scalar @arg and defined $arg[0]
!ll&&!rl&&r
002
MC/DC0%0/2
scalar @arg and defined $arg[0]
scalar @arg defined $arg[0]
Truth table33%1/3
A and B
A= scalar @argB= defined $arg[0]
ABresult
0X0
100
111
218 0 @arg = @_;
219 }
220  
221 }
222 else {
223 0 @arg = @_;
224 0 $timeout = $conf->{timeout};
225 }
226  
227 4 ▶ if ( !defined $timeout or $timeout !~ m/$ALARM_RE/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $timeout and $timeout =~ /$ALARM_RE/) 0 4 4
Condition33%1/3
defined $timeout and $timeout =~ /$ALARM_RE/
!ll&&!rl&&r
004
MC/DC0%0/2
defined $timeout and $timeout =~ /$ALARM_RE/
defined $timeout $timeout =~ /$ALARM_RE/
Truth table33%1/3
A and B
A= defined $timeoutB= $timeout =~ /$ALARM_RE/
ABresult
0X0
100
111
228  
229 # avoid spurious warning
230 4 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
231 0 Catalyst::Exception->throw(
232 "Alarm timeout value is invalid: $timeout");
233 }
234  
235 4 my $e = join( ', ', @arg );
236 4 my @ret;
237 4 $c->alarm->on(0);
238  
239 my $handler = $c->config->{alarm}->{handler}
240 0 ▶ || sub { Catalyst::Exception->throw("Local Alarm timeout for: $e") };
Subroutine __ANON__: not called CC=1 SCAR=2.0
Condition33%1/3
$c->config->{'alarm'}{'handler'} || sub { 'Catalyst::Exception'->throw("Local Alarm timeout for: $e"); }
l!l&&r!l&&!r
400
MC/DC0%0/2
$c->config->{'alarm'}{'handler'} || sub { 'Catalyst::Exception'->throw("Local Alarm timeout for: $e"); }
$c->config->{'alarm'}{'handler'} sub { 'Catalyst::Exception'->throw("Local Alarm timeout for: $e"); }
Truth table33%1/3
A || B
A= $c->config->{'alarm'}{'handler'}B= sub { 'Catalyst::Exception'->throw("Local Alarm timeout for: $e"); }
ABresult
1X1
011
000
241  
242 4 my $prev_alarm = 0;
243  
244 4 ▶ my $alarm_handler = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
245 4 $c->alarm->on(1);
246 4 push @{ $c->alarm->{failed} }, $e;
247  
248 4 $c->error(
249 "Local Alarm sounded after $timeout seconds for action: $e");
250  
251 4 &$handler( $c, \@ret );
252  
253 4 };
254  
255 4 eval {
256 4 my $h = $SIG{ALRM};
257 4 ▶ if ($USE_NATIVE_SIGNALS) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Catalyst::Plugin::Alarm::USE_NATIVE_SIGNALS) { } 2 2 4
258 2 $SIG{ALRM} = $alarm_handler;
259 }
260 else {
261 2 $h = Sys::SigAction::set_sig_handler( 'ALRM', $alarm_handler,
262 { safe => 1 } );
263 }
264  
265 #$c->log->debug( Dumper $h );
266 4 my $sv = [ Time::HiRes::gettimeofday() ];
267  
268 #$c->log->debug("setting alarm for $timeout seconds");
269 # Time::HiRes version of alarm() doing wacky things like going off after
270 # only 1.4.. seconds when $timeout is much greater
271 # TODO see if I am just using it wrong.
272 #my $prev_alarm = Time::HiRes::alarm($timeout);
273 4 $prev_alarm = CORE::alarm($timeout);
274 4 ▶ $c->log->debug("previous alarm was $prev_alarm")
Branch: 1 branch
BranchTrueFalseTotal
if $c->debug 0 4 4
275 if $c->debug;
276  
277 4 @ret = $c->dispatcher->forward( $c, @arg );
278  
279 # NOTE that on alarm, if the default handler is used, we never
280 # reach this point. This exists mostly for debugging
281 # with the test scripts.
282  
283 #$c->log->debug("came back");
284 4 my $ev = [ Time::HiRes::gettimeofday() ];
285 4 my $intv = Time::HiRes::tv_interval( $sv, $ev );
286  
287 #warn "intv = $intv\n";
288 4 ▶ $c->log->debug("resetting alarm after $intv seconds")
Branch: 1 branch
BranchTrueFalseTotal
if $c->debug 0 4 4
289 if $c->debug;
290  
291 #Time::HiRes::alarm(0);
292  
293 # if $prev_alarm was set, then we are running under a global alarm
294 # so do our best to restore it, minus the time we just spent
295 # NOTE that because CORE alarm uses only ints that we end
296 # up taking longer for global alarm than originally configed.
297 4 ▶ if ( $prev_alarm > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($prev_alarm > 0) 4 0 4
298 4 $prev_alarm = $prev_alarm - int($intv);
299 4 ▶ $prev_alarm = 1 if $prev_alarm <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $prev_alarm <= 0 0 4 4
300 4 ▶ $c->log->debug("prev_alarm = $prev_alarm")
Branch: 1 branch
BranchTrueFalseTotal
if $c->debug 0 4 4
301 if $c->debug;
302 }
303  
304 4 CORE::alarm($prev_alarm);
305  
306 4 ▶ if ($USE_NATIVE_SIGNALS) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Catalyst::Plugin::Alarm::USE_NATIVE_SIGNALS) 2 2 4
307 2 $SIG{ALRM} = $h;
308 }
309 };
310  
311 # reset no matter what.
312 #Time::HiRes::alarm(0);
313  
314 4 CORE::alarm($prev_alarm);
315  
316 # despite the forward() pod claim to the contrary,
317 # there is a bug in Catalyst::forward() that returns
318 # in scalar context, so only first return value is used.
319 # we must mimic that behaviour until the bug is fixed.
320 # Otherwise, we'll break existing code that relies on
321 # that scalar behaviour.
322  
323 4 ▶ return $c->alarm->on ? undef : $ret[0];
Branch: 1 branch
BranchTrueFalseTotal
$c->alarm->on ? : 0 4 4
324 }
325  
326 4 1;
327  
328 # C::A package is simple accessors and off() method
329 package Catalyst::Alarm;
330 4 ▶use base qw/Class::Accessor::Fast/;
Subroutine BEGIN: called CC=1 SCAR=0.0
331 4 __PACKAGE__->mk_accessors(
332 qw/
333 timeout
334 start
335 stop
336 total
337 sig_handler
338 handler
339 failed
340 sounded
341 forward
342 override
343 on
344 /
345 );
346  
347 0 ▶sub off {
Subroutine off: not called CC=1 SCAR=6.9
348  
349 #Time::HiRes::alarm(0);
350 0 CORE::alarm(0);
351 }
352  
353 # for those who sleep like I do...
354 0 ▶sub snooze { off() }
Subroutine snooze: not called CC=1 SCAR=6.9
355  
356 4 1;
357  
358 __END__
↑ Summary