Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Runs a function. If the function throws an exception, sleep and try the function again.
- * @param retries The number of times to attempt to call the function
- * @param retryDelayMS The delay (in milliseconds) between attempts to execute the function
- * @param retryLogFn A function that is called when the function fails and will be tried again. Intended to
- * be used to print a meaningful message to the log. Takes as an argument the number of retries
- * remaining.
- * @param f The function to be executed
- * @tparam T The return type of the function to be executed
- * @return The return value of f()
- */
- @tailrec
- def runNowWithRetries[T](retries: Int, retryDelayMS: Long, retryLogFn: (Int) => Unit)(f: () => T,retryable:(Throwable) => Boolean = (t:Throwable) => true): T = {
- (try {
- Some(f())
- } catch {
- case t: Throwable if retryable(t) =>
- if (retries <= 0)
- throw t
- else None
- }) match {
- case Some(x) => x
- case None =>
- retryLogFn(retries)
- delayAndReturn(retryDelayMS)(() => {})
- runNowWithRetries(retries - 1, retryDelayMS, retryLogFn)(f)
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement