Class: Einhorn::Event::Timer

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

Direct Known Subclasses

ACKTimer

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(time, start = nil, &blk) ⇒ Timer

Returns a new instance of Timer.



5
6
7
8
9
# File 'lib/einhorn/event/timer.rb', line 5

def initialize(time, start=nil, &blk)
  @time = time
  @start = start || Time.now
  @blk = blk
end

Instance Attribute Details

#timeObject (readonly)

Returns the value of attribute time.



3
4
5
# File 'lib/einhorn/event/timer.rb', line 3

def time
  @time
end

Class Method Details

.open(*args, &blk) ⇒ Object

TODO: abstract into some interface



12
13
14
15
16
# File 'lib/einhorn/event/timer.rb', line 12

def self.open(*args, &blk)
  instance = self.new(*args, &blk)
  instance.register!
  instance
end

Instance Method Details

#deregister!Object



34
35
36
37
# File 'lib/einhorn/event/timer.rb', line 34

def deregister!
  Einhorn.log_debug("Nuking timer that expired #{Time.now - self.expires_at}s ago")
  Einhorn::Event.deregister_timer(self)
end

#expires_atObject



18
19
20
# File 'lib/einhorn/event/timer.rb', line 18

def expires_at
  @start + @time
end

#register!Object



29
30
31
32
# File 'lib/einhorn/event/timer.rb', line 29

def register!
  Einhorn.log_debug("Scheduling a new #{self.time}s timer")
  Einhorn::Event.register_timer(self)
end

#ring!Object



22
23
24
25
26
27
# File 'lib/einhorn/event/timer.rb', line 22

def ring!
  now = Time.now
  Einhorn.log_debug("Ringing timer that was scheduled #{now - @start}s ago and expired #{now - expires_at}s ago")
  deregister!
  @blk.call
end