Created
November 11, 2014 19:18
-
-
Save technicalpickles/a4581caa9378549dd59d to your computer and use it in GitHub Desktop.
ruby retry example-
This file contains 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
def flaky_code() | |
unless @flaky_code_works | |
@flaky_code_works = true | |
raise 'wtf' | |
end | |
puts "works eventually lol" | |
end | |
max_attempts = 5 | |
attempt = 1 | |
begin | |
flaky_code() | |
rescue => e | |
if attempt <= max_attempts | |
sleep 1 * attempt | |
attempt += 1 | |
puts "rety attempt #{attempt}" | |
retry | |
else | |
raise e | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment