Created
February 28, 2024 11:45
-
-
Save eddbot/c5a728464f3c1c103fe80ad8f615c73d to your computer and use it in GitHub Desktop.
Basic Retry Mechanism using redo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# frozen_string_literal: true | |
backoff = 0.2 | |
retries = 0 | |
MAX_RETRIES = 5 | |
flaky = lambda do | |
flaky_call = rand(10) | |
return if flaky_call == 1 | |
puts "#flaky failed, retrying in #{backoff.round(2)} seconds.." | |
sleep backoff | |
backoff += (backoff * 1.5) | |
retries += 1 | |
# restart the block, wooow :) | |
redo if retries < MAX_RETRIES | |
puts '#flaky error, aborting' | |
end | |
flaky.call |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment