Skip to main content

Retry a command with exponential backoff and jitter.

Project description

recur

recur is a command-line tool that runs a single command repeatedly until it succeeds or allowed attempts run out. It implements optional exponential backoff with configurable jitter. It lets you define the success condition.

Requirements

Python 3.8 or later, PyPI package simpleeval (installed automatically with recur-command).

Installation

The recommended way to install recur is from PyPI with pipx.

pipx install recur-command
# or
pip install --user recur-command

Usage

usage: recur [-h] [-V] [-b BACKOFF] [-c COND] [-d DELAY] [-j JITTER] [-m MAX]
             [-t TRIES] [-v]
             command ...

Retry a command with exponential backoff and jitter.

positional arguments:
  command               command to run
  args                  arguments

options:
  -h, --help            show this help message and exit
  -V, --version         show program's version number and exit
  -b BACKOFF, --backoff BACKOFF
                        multiplier applied to delay on every attempt (default:
                        1, no backoff)
  -c COND, --condition COND
                        success condition (simpleeval expression, default:
                        "code == 0")
  -d DELAY, --delay DELAY
                        constant or initial exponential delay (seconds,
                        default: 0)
  -j JITTER, --jitter JITTER
                        additional random delay (maximum seconds or "min,max"
                        seconds, default: "0,0")
  -m MAX, --max-delay MAX
                        maximum delay (seconds, default: 86400)
  -t TRIES, --tries TRIES
                        maximum number of attempts (negative for infinite,
                        default: 3)
  -v, --verbose         announce exit codes

recur exits with the last command's exit code, unless the user overrides this in the condition.

The CLI options are modeled after the parameters of the retry decorator, which Python programmers may recognize. However, recur does not use retry. The jitter (random delay) behavior is different. Jitter is applied starting with the first retry, not the second. I think this is what the user expects. A single-number jitter argument results in random jitter.

Conditions

recur supports a limited form of scripting. It allows you to set the success condition using the simpleeval expression language, which is a subset of Python. The default condition is code == 0. It means recur will stop retrying when the exit code of the command is zero.

You can use the following variables in the condition expression:

  • attempt: int — the number of the current attempt, starting at one. Combine with --tries -1 to use instead of the built-in attempt counter.
  • code: int — the exit code of the last command.
  • time: float — the time the most recent attempt took, in seconds.
  • total_time: float — the time between the start of the first attempt and the end of the most recent, again in seconds.
  • max_tries: int — the value of the option --tries.

recur defines one custom function:

  • exit(code: int) -> None — exit with the exit code.

This function allows you to override the default behavior of returning the last command's exit code. For example, you can make recur exit with success when the command fails.

recur --condition 'code != 0 and exit(0)' sh -c 'exit 1'
# or
recur --condition 'exit(0) if code != 0 else False' sh -c 'exit 1'

In the following example we stop early and do not retry when the command's exit code indicates incorrect usage.

recur --condition 'code == 0 or (code in {1, 2, 3, 4} and exit(code))' curl "$url"

License

MIT.

Alternatives

recur was inspired by retry-cli. I wanted something like retry-cli, but without the Node.js dependency. There are other tools like this.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

recur_command-0.2.0.tar.gz (6.6 kB view details)

Uploaded Source

Built Distribution

recur_command-0.2.0-py3-none-any.whl (6.4 kB view details)

Uploaded Python 3

File details

Details for the file recur_command-0.2.0.tar.gz.

File metadata

  • Download URL: recur_command-0.2.0.tar.gz
  • Upload date:
  • Size: 6.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.5.1 PyPy/7.3.12 Linux/6.2.0-26-generic

File hashes

Hashes for recur_command-0.2.0.tar.gz
Algorithm Hash digest
SHA256 c193a8a9288e4c5a9a6c7abaf02e65a7c7837cb893e77016c6a3fc5b506e7535
MD5 8ecf9e1b6fb3949e57f91ac55279f3ba
BLAKE2b-256 06b6fec4b75718ab3a0feb8ec31c03b507933526e8ba46b087cc3af2928cd31e

See more details on using hashes here.

File details

Details for the file recur_command-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: recur_command-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 6.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.5.1 PyPy/7.3.12 Linux/6.2.0-26-generic

File hashes

Hashes for recur_command-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c4b9c0a8d5792857107c32b886d2207c6fb7f3b518fdf44ce4deb625287e44b6
MD5 7c56e56a6695c8b9a31ce7a42bb5a5f4
BLAKE2b-256 66412df1b373a4e17893d5198232ece8ea83fbc9b93f8ec9eeb29a7310484ef5

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page