Last active
October 21, 2015 21:08
-
-
Save jtrim/2f646ee98ca9e67977ef to your computer and use it in GitHub Desktop.
A (mostly) referentially-transparent retry method in Ruby that doesn't mutate local state.
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 with_retry(timeout=0.25, tries=3) | |
exception = \ | |
tries.times.reduce(nil) do | |
begin | |
result = yield | |
rescue StandardError => ex | |
sleep timeout | |
ex | |
else | |
return result | |
end | |
end | |
raise exception | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment