Skip to contents

try_again() evaluates x up to times times, and stops at the first attempt that raises no error. After each failed attempt it waits a number of seconds drawn from a uniform distribution on [delay_seconds_min, delay_seconds_max]. The delay does not grow with the attempt number, so this is not exponential backoff.

Usage

try_again(
  x,
  times = 2,
  delay_seconds_min = 5,
  delay_seconds_max = 10,
  verbose = FALSE
)

Arguments

x

The code to run. try_again() evaluates it once for each attempt.

times

The maximum number of attempts. The default is 2.

delay_seconds_min

The shortest delay in seconds. The default is 5.

delay_seconds_max

The longest delay in seconds. The default is 10.

verbose

Logical. TRUE reports each failed attempt as a warning, and a success after a retry as a message. The default is FALSE.

Value

TRUE, invisibly, after the first attempt that succeeds. When every attempt fails, try_again() signals the error of the last attempt.

Details

try_again() is adapted from try_again() in testthat. It adds the delay and the verbose argument.

See also

vignette("plnr") for the concepts. try_again() itself is not part of the Plan workflow.

Other utilities: create_rmarkdown()

Examples

try_again(1 + 1)

# Fail once, then succeed. The delays are 0 so that the example runs fast.
attempt_n <- 0
try_again(
  {
    attempt_n <- attempt_n + 1
    if (attempt_n < 2) stop("not ready yet")
  },
  times = 3,
  delay_seconds_min = 0,
  delay_seconds_max = 0
)
attempt_n
#> [1] 2