Class: Metriks::TimeTracker

Inherits:
Object
  • Object
show all
Defined in:
lib/metriks/time_tracker.rb

Instance Method Summary collapse

Constructor Details

#initialize(interval) ⇒ TimeTracker

Returns a new instance of TimeTracker.



3
4
5
6
# File 'lib/metriks/time_tracker.rb', line 3

def initialize(interval)
  @interval = interval
  @next_time = Time.now.to_f
end

Instance Method Details

#next_timeObject



20
21
22
23
24
# File 'lib/metriks/time_tracker.rb', line 20

def next_time
  now = Time.now.to_f
  @next_time = now if @next_time <= now
  @next_time += @interval - (@next_time % @interval)
end

#now_flooredObject



15
16
17
18
# File 'lib/metriks/time_tracker.rb', line 15

def now_floored
  time = Time.now.to_i
  time - (time % @interval)
end

#sleepObject



8
9
10
11
12
13
# File 'lib/metriks/time_tracker.rb', line 8

def sleep
  sleep_time = next_time - Time.now.to_f
  if sleep_time > 0
    Kernel.sleep(sleep_time)
  end
end