The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Retry

SYNOPSIS

A one-feature module, this provides a method to wrap any function in automatic retry logic, with exponential back-off delays, and a callback for each time an attempt fails.

Example:

  my $agent = Retry->new(
    failure_callback => sub { warn "oh dear, error: " . $_[0]; },
  );
  eval {
    $agent->retry(
      sub {
        this_code_might_die();
      }
    );
  };
  if ($@) {
    die "We totally failed!";
    # Note that if we succeeded on a retry, this won't get called.
  }

ATTRIBUTES

retry_delay

This is the initial delay used when the routine failed, before retrying again.

Every subsequent failure doubles the amount.

It defaults to 8 seconds.

max_retry_attempts

The maximum number of retries we should attempt before giving up completely.

It defaults to 5.

failure_callback

Optional. To be notified of *every* failure (even if we eventually succeed on a later retry), install a subroutine callback here.

For example:

  Retry->new(
      failure_callback => sub { warn "failed $count++ times" }
  );

METHODS

retry

Its purpose is to execute the passed subroutine, over and over, until it succeeds, or the number of retries is exceeded. The delay between retries increases exponentially.

AUTHOR

Toby Corkindale, mailto:tjc@cpan.org

LICENSE

This module is released under the Perl Artistic License.

It is based upon source code which is Copyright 2010 Strategic Data Pty Ltd, however it is used and released with permission.

SEE ALSO

Attempt

Retry differs from Attempt in having exponentially increasing delays, and by having a callback inbetween attempts.

However Attempt has a simpler syntax.