NAME

retry - Retry a command with custom backoff algorithm

VERSION

This document describes version 0.005 of retry (from Perl distribution App-AlgorithmBackoffUtils), released on 2020-10-20.

SYNOPSIS

Usage:

% retry [-D] [-a=s] [--algorithm=s] [--consider-actual-delay] [--debug]
    [--delay-increment-on-failure=s] [--delay-increment-on-success=s]
    [--delay-multiple-on-failure=s] [--delay-multiple-on-success=s]
    [--delay-on-success=s] [--delay=s] [--dry-run] [--exponent-base=s]
    [--format=name] [--initial-delay1=s] [--initial-delay2=s]
    [--initial-delay=s] [--jitter-factor=s] [--json] [--log-level=level]
    [--max-actual-duration=s] [--max-attempts=s] [--max-delay=s]
    [--min-delay=s] [--(no)naked-res] [--no-consider-actual-delay]
    [--noconsider-actual-delay] [--page-result[=program]] [--quiet]
    [--retry-on=s] [--skip-delay] [--success-on=s] [--trace] [--verbose]
    <command> ...

OPTIONS

* marks required options.

Main options

--algorithm=s*, -a

Backoff algorithm.

Valid values:

["Constant","Exponential","Fibonacci","LILD","LIMD","MILD","MIMD"]
--command-json=s

See --command.

--command=s@*

Can be specified multiple times.

--retry-on=s

Comma-separated list of exit codes that should trigger retry.

By default, all non-zero exit codes will trigger retry.

--skip-delay, -D

Do not delay at all.

Useful for testing, along with --dry-run, when you just want to see how the retries are done (the number of retries, along with the number of seconds of delays) by seeing the log messages, without actually delaying.

--success-on=s

Comma-separated list of exit codes that mean success.

By default, only exit code 0 means success.

Common to all algorithms options

--consider-actual-delay

Whether to consider actual delay.

If set to true, will take into account the actual delay (timestamp difference). For example, when using the Constant strategy of delay=2, you log failure() again right after the previous failure() (i.e. specify the same timestamp). failure() will then return ~2+2 = 4 seconds. On the other hand, if you waited 2 seconds before calling failure() again (i.e. specify the timestamp that is 2 seconds larger than the previous timestamp), failure() will return 2 seconds. And if you waited 4 seconds or more, failure() will return 0.

--jitter-factor=s

How much to add randomness.

If you set this to a value larger than 0, the actual delay will be between a random number between original_delay * (1-jitter_factor) and original_delay * (1+jitter_factor). Jitters are usually added to avoid so-called "thundering herd" problem.

The jitter will be applied to delay on failure as well as on success.

--max-actual-duration=s

Maximum number of seconds for all of the attempts (0 means unlimited).

Default value:

0

If set to a positive number, will limit the number of seconds for all of the attempts. This setting is used to limit the amount of time you are willing to spend on a task. For example, when using the Exponential strategy of initial_delay=3 and max_attempts=10, the delays will be 3, 6, 12, 24, ... If failures are logged according to the suggested delays, and max_actual_duration is set to 21 seconds, then the third failure() will return -1 instead of 24 because 3+6+12 >= 21, even though max_attempts has not been exceeded.

--max-attempts=s

Maximum number consecutive failures before giving up.

Default value:

0

0 means to retry endlessly without ever giving up. 1 means to give up after a single failure (i.e. no retry attempts). 2 means to retry once after a failure. Note that after a success, the number of attempts is reset (as expected). So if max_attempts is 3, and if you fail twice then succeed, then on the next failure the algorithm will retry again for a maximum of 3 times.

--max-delay=s

Maximum delay time, in seconds.

--min-delay=s

Maximum delay time, in seconds.

Default value:

0

Constant algorithm options

--delay-on-success=s

Number of seconds to wait after a success.

Default value:

0
--delay=s

Number of seconds to wait after a failure.

Exponential algorithm options

--delay-on-success=s

Number of seconds to wait after a success.

Default value:

0
--exponent-base=s

Default value:

2
--initial-delay=s

