Action-Retry
view release on metacpan or search on metacpan
lib/Action/Retry.pm view on Meta::CPAN
=head2 retry_if_code
ro, CodeRef
The code to run to check if we need to retry the action. It defaults to:
# Returns true if there were an exception evaluating to something true
sub { $_[0] }
It will be given these arguments:
=over
=item *
as first argument, a scalar which is the value of any exception that were
raised by the C<attempt_code>. Otherwise, undef.
=item *
as second argument, a HashRef, which contains these keys:
=over
=item action_retry
it's a reference on the ActionRetry instance. That way you can have access to
the strategy and other attributes.
=item attempt_result
It's a scalar, which is the result of C<attempt_code>. If C<attempt_code>
returned a list, then the scalar is the reference on this list.
=item attempt_parameters
It's the reference on the parameters that were given to C<attempt_code>.
=back
=back
C<retry_if_code> return value will be interpreted as a boolean : true return
value means the execution of C<attempt_code> was a failure and it needs to be
retried. False means it went well.
Here is an example of code that gets the arguments properly:
my $action = Action::Retry->new(
attempt_code => sub { do_stuff; } )->run();
attempt_code => sub { map { $_ * 2 } @_ }
retry_if_code => sub {
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
( run in 0.881 second using v1.01-cache-2.11-cpan-39bf76dae61 )