Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge remote-tracking branch 'origin/round-logger-sleep-time'

  • Loading branch information...
commit 056d10d4383f7100b37c42537d48bf80603cde48 2 parents 5279bfa + 087e606
@eric eric authored
Showing with 29 additions and 11 deletions.
  1. +29 −11 lib/metriks/reporter/logger.rb
View
40 lib/metriks/reporter/logger.rb
@@ -7,22 +7,20 @@ def initialize(options = {})
@log_level = options[:log_level] || ::Logger::INFO
@prefix = options[:prefix] || 'metriks:'
- @registry = options[:registry] || Metriks::Registry.default
- @interval = options[:interval] || 60
- @on_error = options[:on_error] || proc { |ex| }
+ @registry = options[:registry] || Metriks::Registry.default
+ @time_tracker = TimeTracker.new(options[:interval] || 60)
+ @on_error = options[:on_error] || proc { |ex| }
end
def start
@thread ||= Thread.new do
loop do
- sleep @interval
-
- Thread.new do
- begin
- write
- rescue Exception => ex
- @on_error[ex] rescue nil
- end
+ @time_tracker.sleep
+
+ begin
+ write
+ rescue Exception => ex
+ @on_error[ex] rescue nil
end
end
end
@@ -114,5 +112,25 @@ def format_message(args)
end
end.join(' ')
end
+
+ class TimeTracker
+ def initialize(interval)
+ @interval = interval
+ @next_time = Time.now.to_f
+ end
+
+ def sleep
+ sleep_time = next_time - Time.now.to_f
+ if sleep_time > 0
+ Kernel.sleep(sleep_time)
+ end
+ end
+
+ def next_time
+ now = Time.now.to_f
+ @next_time = now if @next_time <= now
+ @next_time += @interval - (@next_time % @interval)
+ end
+ end
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.