Roles/AutoReconnect.pm

filter: Statementstmt 76.4% 65 / 85 Branchbran 41.3% 24 / 58 Conditioncond 30.3% 10 / 33 MC/DCmcdc 15.0% 3 / 20 Subroutinesub 100% 8 / 8 Podpod 0.0% 0 / 4 totaltotal 52.8% 110 / 208 CC 41 SCAR 36.8
CC
41
Cov
56%
dbi_method
62.2
db_connect
41.8
STORE
15.9
CRAP 39.5
↑ Summary Roles/RaiseError.pm »
1 # $Id: AutoReconnect.pm,v 1.4 2005/12/01 18:09:52 dk Exp $
2  
3 package DBIx::Roles::AutoReconnect;
4  
5 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use DBIx::Roles;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use vars qw(%defaults $VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 $VERSION = '1.00';
10  
11 1 %defaults = (
12 ReconnectTimeout => 60,
13 ReconnectMaxTries => 5,
14 ReconnectFailure => undef,
15 );
16  
17 sub initialize
18 3 ▶{
Subroutine initialize: called CC=1 SCAR=0.0
Pod undocumented
19 3 my $self = $_[0];
20 3 return [], \%defaults;
21 }
22  
23 sub connect
24 5 ▶{
Subroutine connect: called CC=2 SCAR=7.0
Pod undocumented
25 5 my ( $self, $conninfo, $dsn, $user, $pass, $attr) = @_;
26  
27 5 @$conninfo = ( $dsn, $user, $pass, $attr );
28
29 5 my ( $super, $private) = $self-> get_super;
30 5 ▶ return unless $super;
Branch: 1 branch
BranchTrueFalseTotal
unless $super 0 5 5
31  
32 5 my $dbh = db_connect( $self, $conninfo, $super, $private);
33  
34 5 return $dbh;
35 }
36  
37 sub db_connect
38 5 ▶{
Subroutine db_connect: called CC=15 SCAR=37.3
Pod undocumented
39 5 my ( $self, $conninfo, $ref, @param) = @_;
40  
41 5 my $attr = $self-> {attr};
42 5 my $tries = 0;
43 5 my $downtime = 0;
44 5 my $ret;
45 5 RETRY: while ( 1) {
46 {
47 9 my $context = $self-> context;
48 9 eval {
49 9 local $conninfo->[3]->{RaiseError} = 1;
50 9 $ret = $ref->( $self, @param, @$conninfo);
51 };
52 9 ▶ if ( $@) {
Branch: 2 branches
BranchTrueFalseTotal
if ($@) { } 4 5 9
elsif ($ret) { } 5 0 5
53 # restore context if calls are restarted
54 4 $self-> context( $context);
55 } elsif ( $ret) {
56 warn "DBIx::Roles::AutoReconnect: successfully reconnected after $tries tries and $downtime sec downtime\n"
57 if $tries > 0 and (
58 $conninfo->[3]-> {PrintError} or
59 not (exists $conninfo->[3]->{PrintError}) # DBI defaults
60 5 ▶ );
Branch: 1 branch
BranchTrueFalseTotal
if $tries > 0 and $conninfo->[3]{"PrintError"} || !exists $conninfo->[3]{"PrintError"} 0 5 5
Condition50%3/6
$tries > 0 and $conninfo->[3]{"PrintError"} || !exists $conninfo->[3]{"PrintError"}
!ll&&!rl&&r
320
$conninfo->[3]{"PrintError"} or not exists $conninfo->[3]{"PrintError"}
l!l&&r!l&&!r
002
MC/DC0%0/3
$tries > 0 and $conninfo->[3]{"PrintError"} || !exists $conninfo->[3]{"PrintError"}
$tries > 0 $conninfo->[3]{"PrintError"} not exists $conninfo->[3]{"PrintError"}
Truth table50%2/4
A and B or not(C)
A= $tries > 0B= $conninfo->[3]{"PrintError"}C= not exists $conninfo->[3]{"PrintError"}
ABCresult
0XX0
1000
11X1
1011
61 5 last RETRY;
62 } else {
63 0 $@ = "DBIx::Roles::AutoReconnect: Cannot connect(): no suitable roles found";
64 }
65 }
66 $attr-> {ReconnectFailure}->()
67 4 ▶ if $attr-> {ReconnectFailure};
Branch: 1 branch
BranchTrueFalseTotal
if $attr->{"ReconnectFailure"} 0 4 4
68 4 $tries++;
69 4 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (defined $attr->{"ReconnectMaxTries"} and $attr->{"ReconnectMaxTries"} <= $tries) 0 4 4
Condition33%1/3
defined $attr->{"ReconnectMaxTries"} and $attr->{"ReconnectMaxTries"} <= $tries
!ll&&!rl&&r
040
MC/DC0%0/2
defined $attr->{"ReconnectMaxTries"} and $attr->{"ReconnectMaxTries"} <= $tries
defined $attr->{"ReconnectMaxTries"} $attr->{"ReconnectMaxTries"} <= $tries
Truth table33%1/3
A and B
A= defined $attr->{"ReconnectMaxTries"}B= $attr->{"ReconnectMaxTries"} <= $tries
ABresult
0X0
100
111
70 defined ($attr-> {ReconnectMaxTries}) and
71 $attr-> {ReconnectMaxTries} <= $tries
72 ) {
73 0 ▶ $@ = "DBIx::Roles::AutoReconnect: Tried to connect $attr->{ReconnectMaxTries} time(s), giving up\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $@ 0 0 0
74 unless $@;
75 0 ▶ if ( $conninfo-> [3]-> {RaiseError}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($conninfo->[3]{'RaiseError'}) { } 0 0 0
76 0 die $@;
77 } else {
78 warn $@ if
79 not (exists $conninfo->[3]->{PrintError}) # DBI defaults
80 0 ▶ or $conninfo->[3]->{PrintError};
Branch: 1 branch
BranchTrueFalseTotal
if not exists $conninfo->[3]{"PrintError"} or $conninfo->[3]{"PrintError"} 0 0 0
Condition0%0/3
not exists $conninfo->[3]{"PrintError"} or $conninfo->[3]{"PrintError"}
l!l&&r!l&&!r
000
MC/DC0%0/2
not exists $conninfo->[3]{"PrintError"} or $conninfo->[3]{"PrintError"}
not exists $conninfo->[3]{"PrintError"} $conninfo->[3]{"PrintError"}
Truth table0%0/3
not(A) or B
A= not exists $conninfo->[3]{"PrintError"}B= $conninfo->[3]{"PrintError"}
ABresult
1X1
011
000
81 0 return undef;
82 }
83 }
84 4 ▶ if ( $attr-> {ReconnectTimeout} > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attr->{"ReconnectTimeout"} > 0) 0 4 4
85 warn "DBIx::AutoReconnect: sleeping for $attr->{ReconnectTimeout} seconds\n"
86 0 ▶ if $conninfo-> [3]->{PrintError};
Branch: 1 branch
BranchTrueFalseTotal
if $conninfo->[3]{"PrintError"} 0 0 0
87 0 sleep $attr-> {ReconnectTimeout};
88 0 $downtime += $attr-> {ReconnectTimeout};
89 }
90 }
91  
92 5 return $ret;
93 }
94  
95 sub dbi_method
96 11 ▶{
Subroutine dbi_method: called CC=18 SCAR=41.3
Pod undocumented
97 11 my ( $self, $conninfo, $method, @parameters) = @_;
98  
99 return $self-> super( $method, @parameters)
100 11 ▶ if $method eq 'connect' or not $self->dbh->{AutoCommit};
Branch: 1 branch
BranchTrueFalseTotal
if $method eq "connect" or not $self->dbh->{"AutoCommit"} 9 2 11
Condition66%2/3
$method eq "connect" or not $self->dbh->{"AutoCommit"}
l!l&&r!l&&!r
092
MC/DC50%1/2
$method eq "connect" or not $self->dbh->{"AutoCommit"}
$method eq "connect" not $self->dbh->{"AutoCommit"}
Truth table66%2/3
A or not(B)
A= $method eq "connect"B= not $self->dbh->{"AutoCommit"}
ABresult
1X1
011
000
101  
102 2 my ( $wantarray, @ret) = ( wantarray);
103 2 my ( $super, $private) = $self-> get_super;
104 2 ▶ return unless $super;
Branch: 1 branch
BranchTrueFalseTotal
unless $super 0 2 2
105  
106 2 my $tries = 0;
107 2 while ( 1) {
108 4 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"attr"}{"ReconnectMaxTries"} and $self->{"attr"}{"ReconnectMaxTries"} <= $tries) 0 4 4
Condition33%1/3
defined $self->{"attr"}{"ReconnectMaxTries"} and $self->{"attr"}{"ReconnectMaxTries"} <= $tries
!ll&&!rl&&r
040
MC/DC0%0/2
defined $self->{"attr"}{"ReconnectMaxTries"} and $self->{"attr"}{"ReconnectMaxTries"} <= $tries
defined $self->{"attr"}{"ReconnectMaxTries"} $self->{"attr"}{"ReconnectMaxTries"} <= $tries
Truth table33%1/3
A and B
A= defined $self->{"attr"}{"ReconnectMaxTries"}B= $self->{"attr"}{"ReconnectMaxTries"} <= $tries
ABresult
0X0
100
111
109 defined ($self->{attr}-> {ReconnectMaxTries}) and
110 $self->{attr}-> {ReconnectMaxTries} <= $tries
111 ) {
112 0 ▶ if ( $conninfo-> [3]-> {RaiseError}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($conninfo->[3]{'RaiseError'}) { } 0 0 0
113 0 die "DBIx::Roles::AutoReconnect: Tried to call '$method' $self->{attr}->{ReconnectMaxTries} time(s), giving up\n";
114 } else {
115 warn "DBIx::Roles::AutoReconnect: Tried to call '$method' $self->{attr}->{ReconnectMaxTries} time(s), giving up\n" if
116 not exists ($conninfo->[3]->{PrintError}) # DBI defaults
117 0 ▶ or $conninfo->[3]->{PrintError};
Branch: 1 branch
BranchTrueFalseTotal
if not exists $conninfo->[3]{"PrintError"} or $conninfo->[3]{"PrintError"} 0 0 0
Condition0%0/3
not exists $conninfo->[3]{"PrintError"} or $conninfo->[3]{"PrintError"}
l!l&&r!l&&!r
000
MC/DC0%0/2
not exists $conninfo->[3]{"PrintError"} or $conninfo->[3]{"PrintError"}
not exists $conninfo->[3]{"PrintError"} $conninfo->[3]{"PrintError"}
Truth table0%0/3
not(A) or B
A= not exists $conninfo->[3]{"PrintError"}B= $conninfo->[3]{"PrintError"}
ABresult
1X1
011
000
118 0 return;
119 }
120 }
121 4 $tries++;
122  
123 4 ▶ unless ( $self-> dbh) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->dbh) 0 4 4
124 $conninfo-> [3]-> {RaiseError} ?
125 0 ▶ croak( "DBIx::Roles::AutoReconnect: not connected" ) :
Branch: 1 branch
BranchTrueFalseTotal
$conninfo->[3]{'RaiseError'} ? : 0 0 0
126 return;
127 }
128 # repeatedly call the roles below until they succeed
129 {
130 4 local $self-> object-> {RaiseError} = 1;
131 4 my $context = $self-> context;
132 4 eval {
133 4 ▶ if ( $wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if ($wantarray) { } 0 4 4
134 0 @ret = $super-> ($self, $private, $method, @parameters);
135 } else {
136 4 $ret[0] = $super-> ($self, $private, $method, @parameters);
137 }
138 };
139 4 ▶ return wantarray ? @ret : $ret[0]
Branch: 2 branches
BranchTrueFalseTotal
unless $@ 2 2 4
wantarray ? : 0 2 2
140 unless $@;
141 # restore context if calls are restarted
142 2 $self-> context( $context);
143 }
144 2 ▶ if ( $self-> dbh-> ping) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->dbh->ping) { } 0 2 2
145 # DB is alive, most probably that was not a DBI-related error
146 0 ▶ if ( $conninfo-> [3]-> {RaiseError}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($conninfo->[3]{'RaiseError'}) { } 0 0 0
147 0 die $@;
148 } else {
149 warn $@ if
150 not (exists $conninfo->[3]->{PrintError}) # DBI defaults
151 0 ▶ or $conninfo->[3]->{PrintError};
Branch: 1 branch
BranchTrueFalseTotal
if not exists $conninfo->[3]{"PrintError"} or $conninfo->[3]{"PrintError"} 0 0 0
Condition0%0/3
not exists $conninfo->[3]{"PrintError"} or $conninfo->[3]{"PrintError"}
l!l&&r!l&&!r
000
MC/DC0%0/2
not exists $conninfo->[3]{"PrintError"} or $conninfo->[3]{"PrintError"}
not exists $conninfo->[3]{"PrintError"} $conninfo->[3]{"PrintError"}
Truth table0%0/3
not(A) or B
A= not exists $conninfo->[3]{"PrintError"}B= $conninfo->[3]{"PrintError"}
ABresult
1X1
011
000
152 0 return;
153 }
154 } else {
155 # without disconnect
156 2 $self-> dbh( $self-> connect( @$conninfo));
157 }
158 }
159 }
160  
161 sub STORE
162 17 ▶{
Subroutine STORE: called CC=9 SCAR=27.7
163 17 my ( $self, $conninfo, $key, $val) = @_;
164 17 ▶ if ( $key eq 'ReconnectTimeout' or $key eq 'ReconnectMaxTries') {
Branch: 3 branches
BranchTrueFalseTotal
if ($key eq 'ReconnectTimeout' or $key eq 'ReconnectMaxTries') { } 4 13 17
elsif ($key eq 'ReconnectFailure') { } 0 13 13
elsif (not exists $self->{'defaults'}{$key}) { } 8 5 13
Condition100%3/3
$key eq "ReconnectTimeout" or $key eq "ReconnectMaxTries"
l!l&&r!l&&!r
2213
MC/DC100%2/2
$key eq "ReconnectTimeout" or $key eq "ReconnectMaxTries"
$key eq "ReconnectTimeout" $key eq "ReconnectMaxTries"
Truth table100%3/3
A or B
A= $key eq "ReconnectTimeout"B= $key eq "ReconnectMaxTries"
ABresult
1X1
011
000
165 4 ▶ die "Fatal: '$key' must be a positive integer"
Branch: 1 branch
BranchTrueFalseTotal
unless $val =~ /^\d+$/ 0 4 4
166 unless $val =~ /^\d+$/;
167 } elsif ( $key eq 'ReconnectFailure') {
168 0 ▶ die "Fatal: '$key' must be either 'undef' or a CODE reference"
Branch: 1 branch
BranchTrueFalseTotal
if not defined $val && ref $val or ref $val ne "CODE" 0 0 0
Condition0%0/6
not defined $val && ref $val or ref $val ne "CODE"
l!l&&r!l&&!r
000
defined $val && ref $val
!ll&&!rl&&r
000
MC/DC0%0/3
not defined $val && ref $val or ref $val ne "CODE"
defined $val ref $val ref $val ne "CODE"
Truth table0%0/4
not(A && B) or C
A= defined $valB= ref $valC= ref $val ne "CODE"
ABCresult
0XX1
10X1
1111
1100
169 if not defined($val) or not ref($val) or ref($val) ne 'CODE';
170 } elsif ( not exists $self->{defaults}->{$key}) {
171 # update $attr for eventual reconnects
172 8 $conninfo->[3]->{$key} = $val;
173 }
174  
175
176 17 return $self-> super( $key, $val);
177 }
178  
179 1 1;
180  
181 __DATA__
↑ Summary Roles/RaiseError.pm »