Created
May 17, 2009 01:27
-
-
Save grantr/112881 to your computer and use it in GitHub Desktop.
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
# Runs a block multiple times, each time with a different alternative | |
# Intended for connection failover | |
# Inspired by http://github.com/carlo/retryable | |
module RetryWith | |
def retry_with(alternatives, options={}, &block) | |
opts = {:tries => alternatives.size, :on => Exception}.merge(options) | |
return if opts[:tries] == 0 | |
retry_exceptions, tries = [opts[:on]].flatten, 0 | |
begin | |
return yield(alternatives[tries]) | |
rescue *retry_exceptions | |
retry if (tries += 1) < opts[:tries] | |
raise $! | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment