From: Austin Ziegler Date: 2005-07-29T22:08:14+09:00 Subject: Re: Allow retry to take arguments? On 7/28/05, Jim Weirich wrote: > On Thursday 28 July 2005 09:51 pm, Daniel Berger wrote: >> 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 One problem that I see, Jim. In Daniel's pseudo-code, he can rescue specific exceptions: begin connect_to_something rescue NetworkBusyException, TimeoutException retry(3, 30) or raise rescue NetworkUnavailableException retry(5, 60) or raise end Yours just allows for one attempt wrapper and doesn't specify what sort of exceptions can be caught. It's a neat idea, though, and I think that it could be generally useful, not just database useful. -austin -- Austin Ziegler * halostatue@gmail.com * Alternate: austin@halostatue.ca