Initial delay for the first attempt after failure, in seconds.

Fibonacci algorithm options

--delay-on-success=s

Number of seconds to wait after a success.

Default value:

0
--initial-delay1=s

Initial delay for the first attempt after failure, in seconds.

--initial-delay2=s

Initial delay for the second attempt after failure, in seconds.

Lild algorithm options

--delay-increment-on-failure=s

How much to add to previous delay, in seconds, upon failure (e.g. 5).

--delay-increment-on-success=s

How much to add to previous delay, in seconds, upon success (e.g. -5).

--initial-delay=s

Initial delay for the first attempt after failure, in seconds.

Limd algorithm options

--delay-increment-on-failure=s

How much to add to previous delay, in seconds, upon failure (e.g. 5).

--delay-multiple-on-success=s

How much to multiple previous delay, upon success (e.g. 0.5).

--initial-delay=s

Initial delay for the first attempt after failure, in seconds.

Logging options

--debug

Shortcut for --log-level=debug.

--log-level=s

Set log level.

--quiet

Shortcut for --log-level=error.

--trace

Shortcut for --log-level=trace.

--verbose

Shortcut for --log-level=info.

Mild algorithm options

--delay-increment-on-success=s

How much to add to previous delay, in seconds, upon success (e.g. -5).

--delay-multiple-on-failure=s

How much to multiple previous delay, upon failure (e.g. 1.5).

--initial-delay=s

Initial delay for the first attempt after failure, in seconds.

Mimd algorithm options

--delay-multiple-on-failure=s

How much to multiple previous delay, upon failure (e.g. 1.5).

--delay-multiple-on-success=s

How much to multiple previous delay, upon success (e.g. 0.5).

--initial-delay=s

Initial delay for the first attempt after failure, in seconds.

Output options

--format=s

Choose output format, e.g. json, text.

Default value:

undef
--json

Set output format to json.

--naked-res

When outputing as JSON, strip result envelope.

Default value:

0

By default, when outputing as JSON, the full enveloped result is returned, e.g.:

[200,"OK",[1,2,3],{"func.extra"=>4}]

The reason is so you can get the status (1st element), status message (2nd element) as well as result metadata/extra result (4th element) instead of just the result (3rd element). However, sometimes you want just the result, e.g. when you want to pipe the result for more post-processing. In this case you can use `--naked-res` so you just get:

[1,2,3]
--page-result

Filter output through a pager.

Other options

--dry-run

Run in simulation mode (also via DRY_RUN=1).

Default value:

""
--help, -h, -?

Display help message and exit.

--version, -v

Display program's version and exit.

COMPLETION

This script has shell tab completion capability with support for several shells.

bash

To activate bash completion for this script, put:

complete -C retry retry

in your bash startup (e.g. ~/.bashrc). Your next shell session will then recognize tab completion for the command. Or, you can also directly execute the line above in your shell to activate immediately.

It is recommended, however, that you install modules using cpanm-shcompgen which can activate shell completion for scripts immediately.

tcsh

To activate tcsh completion for this script, put:

complete retry 'p/*/`retry`/'

in your tcsh startup (e.g. ~/.tcshrc). Your next shell session will then recognize tab completion for the command. Or, you can also directly execute the line above in your shell to activate immediately.

It is also recommended to install shcompgen (see above).

other shells

For fish and zsh, install shcompgen as described above.

HOMEPAGE

Please visit the project's homepage at https://metacpan.org/release/App-AlgorithmBackoffUtils.

SOURCE

Source repository is at https://github.com/perlancar/perl-App-AlgorithmBackoffUtils.

BUGS

Please report any bugs or feature requests on the bugtracker website https://rt.cpan.org/Public/Dist/Display.html?Name=App-AlgorithmBackoffUtils

When submitting a bug or request, please include a test-file or a patch to an existing test-file that illustrates the bug or desired feature.

SEE ALSO

Algorithm::Backoff::Constant.

AUTHOR

perlancar <perlancar@cpan.org>

COPYRIGHT AND LICENSE

This software is copyright (c) 2020, 2019 by perlancar@cpan.org.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.