This post originated from an RSS feed registered with Ruby Buzz
by Daniel Berger.
Original Post: Attempt *this*
Feed Title: Testing 1,2,3...
Feed URL: http://djberg96.livejournal.com/data/rss
Feed Description: A blog on Ruby and other stuff.
I'm porting over an old Perl script to Ruby that involves ftp'ing some files around, and I found myself using this old idiom again:
max = 3
begin
Timeout(30){
FTP.open(host, user, password){ ... }
}
rescue
max -= 1
if max > 0
sleep 30
retry
end
raise
end
This gets old, quick. Plus, if you make a simple mistake somewhere in the begin block it's going to fail, but you won't know why until over a minute later. Then you have to start adding warnings and logging. Blech.
Then I remembered one of my favorite Perl modules called Attempt by Mark Fowler and decided to steal that for Ruby. I put out a call on ruby-talk to see what people thought. Sure enough a few folks were kind enough to reply, and they provided me with a couple useful tips.
I've released my own version tonight, and I plan to put it to good use immediately. So, that entire block above can now be replaced with this:
It also kicks out warnings for what would have been errors to save you the headache of dumb mistakes that might cause inadvertant failures. That's the simple version.
For more advanced features I decided to create an actual Attempt class because there were just too many options I wanted to be able to deal with. These options were based mostly on the feedback I received, such as the ability to automatically increment the sleep interval between retries.
This release doesn't have the "absolute max" option yet, though, but I hope to have it in the next release. This will deal with the nightmare scenario that Jim Weirich brought up, where you wrap code that, in turn, is doing its own little begin/rescue/retry dance, ad nauseum, such that code you thought would time out in seconds is, instead, taking several minutes. Ah, the wonders of Ruby's threads and synchronization mechanisms. :)
On an unrelated note, I took a look at this Perl module, and was once again reminded why Ruby is just so, so much better than Perl.