Skip to content
This repository

Retry a block of code that might fail due to temporary errors.

branch: master

Fetching latest commit…

Octocat-spinner-32-eaf2f5

Cannot retrieve the latest commit at this time

Octocat-spinner-32 lib
Octocat-spinner-32 pkg
Octocat-spinner-32 spec
Octocat-spinner-32 .gitignore
Octocat-spinner-32 .ruby-version
Octocat-spinner-32 Gemfile
Octocat-spinner-32 LICENSE
Octocat-spinner-32 README.rdoc
Octocat-spinner-32 Rakefile
Octocat-spinner-32 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.