Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Trouble with timeout

10 views
Skip to first unread message

Luka Stolyarov

unread,
May 30, 2010, 6:27:48 PM5/30/10
to
Hi. I have a small script that I use to download archives from the web.
The code looks like this:

require 'rubygems'
require 'mechanize'

def downloader()
agent = WWW::Mechanize.new

# create agent object
page = agent.get('http://www.myurl.com')

#authenticate
form = page.forms.first
form.username = '####'
form.password = '####'

#submit form
page = agent.submit form

#grab all links that have zip in them
mylinks = page.links_with(:href => /zip\//)

#for each link download and rename
mylinks.each do |archive|

name = archive.href.split('zip/')[1].sub('/','').strip

puts "Archive #{name} is saving..."

myfile = agent.click(archive)

output = File.open("/Users/luka/Desktop/#{name}.zip", 'w') {|file|
file << myfile.body }

puts "Done!"

end
end

begin
downloader
rescue Timeout::Error
puts 'Timeout was detected. Trying again...'
downloader
end


However, I've been having problems with timeouts from time to time, so I
ended up making this code into a function and upon timeout invoke this
function once again, as you can see at the bottom of the code. However,
I still get an error, which looks like this on timeout:


Archive 914188 is saving...
Timeout was detected. Trying again...
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/timeout.rb:60:in
`rbuf_fill': execution expired (Timeout::Error)

So it tries to rescue the timeout but obviously calling the same
function again is a bad idea. I was wondering why is it, and what would
be a proper solution.
Thanks!

Luka
--
Posted via http://www.ruby-forum.com/.

Walle Wallen

unread,
Jun 1, 2010, 10:50:53 AM6/1/10
to
begin
downloader
rescue Timeout::Error
puts 'Timeout was detected. Trying again...'
retry
end

Luka Stolyarov

unread,
Jun 1, 2010, 12:25:15 PM6/1/10
to

I'll try it out, thanks!
0 new messages