Title: | Repeated Evaluation |
---|---|
Description: | Provide simple mechanism to repeatedly evaluate an expression until either it succeeds or timeout exceeded. It is useful in situations that random failures could happen. |
Authors: | Randy Lai [aut, cre] |
Maintainer: | Randy Lai <[email protected]> |
License: | MIT + file LICENSE |
Version: | 0.1.1 |
Built: | 2024-10-31 20:36:50 UTC |
Source: | https://github.com/randy3k/retry |
Provide simple mechanism to repeatedly evaluate an expression until either it succeeds or timeout exceeded. It is useful in situations that random failures could happen.
Maintainer: Randy Lai [email protected]
Useful links:
Repeatedly evaluate an expression until a condition is met or timeout is exceeded.
retry( expr, upon = "error", when = NULL, until = NULL, envir = parent.frame(), silent = FALSE, timeout = Inf, max_tries = Inf, interval = 0.1, later_run_now = FALSE )
retry( expr, upon = "error", when = NULL, until = NULL, envir = parent.frame(), silent = FALSE, timeout = Inf, max_tries = Inf, interval = 0.1, later_run_now = FALSE )
expr |
an expression to be evaluated, supports quasiquotation. |
upon |
a vector of condition classes. The expression will be evaluated again after
the delay if a condition is thrown. See the |
when |
regular expression pattern that matches the message of the condition. It is used to
decide if we need to evaluate |
until |
a function of two arguments. This function is used to check if we need to
evaluate |
envir |
the environment in which the expression is to be evaluated. |
silent |
suppress messages and warnings |
timeout |
raise an error if this amount of time in seconds has passed. |
max_tries |
maximum number of attempts |
interval |
delay between retries. |
later_run_now |
execute |
retry(10, until = ~TRUE) # returns immediately f <- function(x) { if (runif(1) < 0.9) { stop("random error") } x + 1 } # keep retring when there is a random error retry(f(1), when = "random error") # keep retring until a condition is met retry(f(1), until = function(val, cnd) val == 2) # or using one sided formula retry(f(1), until = ~ . == 2) try({ # it doesn't capture the error of "a" + 1 retry(f("a"), when = "random error") }) try({ # an error is raised after 1 second retry(stop("foo"), when = "foo", timeout = 1) }) try({ # timeout also works for indefinite R code retry(while(TRUE) {}, until = ~FALSE, timeout = 1) })
retry(10, until = ~TRUE) # returns immediately f <- function(x) { if (runif(1) < 0.9) { stop("random error") } x + 1 } # keep retring when there is a random error retry(f(1), when = "random error") # keep retring until a condition is met retry(f(1), until = function(val, cnd) val == 2) # or using one sided formula retry(f(1), until = ~ . == 2) try({ # it doesn't capture the error of "a" + 1 retry(f("a"), when = "random error") }) try({ # an error is raised after 1 second retry(stop("foo"), when = "foo", timeout = 1) }) try({ # timeout also works for indefinite R code retry(while(TRUE) {}, until = ~FALSE, timeout = 1) })
Block the current runtime until the expression returns TRUE
.
wait_until( expr, envir = parent.frame(), timeout = Inf, interval = 0.1, later_run_now = TRUE )
wait_until( expr, envir = parent.frame(), timeout = Inf, interval = 0.1, later_run_now = TRUE )
expr |
an expression to check, supports quasiquotation. |
envir |
the environment in which the expression is to be evaluated. |
timeout |
raise an error if this amount of time in second has passed. |
interval |
delay between retries. |
later_run_now |
execute |
s <- Sys.time() system.time(wait_until(Sys.time() - s > 1)) z <- 0 later::later(function() z <<- 1, 1) wait_until(z == 1) z == 1
s <- Sys.time() system.time(wait_until(Sys.time() - s > 1)) z <- 0 later::later(function() z <<- 1, 1) wait_until(z == 1) z == 1