Class: Twingly::Metrics::TimeTracker
- Inherits:
-
Object
- Object
- Twingly::Metrics::TimeTracker
- Defined in:
- lib/twingly/metrics/time_tracker.rb
Instance Method Summary collapse
-
#initialize(interval) ⇒ TimeTracker
constructor
A new instance of TimeTracker.
- #next_time ⇒ Object
- #now_floored ⇒ Object
- #sleep ⇒ Object
Constructor Details
#initialize(interval) ⇒ TimeTracker
Returns a new instance of TimeTracker.
6 7 8 9 |
# File 'lib/twingly/metrics/time_tracker.rb', line 6 def initialize(interval) @interval = interval @next_time = Time.now.to_f end |
Instance Method Details
#next_time ⇒ Object
23 24 25 26 27 |
# File 'lib/twingly/metrics/time_tracker.rb', line 23 def next_time now = Time.now.to_f @next_time = now if @next_time <= now @next_time += @interval - (@next_time % @interval) end |
#now_floored ⇒ Object
18 19 20 21 |
# File 'lib/twingly/metrics/time_tracker.rb', line 18 def now_floored time = Time.now.to_i time - (time % @interval) end |
#sleep ⇒ Object
11 12 13 14 15 16 |
# File 'lib/twingly/metrics/time_tracker.rb', line 11 def sleep sleep_time = next_time - Time.now.to_f return unless sleep_time.positive? Kernel.sleep(sleep_time) end |