Skip to content
Retry a block of code that might fail due to temporary errors.
Ruby
Find file
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.
lib
pkg
spec
.gitignore
.ruby-version
Gemfile
LICENSE
README.rdoc
Rakefile
rescue_me.gemspec

README.rdoc

rescue_me

Provides a convenience method to retry blocks of code that might fail due to temporary errors, e.g. a network service that becomes temporarily unavailable. The retries are timed to back-off exponentially (2^n seconds), hopefully giving time for the remote server to recover. These are the default wait times between consecutive attempts:

0, 1, 2, 4, 8, 16, 32, 64, 128, ... seconds

Usage:

rescue_and_retry(max_attempts, *temporary_exceptions) {
  # your code
}

Example - retry my code up to 7 times (over about a minute) if I see the following 2 network errors:

rescue_and_retry(7, Net::SMTPServerBusy, IOError) {
  smtp.send_message(message, from_address, to_address )
}

Log output:

WARN -- : rescue_and_retry(1/5) "SMTPServerBusy: 451 4.3.0 Mail server temporarily rejected message." in ./mailer.rb:43
WARN -- : rescue_and_retry(2/5) "SMTPServerBusy: 451 4.3.0 Mail server temporarily rejected message." in ./mailer.rb:43
WARN -- : rescue_and_retry(3/5) "SMTPServerBusy: 451 4.3.0 Mail server temporarily rejected message." in ./mailer.rb:43
# No further output or stacktrace. Block succeeded on 4th attempt.

Copyright

Copyright © 2010 Arild Shirazi. All rights reserved.

See LICENSE for details.

Something went wrong with that request. Please try again.