The Artima Developer Community
Sponsored Link

Ruby Buzz Forum
Attempt *this*

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Daniel Berger

Posts: 1383
Nickname: djberg96
Registered: Sep, 2004

Daniel Berger is a Ruby Programmer who also dabbles in C and Perl
Attempt *this* Posted: Jun 9, 2006 8:12 PM
Reply to this message Reply

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.
Latest Ruby Buzz Posts
Latest Ruby Buzz Posts by Daniel Berger
Latest Posts From Testing 1,2,3...

Advertisement
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:
attempt(3, 30){
   FTP.open(host, user, password){ ... }
}

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.

Read: Attempt *this*

Topic: Elixir Previous Topic   Next Topic Topic: Trawling for Requirements

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use