1 package # hide from pause until we figure it all out
2 DBIx::Class::Storage::BlockRunner;
8 use Context::Preserve 'preserve_context';
9 use DBIx::Class::_Util qw( is_exception qsub dbic_internal_try dbic_internal_catch );
10 use Scalar::Util qw(weaken blessed reftype);
16 DBIx::Class::Storage::BlockRunner - Try running a block of code until success with a configurable retry logic
34 # true - retry, false - rethrow, or you can throw your own (not catching)
35 has retry_handler => (
39 (Scalar::Util::reftype($_[0])||'') eq 'CODE'
40 or DBIx::Class::Exception->throw('retry_handler must be a CODE reference')
46 # use a sub - to be evaluated on the spot lazily
47 default => qsub '$ENV{DBIC_STORAGE_RETRY_DEBUG}',
56 has failed_attempt_count => (
58 init_arg => undef, # ensures one can't pass the value in
59 writer => '_set_failed_attempt_count',
63 $_[0]->throw_exception( sprintf (
64 'Reached max_attempts amount of %d, latest exception: %s',
65 $_[0]->max_attempts, $_[0]->last_exception
66 )) if $_[0]->max_attempts <= ($_[1]||0);
70 has exception_stack => (
73 clearer => '_reset_exception_stack',
74 default => qsub q{ [] },
78 sub last_exception { shift->exception_stack->[-1] }
80 sub throw_exception { shift->storage->throw_exception (@_) }
85 $self->_reset_exception_stack;
86 $self->_set_failed_attempt_count(0);
90 $self->throw_exception('run() requires a coderef to execute as its first argument')
91 if ( reftype($cref)||'' ) ne 'CODE';
93 my $storage = $self->storage;
95 return $cref->( @_ ) if (
96 $storage->{_in_do_block}
101 local $storage->{_in_do_block} = 1 unless $storage->{_in_do_block};
103 return $self->_run($cref, @_);
106 # this is the actual recursing worker
108 # internal method - we know that both refs are strong-held by the
109 # calling scope of run(), hence safe to weaken everything
110 weaken( my $self = shift );
111 weaken( my $cref = shift );
113 my $args = @_ ? \@_ : [];
115 # from this point on (defined $txn_init_depth) is an indicator for wrap_txn
116 # save a bit on method calls
117 my $txn_init_depth = $self->wrap_txn ? $self->storage->transaction_depth : undef;
122 return preserve_context {
124 if (defined $txn_init_depth) {
125 $self->storage->txn_begin;
129 } dbic_internal_catch {
131 (); # important, affects @_ below
136 my $storage = $self->storage;
139 defined $txn_init_depth
141 ! is_exception $run_err
143 defined( my $cur_depth = $storage->transaction_depth )
145 my $delta_txn = (1 + $txn_init_depth) - $cur_depth;
148 # a rollback in a top-level txn_do is valid-ish (seen in the wild and our own tests)
150 'Unexpected reduction of transaction depth by %d after execution of '
151 . '%s, skipping txn_commit()',
154 ) unless $delta_txn == 1 and $cur_depth == 0;
158 $storage->txn_commit;
161 dbic_internal_catch {
167 # something above threw an error (could be the begin, the code or the commit)
168 if ( is_exception $run_err ) {
170 # Attempt a rollback if we did begin in the first place
171 # Will append rollback error if possible
172 $storage->__delicate_rollback( \$run_err )
175 push @{ $self->exception_stack }, $run_err;
177 # this will throw if max_attempts is reached
178 $self->_set_failed_attempt_count($self->failed_attempt_count + 1);
180 # init depth of > 0 ( > 1 with AC) implies nesting - no retry attempt queries
181 $storage->throw_exception($run_err) if (
183 defined $txn_init_depth
185 # FIXME - we assume that $storage->{_dbh_autocommit} is there if
186 # txn_init_depth is there, but this is a DBI-ism
187 $txn_init_depth > ( $storage->{_dbh_autocommit} ? 0 : 1 )
191 local $self->storage->{_in_do_block_retry_handler} = 1
192 unless $self->storage->{_in_do_block_retry_handler};
193 $self->retry_handler->($self)
197 # we got that far - let's retry
198 carp( sprintf 'Retrying %s (attempt %d) after caught exception: %s',
200 $self->failed_attempt_count + 1,
202 ) if $self->retry_debug;
204 $storage->ensure_connected;
205 # if txn_depth is > 1 this means something was done to the
206 # original $dbh, otherwise we would not get past the preceding if()
207 $storage->throw_exception(sprintf
208 'Unexpected transaction depth of %d on freshly connected handle',
209 $storage->transaction_depth,
210 ) if (defined $txn_init_depth and $storage->transaction_depth);
212 return $self->_run($cref, @$args);
215 return wantarray ? @res : $res[0];
219 =head1 FURTHER QUESTIONS?
221 Check the list of L<additional DBIC resources|DBIx::Class/GETTING HELP/SUPPORT>.
223 =head1 COPYRIGHT AND LICENSE
225 This module is free software L<copyright|DBIx::Class/COPYRIGHT AND LICENSE>
226 by the L<DBIx::Class (DBIC) authors|DBIx::Class/AUTHORS>. You can
227 redistribute it and/or modify it under the same terms as the
228 L<DBIx::Class library|DBIx::Class/COPYRIGHT AND LICENSE>.