Class: OneApm::Support::EventLoop::Timer

Inherits:
Object
  • Object
show all
Defined in:
lib/one_apm/support/event/timer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(interval, event, repeat = false) ⇒ Timer

Returns a new instance of Timer.



10
11
12
13
14
15
16
17
# File 'lib/one_apm/support/event/timer.rb', line 10

def initialize(interval, event, repeat=false)
  @interval      = interval
  @event         = event
  @repeat        = repeat
  @started_at    = Time.now
  @last_fired_at = nil
  reschedule
end

Instance Attribute Details

#eventObject (readonly)

Returns the value of attribute event.



8
9
10
# File 'lib/one_apm/support/event/timer.rb', line 8

def event
  @event
end

#intervalObject (readonly)

Returns the value of attribute interval.



8
9
10
# File 'lib/one_apm/support/event/timer.rb', line 8

def interval
  @interval
end

#last_fired_atObject (readonly)

Returns the value of attribute last_fired_at.



8
9
10
# File 'lib/one_apm/support/event/timer.rb', line 8

def last_fired_at
  @last_fired_at
end

#next_fire_timeObject (readonly)

Returns the value of attribute next_fire_time.



8
9
10
# File 'lib/one_apm/support/event/timer.rb', line 8

def next_fire_time
  @next_fire_time
end

Instance Method Details

#advance(amount) ⇒ Object



23
24
25
# File 'lib/one_apm/support/event/timer.rb', line 23

def advance(amount)
  @next_fire_time -= amount
end

#calculate_next_fire_timeObject



31
32
33
34
35
36
37
38
39
# File 'lib/one_apm/support/event/timer.rb', line 31

def calculate_next_fire_time
  now = Time.now
  return now if @interval == 0
  fire_time = @last_fired_at || now
  while fire_time <= now
    fire_time += @interval
  end
  fire_time
end

#due?(now = Time.now) ⇒ Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/one_apm/support/event/timer.rb', line 45

def due?(now=Time.now)
  now >= @next_fire_time
end

#finished?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/one_apm/support/event/timer.rb', line 49

def finished?
  !@repeat && @last_fired_at
end

#last_interval_startObject



27
28
29
# File 'lib/one_apm/support/event/timer.rb', line 27

def last_interval_start
  @last_fired_at || @started_at
end

#rescheduleObject



19
20
21
# File 'lib/one_apm/support/event/timer.rb', line 19

def reschedule
  @next_fire_time = calculate_next_fire_time
end

#set_fired_timeObject



41
42
43
# File 'lib/one_apm/support/event/timer.rb', line 41

def set_fired_time
  @last_fired_at = Time.now
end