-1

I expected that timeout will happen every 30 seconds in the following code:

def action_with_timeout(threshould, &block)
  begin
    Timeout::timeout(threshould) {block.call}
  rescue Exception => e
    ap("Timeout #{threshould}, stop loading pag, #{Time.now}")
    stop_loading_page
  end
end
while true
  action_with_timeout(30) {
    ap("to INDEX")
    browse_to(CONFIG["ROOT_URL"])
  }
end    

However, it only works for the first time. The remaining will timeout in a short time. The output is:

"Timeout 30, stop loading pag, 2015-07-01 19:59:10 +0800"
"Timeout 30, stop loading pag, 2015-07-01 19:59:13 +0800"
"Timeout 30, stop loading pag, 2015-07-01 19:59:16 +0800"
"Timeout 30, stop loading pag, 2015-07-01 19:59:19 +0800"

How can I fix it?

sawa
  • 165,429
  • 45
  • 277
  • 381
newBike
  • 14,385
  • 29
  • 109
  • 192

1 Answers1

1

rescue Exception will rescue any exception (it's a bad idea to do so).

Be specific in order to rescue the exception you're interested in:

require 'timeout'

def action_with_timeout(sec, &block)
  Timeout::timeout(sec, &block)
rescue Timeout::Error
  # ...
end

Other exceptions will not be rescued. This should reveal your actual problem.

Community
  • 1
  • 1
Stefan
  • 109,145
  • 14
  • 143
  • 218