Exit loop after checking for some time

C

Claire Hill

hi all, i'm really new to ruby and i need to do a script that will check
my email every 5minutes and after 20minutes of no new mail it will
produce "test fail" (im a tester by the way). i have found a cool code
for checking email every 5min but i could not find a way to stop it
after 20minutes? any help is greatly appreciated :)

here's the code: (i took credit to the originator of the code here and
my sincere thanks..:))

def time_block
start_time = Time.now
Thread.new { yield }
Time.now - start_time
end

def repeat_every(seconds)
begin
time_spent = time_block { yield }
sleep(seconds - time_spent)
end while time_spent < seconds
end


repeat_every(300) { # checking of email ever 5 minutes
puts "Start checking: #{Time.now}"
# email checking here
puts "End checking: #{Time.now}"
}
 
B

Bosko Ivanisevic

hi all, i'm really new to ruby and i need to do a script that will check
my email every 5minutes and after 20minutes of no new mail it will
produce "test fail" (im a tester by the way). i have found a cool code
for checking email every 5min but i could not find a way to stop it
after 20minutes? any help is greatly appreciated :)

here's the code: (i took credit to the originator of the code here and
my sincere thanks..:))

def time_block
  start_time = Time.now
  Thread.new { yield }
  Time.now - start_time
end

def repeat_every(seconds)
 begin
    time_spent = time_block { yield }
    sleep(seconds - time_spent)
 end while time_spent < seconds
end

repeat_every(300) { # checking of email ever 5 minutes
  puts "Start checking: #{Time.now}"
  # email checking here
  puts "End checking: #{Time.now}"}

Easiest way is to add one more argument to the repeat_every function:

def repeat_every(seconds, checks_count)
check_no = 0
begin
time_spent = time_block { yield }
check_no += 1
break if check_no >= checks_count
sleep(seconds - time_spent)
end while time_spent < seconds
end

And call it with:

repeat_every(300, 4) {....}
 
C

Claire Hill

Bosko said:
Easiest way is to add one more argument to the repeat_every function:

def repeat_every(seconds, checks_count)
check_no = 0
begin
time_spent = time_block { yield }
check_no += 1
break if check_no >= checks_count
sleep(seconds - time_spent)
end while time_spent < seconds
end

And call it with:

repeat_every(300, 4) {....}

hey there, thank you so much..you save me!!! my heartfelt thanks :)
 
L

lasitha

[...]

def repeat_every(seconds)
=A0begin
=A0 =A0time_spent =3D time_block { yield }
=A0 =A0sleep(seconds - time_spent)
=A0end while time_spent < seconds
end

FYI, if time_spent > seconds in the above method, the call to sleep
will raise 'ArgumentError: time interval must be positive'.

cheers,
lasitha
 
C

Claire Hill

lasitha said:
[...]

def repeat_every(seconds)
�begin
� �time_spent = time_block { yield }
� �sleep(seconds - time_spent)
�end while time_spent < seconds
end

FYI, if time_spent > seconds in the above method, the call to sleep
will raise 'ArgumentError: time interval must be positive'.

cheers,
lasitha

hi lasitha..:) thanks for the info...sure it helps a lot..:)
 

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

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top