From: kate rhodes Date: 2006-06-10T00:18:56+09:00 Subject: Re: Package idea: attempt The following is incredibly nitpicky I admit but I figure I may as well mention it. The line Timeout.timeout(timeout){ yield } Is it just me or is that a lot of "timeout" Hurts a strangers understanding of the code. Why not change the name of the passed in timeout var to user_timeout or anything else that isn't just 'timeout' - kate = masukom On 6/8/06, Daniel Berger wrote: > Hi all, > > I'm tired of this idiom: > > max = 3 > begin > Timeout.timeout(val){ > # some op that could fail or timeout on occasion > } > rescue Exception > max -= 1 > if max > 0 > sleep interval > retry > end > raise > end > > Mark Fowler wrote a Perl module called "attempt" > (http://search.cpan.org/~markf/Attempt-1.01/lib/Attempt.pm) that I think > is pretty handy, and I would like this for myself. I figure the API > should look like this: > > # 1st arg is retries, 2nd arg is interval > attempt(3, 300){ > FTP.open(host, user, passwd){ ... } > } > > Here's my possibly naive implementation: > > require 'timeout' > > module Kernel > def attempt(tries = 3, interval = 60, timeout = nil) > begin > if timeout > Timeout.timeout(timeout){ yield } > else > yield > end > rescue > tries -= 1 > if tries > 0 > sleep interval > retry > end > raise > end > end > end > > What do you think? Useful? Are there any gotchas I need to consider, > such as nested begin/end blocks, try/catch? Anything else? Should I > provide some way to provide debug info? Finer grained error handling? > > Ideas welcome. > > Thanks, > > Dan > >