On Thursday 28 July 2005 09:51 pm, Daniel Berger wrote:
> Hi all,
>
> What would folks think of allowing retry to take two arguments - a
> num_retries and delay argument.  Then you could do something like this:
>
> begin
>    connect_to_something
> rescue Exception
>    # Retry up to 3 times, sleeping 30 seconds between attempts
>    retry(3, 30)
>    raise
> end
>
> This would mostly be a convenience for db/network code, though I'm not
> sure what other clever uses one might come up for it.
>
> Good idea?  Fraught with danger?  Too difficult to implement?  Too
> specialized?
>
> Inspired by Mark Fowler's Attempt module -
> http://search.cpan.org/dist/Attempt/lib/Attempt.pm

How about:

  def attempt(trys, wait_time=0)
    yield
  rescue
    trys -= 1
    if trys > 0
      sleep(wait_time) if wait_time > 0
      retry
    end
  end

  attempt(3, 30) do
    connect_to_something
  end

-- 
-- Jim Weirich    jim / weirichhouse.org     http://onestepback.org
-----------------------------------------------------------------
"Beware of bugs in the above code; I have only proved it correct, 
not tried it." -- Donald Knuth (in a memo to Peter van Emde Boas)