How to resume execution of a FOR loop after timeout error

S

Stephen Lead

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
 
M

Marvin Gülker

Stephen said:
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
 
S

Stephen Lead

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
 
A

Aldric Giacomoni

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,050
Latest member
AngelS122

Latest Threads

Top