1 package # hide from pause until we figure it all out
2 DBIx::Class::Storage::BlockRunner;
4 use Sub::Quote 'quote_sub';
5 use DBIx::Class::Exception;
7 use Context::Preserve 'preserve_context';
8 use DBIx::Class::_Util 'is_exception';
9 use Scalar::Util qw(weaken blessed reftype);
12 use warnings NONFATAL => 'all';
17 DBIx::Class::Storage::BlockRunner - Try running a block of code until success with a configurable retry logic
35 # true - retry, false - rethrow, or you can throw your own (not catching)
36 has retry_handler => (
40 (Scalar::Util::reftype($_[0])||'') eq 'CODE'
41 or DBIx::Class::Exception->throw('retry_handler must be a CODE reference')
47 # use a sub - to be evaluated on the spot lazily
48 default => quote_sub( '$ENV{DBIC_STORAGE_RETRY_DEBUG}' ),
57 has failed_attempt_count => (
59 init_arg => undef, # ensures one can't pass the value in
60 writer => '_set_failed_attempt_count',
63 trigger => quote_sub(q{
64 $_[0]->throw_exception( sprintf (
65 'Reached max_attempts amount of %d, latest exception: %s',
66 $_[0]->max_attempts, $_[0]->last_exception
67 )) if $_[0]->max_attempts <= ($_[1]||0);
71 has exception_stack => (
74 clearer => '_reset_exception_stack',
75 default => quote_sub(q{ [] }),
79 sub last_exception { shift->exception_stack->[-1] }
81 sub throw_exception { shift->storage->throw_exception (@_) }
86 $self->_reset_exception_stack;
87 $self->_set_failed_attempt_count(0);
91 $self->throw_exception('run() requires a coderef to execute as its first argument')
92 if ( reftype($cref)||'' ) ne 'CODE';
94 my $storage = $self->storage;
96 return $cref->( @_ ) if (
97 $storage->{_in_do_block}
102 local $storage->{_in_do_block} = 1 unless $storage->{_in_do_block};
104 return $self->_run($cref, @_);
107 # this is the actual recursing worker
109 # internal method - we know that both refs are strong-held by the
110 # calling scope of run(), hence safe to weaken everything
111 weaken( my $self = shift );
112 weaken( my $cref = shift );
114 my $args = @_ ? \@_ : [];
116 # from this point on (defined $txn_init_depth) is an indicator for wrap_txn
117 # save a bit on method calls
118 my $txn_init_depth = $self->wrap_txn ? $self->storage->transaction_depth : undef;
123 return preserve_context {
125 if (defined $txn_init_depth) {
126 $self->storage->txn_begin;
132 (); # important, affects @_ below
137 my $storage = $self->storage;
138 my $cur_depth = $storage->transaction_depth;
140 if (defined $txn_init_depth and $run_err eq '') {
141 my $delta_txn = (1 + $txn_init_depth) - $cur_depth;
144 # a rollback in a top-level txn_do is valid-ish (seen in the wild and our own tests)
146 'Unexpected reduction of transaction depth by %d after execution of '
147 . '%s, skipping txn_commit()',
150 ) unless $delta_txn == 1 and $cur_depth == 0;
153 $run_err = eval { $storage->txn_commit; 1 } ? '' : $@;
157 # something above threw an error (could be the begin, the code or the commit)
158 if ( is_exception $run_err ) {
160 # attempt a rollback if we did begin in the first place
162 # some DBDs go crazy if there is nothing to roll back on, perform a soft-check
163 my $rollback_exception = $storage->_seems_connected
164 ? (! eval { $storage->txn_rollback; 1 }) ? $@ : ''
165 : 'lost connection to storage'
168 if ( $rollback_exception and (
169 ! defined blessed $rollback_exception
171 ! $rollback_exception->isa('DBIx::Class::Storage::NESTED_ROLLBACK_EXCEPTION')
173 $run_err = "Transaction aborted: $run_err. Rollback failed: $rollback_exception";
177 push @{ $self->exception_stack }, $run_err;
179 # this will throw if max_attempts is reached
180 $self->_set_failed_attempt_count($self->failed_attempt_count + 1);
182 # init depth of > 0 ( > 1 with AC) implies nesting - no retry attempt queries
183 $storage->throw_exception($run_err) if (
185 defined $txn_init_depth
187 # FIXME - we assume that $storage->{_dbh_autocommit} is there if
188 # txn_init_depth is there, but this is a DBI-ism
189 $txn_init_depth > ( $storage->{_dbh_autocommit} ? 0 : 1 )
190 ) or ! $self->retry_handler->($self)
193 # we got that far - let's retry
194 carp( sprintf 'Retrying %s (attempt %d) after caught exception: %s',
196 $self->failed_attempt_count + 1,
198 ) if $self->retry_debug;
200 $storage->ensure_connected;
201 # if txn_depth is > 1 this means something was done to the
202 # original $dbh, otherwise we would not get past the preceding if()
203 $storage->throw_exception(sprintf
204 'Unexpected transaction depth of %d on freshly connected handle',
205 $storage->transaction_depth,
206 ) if (defined $txn_init_depth and $storage->transaction_depth);
208 return $self->_run($cref, @$args);
211 return wantarray ? @res : $res[0];
215 =head1 AUTHOR AND CONTRIBUTORS
217 See L<AUTHOR|DBIx::Class/AUTHOR> and L<CONTRIBUTORS|DBIx::Class/CONTRIBUTORS> in DBIx::Class
221 You may distribute this code under the same terms as Perl itself.