Action-Retry
view release on metacpan or search on metacpan
lib/Action/Retry.pm view on Meta::CPAN
);
sub run {
my $self = shift;
while(1) {
if (my $timestamp = $self->_needs_sleeping_until) {
# we can't retry until we have waited enough time
my ($seconds, $microseconds) = gettimeofday;
$seconds * 1000 + int($microseconds / 1000) >= $timestamp
or return;
$self->_needs_sleeping_until(0);
$self->strategy->next_step;
}
my $error;
my @attempt_result;
my $attempt_result;
my $wantarray;
lib/Action/Retry.pm view on Meta::CPAN
or $self->strategy->reset, $@ = $error, return ( $wantarray ? @attempt_result : $attempt_result );
if (! $self->strategy->needs_to_retry) {
$self->strategy->reset;
$self->has_on_failure_code
and return $self->on_failure_code->($error, $h);
return;
}
if ($self->non_blocking) {
my ($seconds, $microseconds) = gettimeofday;
$self->_needs_sleeping_until($seconds * 1000 + int($microseconds / 1000) + $self->strategy->compute_sleep_time);
} else {
usleep($self->strategy->compute_sleep_time * 1000);
$self->strategy->next_step;
}
}
}
sub retry (&;@) {
my $code = shift;
lib/Action/Retry.pm view on Meta::CPAN
Action::Retry - Module to try to perform an action, with various ways of retrying and sleeping between retries.
=head1 VERSION
version 0.24
=head1 SYNOPSIS
# Simple usage, will attempt to run the code, retrying if it dies, retrying
# 10 times max, sleeping 1 second between retries
# functional interface
use Action::Retry qw(retry);
retry { do_stuff };
# OO interface
use Action::Retry;
Action::Retry->new( attempt_code => sub { do_stuff; } )->run();
lib/Action/Retry.pm view on Meta::CPAN
=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
lib/Action/Retry.pm view on Meta::CPAN
=head1 SEE ALSO
I created this module because the other related modules I found didn't exactly
do what I wanted. Here is the list and why:
=over
=item L<Retry>
No custom checking code. No retry strategies. Can't sleep under one second. No
non-blocking mode. No custom failure code.
=item L<Sub::Retry>
No retry strategies. Can't sleep under one second. Retry code is
passed the results of the attempt code, but not the exception. No non-blocking mode. No custom failure code.
=item Attempt
No custom checking code. Strange exception catching behavior. No retry strategies. No non-blocking mode. No custom failure code.
=item AnyEvent::Retry
Depends on AnyEvent, and Moose. Strategies are less flexibles, and they don't have sleep timeouts (only max tries).
lib/Action/Retry/Strategy/Constant.pm view on Meta::CPAN
version 0.24
=head1 SYNOPSIS
To be used as strategy in L<Action::Retry>
=head1 ATTRIBUTES
=head2 sleep_time
ro, Int, defaults to 1000 ( 1 second )
The number of milliseconds to wait between retries
=head2 max_retries_number
ro, Int|Undef, defaults to 10
The number of times we should retry before giving up. If set to undef, never
stop retrying
=head1 AUTHOR
lib/Action/Retry/Strategy/Fibonacci.pm view on Meta::CPAN
=head2 initial_term_index
ro, Int, defaults to 0
Term number of the Fibonacci sequence to start at. Defaults to 0
=head2 multiplicator
ro, Int, defaults to 1000
Number of milliseconds that will be multiplied by the fibonacci sequence term
value. Defaults to 1000 ( 1 second )
=head2 max_retries_number
ro, Int, defaults to 10
The number of times we should retry before giving up. If set to undef, never stop retrying
=head2 max_sleep_time
ro, Int|Undef, defaults to undef
If Action::Retry is about to sleep more than this number ( in milliseconds ),
stop retrying. If set to undef, never stop retrying
=head1 AUTHOR
Damien "dams" Krotkine
=head1 COPYRIGHT AND LICENSE
This software is copyright (c) 2013 by Damien "dams" Krotkine.
lib/Action/Retry/Strategy/Linear.pm view on Meta::CPAN
version 0.24
=head1 SYNOPSIS
To be used as strategy in L<Action::Retry>
=head1 ATTRIBUTES
=head2 initial_sleep_time
ro, Int, defaults to 1000 ( 1 second )
The number of milliseconds to wait for the first retry
=head2 multiplicator
ro, Int, defaults to 2
Number multiplied by the last sleep time. E.g. if set to 2, the time between
two retries will double. If set to 1, it'll remain constant. Defaults to 2
=head2 max_retries_number
ro, Int|Undef, defaults to 10
The number of times we should retry before giving up. If set to undef, never
stop retrying.
=head2 max_sleep_time
ro, Int|Undef, defaults to undef
If Action::Retry is about to sleep more than this number ( in milliseconds ),
stop retrying. If set to undef, never stop retrying.
=head1 AUTHOR
Damien "dams" Krotkine
=head1 COPYRIGHT AND LICENSE
This software is copyright (c) 2013 by Damien "dams" Krotkine.
t/nonblocking.t view on Meta::CPAN
use Test::More;
use Time::HiRes qw(gettimeofday usleep);
sub now {
my ($s, $m) = gettimeofday;
my $now = $s * 1000 + int($m / 1000);
}
my $var = 0;
# cosntant 10 millisecond sleep
my $action = Action::Retry->new(
attempt_code => sub { $var++; die "plop" },
non_blocking => 1,
strategy => { Linear => { initial_sleep_time => 100,
multiplicator => 1,
} },
);
$action->run();
$action->run();
( run in 1.215 second using v1.01-cache-2.11-cpan-39bf76dae61 )