Timeout :: Error Does Not Save Ruby

I'm still new to Ruby, and the first time I'm trying to use Timeout for some HTTP functions, but obviously I'm missing a mark somewhere. My code is below, but it does not work. Instead, the following exception occurs:

C:/Ruby193/lib/ruby/1.9.1/net/http.rb:762:in `initialize': execution expired (Timeout::Error) 

Which does not make much sense to me, since the part of the code that he chooses is wrapped in a begin / rescue / end block and, in particular, will save Timeout :: Error. Am I doing something wrong or something that is not supported in Ruby?

  retries = 10 Timeout::timeout(5) do begin File.open("#{$temp}\\http.log", 'w') { |f| http.request(request) do |str| f.write str.body end } rescue Timeout::Error if retries > 0 print "Timeout - Retrying..." retries -= 1 retry else puts "ERROR: Not responding after 10 retries! Giving up!") exit end end end 
+4
source share
2 answers

The Timeout::Error message appears in the Timeout::timeout call, so you need to put it in the begin block:

 retries = 10 begin Timeout::timeout(5) do File.open("#{$temp}\\http.log", 'w') do |f| http.request(request) do |str| f.write str.body end end end rescue Timeout::Error if retries > 0 print "Timeout - Retrying..." retries -= 1 retry else puts "ERROR: Not responding after 10 retries! Giving up!") exit end end 
+20
source

Use retry to make this simple

https://github.com/nfedyashev/retryable#readme

 require "open-uri" retryable(:tries => 3, :on => OpenURI::HTTPError) do xml = open("http://example.com/test.xml").read end 
+2
source

All Articles