From 4804205a640561664f6d2fc07a0dbab7cfa7cf07 Mon Sep 17 00:00:00 2001 From: Patrick J Cherry Date: Fri, 19 Aug 2011 16:53:30 +0100 Subject: Tweaked sleep in the thread to make sure poll_every does what it says. --- lib/mauve/mauve_thread.rb | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) (limited to 'lib') diff --git a/lib/mauve/mauve_thread.rb b/lib/mauve/mauve_thread.rb index 33ebcab..2191d58 100644 --- a/lib/mauve/mauve_thread.rb +++ b/lib/mauve/mauve_thread.rb @@ -36,9 +36,12 @@ module Mauve self.state = :starting @poll_every ||= interval + # + # Make sure we get a number. + # + @poll_every = 5 unless @poll_every.is_a?(Numeric) - sleep_loops = (@poll_every.to_f / 0.1).round.to_i - sleep_loops = 1 if sleep_loops.nil? or sleep_loops < 1 + rate_limit = 0.1 while self.state != :stopping do @@ -53,19 +56,22 @@ module Mauve self.state = :started end + yield_start = Time.now.to_f + yield # - # Ah-ha! Sleep with a break clause. + # Ah-ha! Sleep with a break clause. Make sure we poll every @poll_every seconds. # - sleep_loops.times do + ((@poll_every.to_f - Time.now.to_f + yield_start.to_f)/rate_limit). + round.to_i.times do break if self.should_stop? # # This is a rate-limiting step # - Kernel.sleep 0.1 + Kernel.sleep rate_limit end end -- cgit v1.2.1