Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.70 KB | None | 0 0
  1. class Utils
  2. ##
  3. # utility method that can be used to retry a block of code for a number of times
  4. # with waiting time in between each trial
  5. #
  6. # limit: number of trials that will be executed
  7. # exception_types: exception types that will be rescued during a trial
  8. # wait_for: number of seconds to wait between trials, use 0 or -1 to disable waiting
  9. # block: block of code to be executed during a trial
  10.  
  11. def self.with_retries(limit: 3, exception_types: [StandardError], wait_for: 1, &block)
  12. trials = 0
  13. begin
  14. trials += 1
  15. return block.call
  16. rescue *exception_types => e
  17. raise e if trials == limit
  18. sleep(wait_for) if wait_for.to_i.positive?
  19. retry
  20. end
  21. end
  22. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement