| 1 | package DBIx::Connector::Retry; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | 3 | our $AUTHORITY = 'cpan:GSG'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | # ABSTRACT: DBIx::Connector with block retry support | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | 3 | ▶ | use version; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | 3 | our $VERSION = 'v0.900.2'; # VERSION | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | 3 | ▶ | use Moo; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | 3 | extends 'DBIx::Connector', 'Moo::Object'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | 3 | ▶ | use Scalar::Util qw( weaken ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | 3 | ▶ | use Types::Standard qw( Str Bool HashRef CodeRef Dict Tuple Optional Maybe ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | 3 | ▶ | use Types::Common::Numeric qw( PositiveInt ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | 3 | ▶ | use namespace::clean; # don't export the above | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | #pod =encoding utf8 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | #pod =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | #pod my $conn = DBIx::Connector::Retry->new( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | #pod connect_info => [ 'dbi:Driver:database=foobar', $user, $pass, \%args ], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | #pod retry_debug => 1, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | #pod max_attempts => 5, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | #pod ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | #pod # Keep retrying/reconnecting on errors | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | #pod my ($count) = $conn->run(ping => sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | #pod $_->do('UPDATE foobar SET updated = 1 WHERE active = ?', undef, 'on'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | #pod $_->selectrow_array('SELECT COUNT(*) FROM foobar WHERE updated = 1'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | #pod }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | #pod # Add a simple retry_handler for a manual timeout | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | #pod my $start_time = time; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | #pod $conn->retry_handler(sub { time <= $start_time + 60 }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | #pod my ($count) = $conn->txn(fixup => sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | #pod $_->selectrow_array('SELECT COUNT(*) FROM barbaz'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | #pod }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | #pod $conn->clear_retry_handler; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | #pod # Plus everything else in DBIx::Connector | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | #pod =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | #pod DBIx::Connector::Retry is a Moo-based subclass of L<DBIx::Connector> that will retry on | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | #pod failures. Most of the interface was modeled after L<DBIx::Class::Storage::BlockRunner> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | #pod and adapted for use in DBIx::Connector. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | #pod =head1 ATTRIBUTES | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | #pod =head2 connect_info | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | #pod An arrayref that contains all of the connection details normally found in the L<DBI> or | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | #pod L<DBIx::Connector> call. This data can be changed, but won't take effect until the next | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | #pod C<$dbh> re-connection cycle. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | #pod Obviously, this is required. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | #pod =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | 3 | has connect_info => ( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | is => 'rw', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | # Yes, DBI->connect() is still technically-valid syntax | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | isa => Tuple[ Maybe[Str], Maybe[Str], Maybe[Str], Optional[HashRef] ], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | required => 1, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | #pod =head2 mode | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | #pod This is just like L<DBIx::Connector/mode> except that it can be set from within the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | #pod constructor. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | #pod Unlike DBIx::Connector, the default is C<ping>, not C<no_ping>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | #pod =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | 3 | has _mode => ( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | is => 'bare', # use DBIx::Connector's accessor | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | isa => Str, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | init_arg => 'mode', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | required => 0, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | default => 'ping', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | #pod =head2 disconnect_on_destroy | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | #pod This is just like L<DBIx::Connector/disconnect_on_destroy> except that it can be set | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | #pod from within the constructor. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | #pod Default is on. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | #pod =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | 3 | has _dond => ( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | is => 'bare', # use DBIx::Connector's accessor | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | isa => Bool, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | init_arg => 'disconnect_on_destroy', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | required => 0, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | default => 1, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | #pod =head2 max_attempts | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | #pod The maximum amount of block running attempts before the Connector gives up and dies. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 111 | #pod Default is 10. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 113 | #pod =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 115 | 3 | has max_attempts => ( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 116 | is => 'rw', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 117 | isa => PositiveInt, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 118 | required => 0, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 119 | default => 10, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 120 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 122 | #pod =head2 retry_debug | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 123 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 124 | #pod If enabled, any retries will output a debug warning with the error message and number | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 125 | #pod of retries. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 126 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 127 | #pod =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 129 | 3 | has retry_debug => ( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 130 | is => 'rw', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 131 | isa => Bool, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 132 | required => 0, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 133 | default => 0, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 134 | lazy => 1, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 135 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 137 | sub _warn_retry_debug { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 138 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_warn_retry_debug: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 140 | 0 | my $current_attempt_count = $self->failed_attempt_count + 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 142 | 0 | warn sprintf( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 143 | 'Retrying %s coderef (attempt %d) after caught exception: %s', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 144 | $self->execute_method, $current_attempt_count, $self->last_exception | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 145 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 148 | #pod =head2 retry_handler | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 149 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 150 | #pod An optional handler that will be checked on each retry. It will be passed the Connector | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 151 | #pod object as its only input. If the handler returns a true value, retries will continue. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 152 | #pod A false value will cause the retry loop to immediately rethrow the exception. You can | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 153 | #pod also throw your own, if you prefer. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 154 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 155 | #pod This check is independent of checks for L</max_attempts>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 156 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 157 | #pod The last exception can be inspected as part of the check by looking at L</last_exception>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 158 | #pod This is recommended to make sure the failure is actually what you expect it to be. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 159 | #pod For example: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 160 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 161 | #pod $conn->retry_handler(sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 162 | #pod my $c = shift; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 163 | #pod my $err = $c->last_exception; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 164 | #pod $err = $err->error if blessed $err && $err->isa('DBIx::Connector::RollbackError'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 165 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 166 | #pod # only retry on deadlocks or timeouts (only look in the first line | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 167 | #pod # of the error to avoid e.g. accidental matches in a stack trace) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 168 | #pod $err =~ /^\V*(?:deadlock|timeout)/i; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 169 | #pod }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 170 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 171 | #pod Default is an always-true coderef. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 172 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 173 | #pod This attribute has the following handles: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 174 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 175 | #pod =head3 clear_retry_handler | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 176 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 177 | #pod Sets it back to the always-true default. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 178 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 179 | #pod =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 181 | has retry_handler => ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 182 | is => 'rw', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 183 | isa => CodeRef, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 184 | required => 1, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 185 | 9 | ▶ | default => sub { sub { 1 } }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 186 | 3 | ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 188 | 0 | ▶ | sub clear_retry_handler { shift->retry_handler(sub { 1 }) } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
Subroutine
clear_retry_handler: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 190 | #pod =head2 execute_method | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 191 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 192 | #pod The current L<DBIx::Connector> execution method name being called, which would either be | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 193 | #pod C<run> or C<txn>. Since C<svp> is not overridden, it would never be encountered. If the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 194 | #pod connector is not in the middle of DB block execution, this attribute is blank. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 195 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 196 | #pod =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 198 | 3 | has execute_method => ( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 199 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 200 | isa => Str, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 201 | init_arg => undef, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 202 | writer => '_set_execute_method', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 203 | default => '', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 204 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 206 | #pod =head2 failed_attempt_count | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 207 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 208 | #pod The number of failed attempts so far. This can be used in the L</retry_handler> or | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 209 | #pod checked afterwards. It will be reset on each block run. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 210 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 211 | #pod Not available for initialization. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 212 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 213 | #pod =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 215 | has failed_attempt_count => ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 216 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 217 | init_arg => undef, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 218 | writer => '_set_failed_attempt_count', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 219 | default => 0, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 220 | lazy => 1, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 221 | trigger => sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 222 | 24 | ▶ | my ($self, $val) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=12.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 223 | 24 | ▶ | $self->_die_from_max_attempts if $self->max_attempts <= ( $val || 0 ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$val || 0
MC/DC0%0/1
$val || 0
$val
Truth table50%1/2
A
A= $val
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 224 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 225 | 3 | ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 227 | sub _die_from_max_attempts { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 228 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_die_from_max_attempts: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 229 | 0 | die sprintf ( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 230 | 'Reached max_attempts amount of %d, latest exception: %s', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 231 | $self->max_attempts, $self->last_exception | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 232 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 233 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 235 | #pod =head2 exception_stack | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 236 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 237 | #pod The stack of exceptions received so far, as an arrayref. This can be used in the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 238 | #pod L</retry_handler> or checked afterwards. It will be reset on each block run. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 239 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 240 | #pod Not available for initialization. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 241 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 242 | #pod This attribute has the following handles: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 243 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 244 | #pod =head3 last_exception | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 245 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 246 | #pod The last exception on the stack. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 247 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 248 | #pod =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 250 | has exception_stack => ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 251 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 252 | init_arg => undef, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 253 | clearer => '_reset_exception_stack', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 254 | 0 | ▶ | default => sub { [] }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 255 | 3 | lazy => 1, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 256 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 257 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 258 | 0 | ▶ | sub last_exception { shift->exception_stack->[-1] } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
last_exception: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 259 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 260 | #pod =head1 CONSTRUCTORS | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 261 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 262 | #pod =head2 new | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 263 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 264 | #pod my $conn = DBIx::Connector::Retry->new( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 265 | #pod connect_info => [ 'dbi:Driver:database=foobar', $user, $pass, \%args ], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 266 | #pod max_attempts => 5, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 267 | #pod # ...etc... | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 268 | #pod ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 269 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 270 | #pod # Old-DBI syntax | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 271 | #pod my $conn = DBIx::Connector::Retry->new( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 272 | #pod 'dbi:Driver:database=foobar', $user, $pass, \%dbi_args, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 273 | #pod max_attempts => 5, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 274 | #pod # ...etc... | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 275 | #pod ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 276 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 277 | #pod As this is a L<Moo> class, it uses the standard Moo constructor. The L</connect_info> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 278 | #pod should be specified as its own key. The L<DBI>/L<DBIx::Connector> syntax is available, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 279 | #pod but only as a nicety for compatibility. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 280 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 281 | #pod =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 282 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 283 | around BUILDARGS => sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 284 | 14 | ▶ | my ($orig, $class, @args) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=13 SCAR=25.8
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 285 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 286 | # Old-style DBI/DBIx::Connector parameters. Try to fix it up. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 287 | 14 | ▶ | if (@args && $args[0] && !ref $args[0] && $args[0] =~ /^dbi:/) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition88%8/9
@args and $args[0] and not ref $args[0] and $args[0] =~ /^dbi:/
@args and $args[0] and not ref $args[0]
@args and $args[0]
MC/DC75%3/4
@args and $args[0] and not ref $args[0] and $args[0] =~ /^dbi:/
@args
$args[0]
not ref $args[0]
$args[0] =~ /^dbi:/
Truth table80%4/5
A and B and not(C) and D
A= @argsB= $args[0]C= not ref $args[0]D= $args[0] =~ /^dbi:/
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 288 | 6 | my @connect_info = splice(@args, 0, 3); # DBI DSN, UN, PW | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 289 | 6 | ▶ | push @connect_info, shift @args if $args[0] && (ref $args[0]||'') eq 'HASH'; # DBI \%attr, if it exists | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition80%4/5
$args[0] and (ref $args[0] || "") eq "HASH"
ref $args[0] || ''
MC/DC50%1/2
$args[0] and (ref $args[0] || "") eq "HASH"
$args[0]
ref $args[0]
Truth table66%2/3
A and B
A= $args[0]B= ref $args[0]
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 290 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 291 | 6 | ▶ | if ( @args && $args[0] && (my $ref = ref $args[0]) ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
@args and $args[0] and my $ref = ref $args[0]
@args and $args[0]
MC/DC66%2/3
@args and $args[0] and my $ref = ref $args[0]
@args
$args[0]
my $ref = ref $args[0]
Truth table75%3/4
A and B and C
A= @argsB= $args[0]C= my $ref = ref $args[0]
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 292 | 3 | ▶ | if ($ref eq 'ARRAY') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 293 | 2 | push @{$args[0]}, ( connect_info => \@connect_info ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 294 | 2 | @args = @{$args[0]}; # Moo::Object::BUILDARGS doesn't actually support lone ARRAYREFs | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 295 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 296 | elsif ($ref eq 'HASH') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 297 | 1 | $args[0]{connect_info} = \@connect_info; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 298 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 299 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 300 | # Mimicing Moo::Object::BUILDARGS here | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 301 | 0 | Carp::croak(join ' ', | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 302 | "The new() method for $class cannot parse the strange argument list.", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 303 | "Please switch to a standard Moo constructor, instead of the DBI syntax.", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 304 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 305 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 306 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 307 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 308 | # either the key within a list or we're out of arguments | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 309 | 3 | push @args, ( connect_info => \@connect_info ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 310 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 311 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 312 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 313 | 14 | return $class->$orig(@args); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 314 | 3 | }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 315 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 316 | sub BUILD { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 317 | 12 | ▶ | my ($self, $args) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BUILD: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 318 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 319 | 12 | my @connect_args = @{ $self->connect_info }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 320 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 321 | # Add in the keys that DBIx::Connector expects. For the purposes of future | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 322 | # expandability of DBIx::Connector, we do this by getting a new base Connector | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 323 | # object, and inject those properties into our own object. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 324 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 325 | 12 | my $base_obj = DBIx::Connector->new(@connect_args); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 326 | 12 | %$self = ( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 327 | %$base_obj, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 328 | %$self, # $self's existing attributes take priority | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 329 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 330 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 331 | # DBIx::Connector stores connection details in a coderef (for some reason). Instead | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 332 | # of just dumping the same arguments as another copy, we'll tie it directly to the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 333 | # attr. If connect_info ever changes, it will grab the latest version. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 334 | 12 | ▶ | $self->{_args} = sub { @{ $self->connect_info } }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 335 | 12 | weaken $self; # circular closure ref | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 336 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 337 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 338 | #pod =head1 MODIFIED METHODS | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 339 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 340 | #pod =head2 run / txn | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 341 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 342 | #pod my @result = $conn->run($mode => $coderef); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 343 | #pod my $result = $conn->run($mode => $coderef); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 344 | #pod $conn->run($mode => $coderef); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 345 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 346 | #pod my @result = $conn->txn($mode => $coderef); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 347 | #pod my $result = $conn->txn($mode => $coderef); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 348 | #pod $conn->txn($mode => $coderef); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 349 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 350 | #pod Both L<run|DBIx::Connector/run> and L<txn|DBIx::Connector/txn> are modified to run inside | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 351 | #pod a retry loop. If the original Connector action dies, the exception is caught, and if | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 352 | #pod L</retry_handler> and L</max_attempts> allows it, the action is retried. The database | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 353 | #pod handle may be reset by the Connector action, according to its connection mode. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 354 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 355 | #pod See L</CAVEATS> for important behaviors/limitations. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 356 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 357 | #pod =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 358 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 359 | 3 | foreach my $method (qw< run txn >) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 360 | around $method => sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 361 | 35 | ▶ | my $orig = shift; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 362 | 35 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 363 | 35 | ▶ | my $mode = ref $_[0] eq 'CODE' ? $self->{_mode} : shift; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 364 | 35 | my $cref = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 365 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 366 | 35 | my $wantarray = wantarray; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 367 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 368 | 35 | return $self->_retry_loop($orig, $method, $mode, $cref, $wantarray); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 369 | 6 | }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 370 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 371 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 372 | sub _retry_loop { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 373 | 35 | ▶ | my ($self, $orig, $method, $mode, $cref, $wantarray) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_retry_loop: called CC=11 SCAR=24.3
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 374 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 375 | # For the purposes of nesting, these variables should be localized. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 376 | 35 | local $self->{exception_stack} = []; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 377 | 35 | local $self->{failed_attempt_count} = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 378 | 35 | local $self->{execute_method} = $method; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 379 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 380 | # If we already started in a transaction, that implies nesting, so don't | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 381 | # retry the query. We can't guarantee that the statements before the block | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 382 | # run will be committed, and are assuming that the connection will break. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 383 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 384 | # We cannot rely on checking the database connection via ping, because some | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 385 | # DBDs (like mysql) will try to reconnect to the DB if the first ping check | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 386 | # fails, and a reconnection auto-rollbacks all transactions, locks, etc. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 387 | 35 | ▶ | if ($self->in_txn) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 388 | 25 | ▶ | unless (defined $wantarray) { return $self->$orig($mode, $cref) } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 389 | 0 | elsif ($wantarray) { return ($self->$orig($mode, $cref)) } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 390 | 0 | else { return scalar $self->$orig($mode, $cref) } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 391 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 392 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 393 | # Mode is localized within $orig, but we should localize it again ourselves, in case | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 394 | # it's changed on-the-fly. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 395 | 10 | local $self->{_mode} = $mode; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 396 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 397 | 10 | my $run_err; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 398 | my @res; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 399 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 400 | 10 | ▶ | do { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 401 | TRY: { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 402 | 34 | local $@; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 403 | 34 | eval { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 404 | 34 | ▶ | unless (defined $wantarray) { $self->$orig($mode, $cref) } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 405 | 1 | elsif ($wantarray) { @res = $self->$orig($mode, $cref) } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 406 | 1 | else { $res[0] = $self->$orig($mode, $cref) } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 407 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 408 | 34 | $run_err = $@; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 409 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 410 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 411 | 34 | ▶ | if ($run_err) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 412 | 24 | push @{ $self->exception_stack }, $run_err; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 413 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 414 | # This will throw if max_attempts is reached | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 415 | 24 | $self->_set_failed_attempt_count($self->failed_attempt_count + 1); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 416 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 417 | # If the retry handler says no, then die | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 418 | 24 | ▶ | die $run_err unless $self->retry_handler->($self); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 419 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 420 | # Debug line | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 421 | 24 | ▶ | $self->_warn_retry_debug if $self->retry_debug; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 422 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 423 | } while ($run_err); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 424 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 425 | 10 | ▶ | return $wantarray ? @res : $res[0]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 426 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 427 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 428 | #pod =head1 CAVEATS | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 429 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 430 | #pod =head2 $dbh settings | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 431 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 432 | #pod Like L<DBIx::Connector>, it's important that the L</connect_info> properties have sane | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 433 | #pod connection settings. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 434 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 435 | #pod L<AutoCommit|DBI/AutoCommit> should be turned on. Otherwise, the connection is | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 436 | #pod considered to be already in a transaction, and no retries will be attempted. Instead, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 437 | #pod use transactions via L<txn|DBIx::Connector/txn>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 438 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 439 | #pod L<RaiseError|DBI/RaiseError> should also be turned on, since exceptions are captured, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 440 | #pod and both Retry and Connector use them to determine if any of the C<$dbh> calls failed. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 441 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 442 | #pod =head2 Savepoints and nested transactions | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 443 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 444 | #pod L<The svp method|DBIx::Connector/svp> is NOT modified to work inside of a retry loop, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 445 | #pod because retries are generally not possible for savepoints, and a disconnected connection | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 446 | #pod will rollback any uncommited statements in most RDBMS. The same goes for any C<run>/C<txn> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 447 | #pod calls attempted inside of a transaction. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 448 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 449 | #pod Consider the following: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 450 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 451 | #pod # If this dies, sub will retry | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 452 | #pod $conn->txn(ping => sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 453 | #pod shift->do('UPDATE foobar SET updated = 1 WHERE active = ?', undef, 'on'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 454 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 455 | #pod # If this dies, it will not retry | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 456 | #pod $conn->svp(sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 457 | #pod my $c = shift; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 458 | #pod $c->do('INSERT foobar (name, updated, active) VALUES (?, ?)', undef, 'barbaz', 0, 'off'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 459 | #pod }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 460 | #pod }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 461 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 462 | #pod If the savepoint actually tried to retry, the C<UPDATE> statement would get rolled back by | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 463 | #pod virtue of database disconnection. However, the savepoint code would continue, possibly | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 464 | #pod even succeeding. You would never know that the C<UPDATE> statement was rolled back. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 465 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 466 | #pod However, without savepoint retry support, as it is currently designed, the statements | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 467 | #pod will work as expected. If the savepoint code dies, and if C<$conn> is set up for | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 468 | #pod retries, the transaction code is restarted, after a rollback or reconnection. Thus, the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 469 | #pod C<UPDATE> and C<INSERT> statements are both ran properly if they now succeed. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 470 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 471 | #pod Obviously, this will not work if transactions are manually started outside of the main | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 472 | #pod Connector interface: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 473 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 474 | #pod # Don't do this! The whole transaction isn't compartmentalized properly! | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 475 | #pod $conn->run(ping => sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 476 | #pod $_->begin_work; # don't ever call this! | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 477 | #pod $_->do('UPDATE foobar SET updated = 1 WHERE active = ?', undef, 'on'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 478 | #pod }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 479 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 480 | #pod # If this dies, the whole app will probably crash | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 481 | #pod $conn->svp(sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 482 | #pod my $c = shift; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 483 | #pod $c->do('INSERT foobar (name, updated, active) VALUES (?, ?)', undef, 'barbaz', 0, 'off'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 484 | #pod }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 485 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 486 | #pod # Don't do this! | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 487 | #pod $conn->run(ping => sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 488 | #pod $_->commit; # no, let Connector handle this process! | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 489 | #pod }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 490 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 491 | #pod =head2 Fixup mode | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 492 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 493 | #pod Because of the nature of L<fixup mode|DBIx::Connector/Connection Modes>, the block may be | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 494 | #pod executed twice as often. Functionally, the code looks like this: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 495 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 496 | #pod # Very simplified example | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 497 | #pod sub fixup_run { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 498 | #pod my ($self, $code) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 499 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 500 | #pod my (@ret, $run_err); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 501 | #pod do { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 502 | #pod eval { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 503 | #pod @ret = eval { $code->($dbh) }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 504 | #pod my $err = $@; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 505 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 506 | #pod if ($err) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 507 | #pod die $err if $self->connected; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 508 | #pod # Not connected. Try again. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 509 | #pod return $code->($dbh); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 510 | #pod } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 511 | #pod }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 512 | #pod $run_err = $@; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 513 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 514 | #pod if ($run_err) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 515 | #pod # Push exception_stack, set/check attempts, check retry_handler | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 516 | #pod } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 517 | #pod } while ($run_err); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 518 | #pod return @ret; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 519 | #pod } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 520 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 521 | #pod If the first eval dies because of a connection failure, the code is ran twice before the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 522 | #pod retry loop finds it. This is only considered to be one attempt. If it dies because of | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 523 | #pod some other fault, it's only ran once and continues the retry loop. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 524 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 525 | #pod If this is behavior is undesirable, this can be worked around by using the L</retry_handler> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 526 | #pod to change the L<mode|DBIx::Connector/mode> after the first attempt: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 527 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 528 | #pod $conn->retry_handler(sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 529 | #pod my $c = shift; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 530 | #pod $c->mode('ping') if $c->mode eq 'fixup'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 531 | #pod 1; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 532 | #pod }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 533 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 534 | #pod Mode is localized outside of the retry loop, so even C<< $conn->run(fixup => $code) >> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 535 | #pod calls work, and the default mode will return to normal after the block run. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 536 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 537 | #pod =head1 SEE ALSO | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 538 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 539 | #pod L<DBIx::Connector>, L<DBIx::Class> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 540 | #pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 541 | #pod =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 542 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 543 | 3 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 544 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 545 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||