How to resume execution of a FOR loop after timeout error

Hi everyone,

I’m new to Ruby but familiar with some other programming and scripting
languages. I like what I see so far.

My question is how to resume execution of a FOR loop, after a certain
time has elapsed. Some pseudo-code appears below.

I’m testing the performance of a website using WATIR, by running the
same test script multiple times (“for i in 1…1000”) and logging the
results. This works fine, except in cases when the entire site freezes
(it’s not a very robust site, hence the testing).

I’d like to be able to run the test overnight, and have Ruby
automatically cancel the current iteration of the FOR loop after (say)
10 minutes.

Currently, the script will wait for the browser’s error dialog to be
dismissed if the site crashes during the test. I’d like to close the
browser and open a new instance in this case.

Thanks,
Steve

require ‘timeout’
for i in 1…1000
status = Timeout::timeout(60) {
b = Firefox.new
b.goto “some site”
#1. perform some processing, which should take less than 60 seconds
#2. if the process was successful, write the result to a text file
#3. if it took too long, abort this iteration, and continue with
“next i”
b.close
}
end

Stephen Lead wrote:

require ‘timeout’
for i in 1…1000
status = Timeout::timeout(60) {
b = Firefox.new
b.goto “some site”
#1. perform some processing, which should take less than 60 seconds
#2. if the process was successful, write the result to a text file
#3. if it took too long, abort this iteration, and continue with
“next i”
b.close
}
end

Hi,

You have to rescue the Timeout::Error.

require “timeout”
for i in 1…1000
begin
status = Timeout.timeout(60) do
#Do whatever you want…
end
rescue Timeout::Error
#Continue with whatever you want…
end
end

Marvin

Hi Marvin,

Thanks for the reply.

You have to rescue the Timeout::Error

That’s the issue for me - how do I cause the rescue block to exit
without failing, and continue with the FOR I loop?

Thanks
Steve


require “timeout”
for i in 1…1000
begin
status = Timeout.timeout(60) do
#Do whatever you want…
end
rescue Timeout::Error
#Continue with whatever you want…
end
end

Marvin

Stephen Lead wrote:
Like Marvin wrote…

require ‘timeout’
for i in 1…1000
begin
status = Timeout::timeout(60) {
b = Firefox.new
b.goto “some site”
#1. perform some processing, which should take less than 60
seconds
#2. if the process was successful, write the result to a text file
rescue Timeout::Error
#3. if it took too long, abort this iteration, and continue with
‘next i’
# here you don’t have to write anything if you don’t want to.
end
b.close
}
end

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs