Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2014
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.21 KB | None | 0 0
  1.  
  2.  
  3. /**
  4. * Runs a function. If the function throws an exception, sleep and try the function again.
  5. * @param retries The number of times to attempt to call the function
  6. * @param retryDelayMS The delay (in milliseconds) between attempts to execute the function
  7. * @param retryLogFn A function that is called when the function fails and will be tried again. Intended to
  8. * be used to print a meaningful message to the log. Takes as an argument the number of retries
  9. * remaining.
  10. * @param f The function to be executed
  11. * @tparam T The return type of the function to be executed
  12. * @return The return value of f()
  13. */
  14. @tailrec
  15. def runNowWithRetries[T](retries: Int, retryDelayMS: Long, retryLogFn: (Int) => Unit)(f: () => T,retryable:(Throwable) => Boolean = (t:Throwable) => true): T = {
  16. (try {
  17. Some(f())
  18. } catch {
  19. case t: Throwable if retryable(t) =>
  20. if (retries <= 0)
  21. throw t
  22. else None
  23. }) match {
  24. case Some(x) => x
  25. case None =>
  26. retryLogFn(retries)
  27. delayAndReturn(retryDelayMS)(() => {})
  28. runNowWithRetries(retries - 1, retryDelayMS, retryLogFn)(f)
  29. }
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement