Skip to content

Instantly share code, notes, and snippets.

@grantr
Created May 17, 2009 01:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save grantr/112881 to your computer and use it in GitHub Desktop.
Save grantr/112881 to your computer and use it in GitHub Desktop.
# 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