Project

rescue_me

0.02
No commit activity in last 3 years
No release in over 3 years
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.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

 Project Readme

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 © 2010 Arild Shirazi. All rights reserved.

See LICENSE for details.