Class: NewRelic::Agent::EventLoop::Timer

Inherits:
Object
  • Object
show all
Defined in:
lib/new_relic/agent/event_loop.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Timer.



13
14
15
16
17
18
19
20
# File 'lib/new_relic/agent/event_loop.rb', line 13

def initialize(interval, event, repeat = false)
  @interval = interval
  @event = event
  @repeat = repeat
  @started_at = Process.clock_gettime(Process::CLOCK_REALTIME)
  @last_fired_at = nil
  reschedule
end

Instance Attribute Details

#eventObject (readonly)



11
12
13
# File 'lib/new_relic/agent/event_loop.rb', line 11

def event
  @event
end

#intervalObject (readonly)



11
12
13
# File 'lib/new_relic/agent/event_loop.rb', line 11

def interval
  @interval
end

#last_fired_atObject (readonly)



11
12
13
# File 'lib/new_relic/agent/event_loop.rb', line 11

def last_fired_at
  @last_fired_at
end

#next_fire_timeObject (readonly)



11
12
13
# File 'lib/new_relic/agent/event_loop.rb', line 11

def next_fire_time
  @next_fire_time
end

Instance Method Details

#advance(amount) ⇒ Object



26
27
28
# File 'lib/new_relic/agent/event_loop.rb', line 26

def advance(amount)
  @next_fire_time -= amount
end

#calculate_next_fire_timeObject



34
35
36
37
38
39
40
41
42
43
# File 'lib/new_relic/agent/event_loop.rb', line 34

def calculate_next_fire_time
  now = Process.clock_gettime(Process::CLOCK_REALTIME)
  return now if @interval == 0

  fire_time = @last_fired_at || now
  while fire_time <= now
    fire_time += @interval
  end
  fire_time
end

#due?(now = Process.clock_gettime(Process::CLOCK_REALTIME)) ⇒ Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/new_relic/agent/event_loop.rb', line 49

def due?(now = Process.clock_gettime(Process::CLOCK_REALTIME))
  now >= @next_fire_time
end

#finished?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/new_relic/agent/event_loop.rb', line 53

def finished?
  !@repeat && @last_fired_at
end

#last_interval_startObject



30
31
32
# File 'lib/new_relic/agent/event_loop.rb', line 30

def last_interval_start
  @last_fired_at || @started_at
end

#rescheduleObject



22
23
24
# File 'lib/new_relic/agent/event_loop.rb', line 22

def reschedule
  @next_fire_time = calculate_next_fire_time
end

#set_fired_timeObject



45
46
47
# File 'lib/new_relic/agent/event_loop.rb', line 45

def set_fired_time
  @last_fired_at = Process.clock_gettime(Process::CLOCK_REALTIME)
end