Action-Retry
view release on metacpan or search on metacpan
lib/Action/Retry.pm view on Meta::CPAN
my ($error, $h) = @_;
my $attempt_code_result = $h->{attempt_result};
my $attempt_code_params = $h->{attempt_parameters};
my @results = @$attempt_code_result;
# will contains (2, 4);
my @original_parameters = @$attempt_code_params;
# will contains (1, 2);
}
);
my @results = $action->run(1, 2);
=head2 on_failure_code
ro, CodeRef, optional
If given, will be executed when retries are given up.
It will be given the same arguments as C<retry_if_code>. See C<retry_if_code> for their descriptions
=head2 strategy
ro, defaults to 'Constant'
The strategy for managing retrying times, sleeping, and giving up. It must be
an object that does the L<Action::Retry::Strategy> role.
This attribute has a coercion from strings and hashrefs. If you pass a string,
it will be treated as a class name (under C<Action::Retry::Strategy::>, unless
it is prefxed with a C<+>) to instantiate.
If you pass a hashref, the first key will be treated as a class name as above,
and the value of that key will be treated as the args to pass to C<new>.
Some existing stragies classes are L<Action::Retry::Strategy::Constant>,
L<Action::Retry::Strategy::Fibonacci>, L<Action::Retry::Strategy::Linear>.
Defaults to C<'Constant'>
=head2 non_blocking
ro, defaults to 0
If true, the instance will be in a pseudo non blocking mode. In this mode, the
C<run()> function behaves a bit differently: instead of sleeping between
retries, the C<run()> command will immediately return. Subsequent call to
C<run()> will immediately return, until the time to sleep has been elapsed.
This allows to do things like that:
my $action = Action::Retry->new( ... , non_blocking => 1 );
while (1) {
# if the action failed, it doesn't sleep
# next time it's called, it won't do anything until it's time to retry
$action->run();
# do something else while time goes on
}
If you need a more advanced non blocking mode and callbacks, then look at L<AnyEvent::Retry>
=head1 METHODS
=head2 run
Does the following:
=over
=item step 1
Runs the C<attempt_code> CodeRef in the proper context in an eval {} block,
saving C<$@> in C<$error>.
=item step 2
Runs the C<retry_if_code> CodeRef in scalar context, giving it as arguments
C<$error>, and the return values of C<attempt_code>. If it returns true, we
consider that it was a failure, and move to step 3. Otherwise, we consider it
means success, and return the return values of C<attempt_code>.
=item step 3
Ask the C<strategy> if it's still useful to retry. If yes, sleep accordingly,
and go back to step 2. If not, go to step 4.
=item step 4
Runs the C<on_failure_code> CodeRef in the proper context, giving it as
arguments C<$error>, and the return values of C<attempt_code>, and returns the
results back to the caller.
=back
Arguments passed to C<run()> will be passed to C<attempt_code>. They will also
passed to C<on_failure_code> as well if the case arises.
=head2 retry
retry { ..code.. } some => 'arguments';
Is equivalent to
Action::Retry->new(attempt_code => sub { ..code.. }, some => arguments )->run();
A functional interface, alternative to the OO interface.
=head1 SRATEGIES
Here are the strategies currently included by default. Check their
documentation for more details.
=over
=item L<Action::Retry::Strategy::Constant>
Provides a simple constant sleep time strategy
=item L<Action::Retry::Strategy::Fibonacci>
( run in 0.644 second using v1.01-cache-2.11-cpan-cdf2f3d4e48 )