Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Utils
- ##
- # utility method that can be used to retry a block of code for a number of times
- # with waiting time in between each trial
- #
- # limit: number of trials that will be executed
- # exception_types: exception types that will be rescued during a trial
- # wait_for: number of seconds to wait between trials, use 0 or -1 to disable waiting
- # block: block of code to be executed during a trial
- def self.with_retries(limit: 3, exception_types: [StandardError], wait_for: 1, &block)
- trials = 0
- begin
- trials += 1
- return block.call
- rescue *exception_types => e
- raise e if trials == limit
- sleep(wait_for) if wait_for.to_i.positive?
- retry
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement