[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

A code snippet: Controlled retry

Hal E. Fulton

11/24/2003 6:12:00 AM

I sometimes find myself retrying operations when in a networked
situation (e.g., maybe server isn't up, just try again).

I just wrote this little bit of code.

Tell me what you think... I'm sure it can be improved.

Cheers,
Hal


def try(quiet=true,times=5,secs=1,&block)
count = 0
catch :finished do
begin
block.call
rescue => err
puts "Error was: #{err}" if not quiet
count += 1
throw :finished if count > times
sleep secs
retry
end
end
end




17 Answers

Gennady

11/24/2003 7:03:00 AM

0


On Nov 23, 2003, at 22:11, Hal Fulton wrote:

> def try(quiet=true,times=5,secs=1,&block)
> count = 0
> catch :finished do
> begin
> block.call
> rescue => err
> puts "Error was: #{err}" if not quiet
> count += 1
> throw :finished if count > times
> sleep secs
> retry
> end
> end
> end


def try(quiet=true,times=5,secs=1,&block)
return unless block
times.times do
begin
return block.call
rescue => err
puts "Error was: #{err}" unless quiet
sleep secs
end
end
end


Sincerely,
Gennady Bystritsky




Gennady Bystritsky

11/24/2003 7:25:00 AM

0

On Nov 23, 2003, at 23:02, Gennady wrote:

>
> On Nov 23, 2003, at 22:11, Hal Fulton wrote:
>
>> def try(quiet=true,times=5,secs=1,&block)
>> count = 0
>> catch :finished do
>> begin
>> block.call
>> rescue => err
>> puts "Error was: #{err}" if not quiet
>> count += 1
>> throw :finished if count > times
>> sleep secs
>> retry
>> end
>> end
>> end
>
>
> def try(quiet=true,times=5,secs=1,&block)
> return unless block
> times.times do
> begin
> return block.call
> rescue => err
> puts "Error was: #{err}" unless quiet
> sleep secs
> end
> end
> end
>

Oops, times.times should become times.next.times to function like the
original. Also, when all attempts are exhausted, the last exception
must be re-raised or another more appropriate one raised (boolean
return would also do the trick).



Robert Klemme

11/24/2003 8:50:00 AM

0


"Gennady" <bystr@mac.com> schrieb im Newsbeitrag
news:4BB0276B-1E4F-11D8-A73D-0003939AEA24@mac.com...
> On Nov 23, 2003, at 23:02, Gennady wrote:
>
> >
> > On Nov 23, 2003, at 22:11, Hal Fulton wrote:
> >
> >> def try(quiet=true,times=5,secs=1,&block)
> >> count = 0
> >> catch :finished do
> >> begin
> >> block.call
> >> rescue => err
> >> puts "Error was: #{err}" if not quiet
> >> count += 1
> >> throw :finished if count > times
> >> sleep secs
> >> retry
> >> end
> >> end
> >> end
> >
> >
> > def try(quiet=true,times=5,secs=1,&block)
> > return unless block
> > times.times do
> > begin
> > return block.call
> > rescue => err
> > puts "Error was: #{err}" unless quiet
> > sleep secs
> > end
> > end
> > end
> >
>
> Oops, times.times should become times.next.times to function like the
> original. Also, when all attempts are exhausted, the last exception
> must be re-raised or another more appropriate one raised (boolean
> return would also do the trick).

Combining the two of them (i.e. times.next and re-raising) by simply
adding a line,
plus adding a check to allow for non-sleeping retries,
plus output parameter that can be an IO or an Array,
plus changed the first return to an exception in order to let the user
know he forgot something:

def try(times=0, secs=nil, log=nil, &block)
raise LocalJumpError, "no block given" unless block

times.times do
begin
return block.call
rescue => err
log << "Error was: #{err}\n" if log
sleep secs if secs
end
end

block.call
end

Cheers

robert

Pete B

9/18/2008 1:20:00 PM

0

In article <Xns9B1C1A6DC8F38scratchmonkey@216.196.97.136>,
ScratchMonkey.blacklist@sewingwitch.com says...
> Urbin <urbin@invalid.invalid> wrote in
> news:slrngd1dd5.7j4.urbin@stinky.trash.net:
>
> > Didn't know it was preloading until I read it here (as I don't use the
> > launcher). After hearing about it I started the background downloader
> > which told me "no new data to download". Started the launcher, it
> > updated my background downloader and created a torrent file. Used
> > Azzurreus and got the whole 821megs in about 1.5 hours. I'm talking EU
> > here, done it on sunday night.
>
> Once I hear about a patch, I go here to get the torrent:
>
> http://www.wowwiki.c...

I would never trust mirrors.



ScratchMonkey

9/18/2008 9:44:00 PM

0

Pete B <xxxh@_xsomeething.com> wrote in
news:MPG.233c73fd201fb4dd98c54b@news.usenetserver.com:

>> Once I hear about a patch, I go here to get the torrent:
>>
>> http://www.wowwiki.c...
>
> I would never trust mirrors.

A mirror is no less safe than the Blizzard downloader, which uses
Bittorrent. Using their downloader, you are effectively downloading from
hundreds of mirrors.

Your concern is actually addressed by the Bittorrent protocol.

http://en.wikipedia.org/wiki/B...(protocol)

Torrents checksum fragments of the file. It's like each 256kB block has its
own SHA1 checksum.

After downloading a patch from a mirror, run the Blizzard downloader. It
will perform this checksum on all the pieces to make sure you got good data
before it attempts to start sharing it to the cloud. If the downloader
detects any bad data, it will re-download those blocks from the cloud.

ScratchMonkey

9/19/2008 7:39:00 AM

0

Other game vendors are starting to use Bittorrent to distribute:

http://torrentfreak.com/bittorrent-to-speed-up-game-distributi...

pv+usenet

9/19/2008 3:06:00 PM

0

ScratchMonkey@sewingwitch.com writes:
>Other game vendors are starting to use Bittorrent to distribute:
>
>http://torrentfreak.com/bittorrent-to-speed-up-game-distributi...

Not really a surprise. Think about what Blizzard is doing here - sending
out a 1 gigabyte download to 11 million people. That's 11 petabytes of data
they need to push out the pipe! No conventional distribution is going to
cope with that. *
--
* PV something like badgers--something like lizards--and something
like corkscrews.

Brent Stroh

9/19/2008 3:44:00 PM

0

pv+usenet@pobox.com (PV) wrote:

>Not really a surprise. Think about what Blizzard is doing here - sending
>out a 1 gigabyte download to 11 million people. That's 11 petabytes of data
>they need to push out the pipe! No conventional distribution is going to
>cope with that. *

It's also a good way to keep bandwidth prices down by leveraging customer's
connections.

-Brent

Pete B

9/20/2008 10:01:00 PM

0

In article <Xns9B1D95DFF6A28scratchmonkey@216.196.97.136>,
ScratchMonkey.blacklist@sewingwitch.com says...
> Pete B <xxxh@_xsomeething.com> wrote in
> news:MPG.233c73fd201fb4dd98c54b@news.usenetserver.com:
>
> >> Once I hear about a patch, I go here to get the torrent:
> >>
> >> http://www.wowwiki.c...
> >
> > I would never trust mirrors.
>
> A mirror is no less safe than the Blizzard downloader,

I disagree.

> which uses
> Bittorrent. Using their downloader, you are effectively downloading from
> hundreds of mirrors.

Tiny slices - you'd have to be the master to be able to replace them
with something which not only matched the hashes but also created a new
mischievous whole.


> After downloading a patch from a mirror, run the Blizzard downloader. It
> will perform this checksum on all the pieces to make sure you got good data
> before it attempts to start sharing

Ah yes, good point.

> ... it to the cloud.

hehe the cloud.

> If the downloader detects any bad data, it will re-download
>those blocks from the cloud.

hehe... the cloud *g*

Pete B

9/20/2008 10:02:00 PM

0

In article <Xns9B1E6AF02705scratchmonkey@216.196.97.136>,
ScratchMonkey.blacklist@sewingwitch.com says...
> Other game vendors are starting to use Bittorrent to distribute:
>
> http://torrentfreak.com/bittorrent-to-speed-up-game-distributi...

Good, more ammo to send to politicians when they say that bittorent is
evil.