Created
March 5, 2009 05:39
-
-
Save wtnabe/74214 to your computer and use it in GitHub Desktop.
A retrier for poor quality network and servers
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
# | |
# always retry | |
# | |
# ex) | |
# do_with_retry( 'dns' ) { Resolv.getaddress( 'example.com' ) } | |
# | |
def do_with_retry( name, &block ) | |
times = RETRY_LIMIT | |
begin | |
block.call | |
rescue => e | |
if ( times > 0 ) | |
times -= 1 | |
sleep( rand( 3 ) ) | |
@logger.info( "retry #{name}" ) if ( @logger ) | |
retry | |
else | |
raise e | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment