aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPatrick J Cherry <patrick@bytemark.co.uk>2011-08-19 16:53:30 +0100
committerPatrick J Cherry <patrick@bytemark.co.uk>2011-08-19 16:53:30 +0100
commit4804205a640561664f6d2fc07a0dbab7cfa7cf07 (patch)
tree2c449b1ae2bd289b1ce2ddd8df50d76c785d906b
parentcdb78656916abe5adb946a25b913cda7785a42de (diff)
Tweaked sleep in the thread to make sure poll_every does what it says.
-rw-r--r--lib/mauve/mauve_thread.rb16
1 files changed, 11 insertions, 5 deletions
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