Retry
libraryRuns a Lua function again when it fails, for steps that can fail temporarily (an API that is not ready yet, a file that has not arrived). Combine it with `Assert` to retry until a condition holds. For browser steps use the retry behaviour of the browser libraries instead.
- Kind Library, a global in every script
- Category Utilities
- Functions 1
Functions
-
runany Calls the function up to `attempts` times until it finishes without an error.
Retry.run(attempts: number, function: function)→anyRetry.run(attempts: number, delayMs: number, function: function)→anyRetry.run(attempts: number, delayMs: number, maxDurationMs: number, function: function)→any Calls the function up to `attempts` times until it finishes without an error. Any error raised by the function (including a failed `Assert`) counts as a failed attempt. Optionally waits `delayMs` between attempts and stops retrying once `maxDurationMs` has passed since the first attempt; the limit is only checked between attempts, so a slow attempt can overrun it. When the last attempt also fails, the error of that last attempt is thrown unchanged. Stopping the run aborts immediately without further attempts. The function is called without arguments. Use exactly 2, 3 or 4 arguments, with the function always last: `(attempts, fn)`, `(attempts, delayMs, fn)` or `(attempts, delayMs, maxDurationMs, fn)`. Invalid values (attempts below 1, a negative delay, a max duration below 1, a last argument that is not a function) throw before anything runs.
Parameters
-
attemptsnumber - Maximum number of attempts, including the first run (at least 1)
-
delayMsnumber - Wait between attempts in milliseconds (0 or more)
-
maxDurationMsnumber - Stop retrying after this many milliseconds in total (at least 1)
-
functionfunction - The function to run; called without arguments
Returns
Example runs in CI
local attempts = 0local value = Retry.run(3, 10, function() attempts = attempts + 1 Assert.that(attempts >= 2, "not ready yet") return "done after " .. attemptsend)print(value)