Class: Nanite::Reaper

Inherits:
Object show all
Defined in:
lib/nanite/reaper.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(frequency = 2) ⇒ Reaper

Returns a new instance of Reaper.



4
5
6
7
# File 'lib/nanite/reaper.rb', line 4

def initialize(frequency=2)
  @timeouts = {}
  EM.add_periodic_timer(frequency) { EM.next_tick { reap } }
end

Instance Attribute Details

#timeoutsObject (readonly)

Returns the value of attribute timeouts.



3
4
5
# File 'lib/nanite/reaper.rb', line 3

def timeouts
  @timeouts
end

Instance Method Details

#register(token, seconds, &blk) ⇒ Object

Add the specified token to the internal timeout hash. The reaper will then check this instance on every reap.



11
12
13
# File 'lib/nanite/reaper.rb', line 11

def register(token, seconds, &blk)
  @timeouts[token] = {:timestamp => Time.now + seconds, :seconds => seconds, :callback => blk}
end

#unregister(token) ⇒ Object



15
16
17
# File 'lib/nanite/reaper.rb', line 15

def unregister(token)
  @timeouts.delete(token)
end

#update(token, seconds, &blk) ⇒ Object

Updates the timeout timestamp for the given token. If the token is unknown to this reaper instance it will be auto-registered, usually happening when you have several mappers and not all of them know this agent yet, but received a ping from it.



23
24
25
26
27
28
# File 'lib/nanite/reaper.rb', line 23

def update(token, seconds, &blk)
  unless @timeouts[token]
    register(token, seconds, &blk)
  end
  @timeouts[token][:timestamp] = Time.now + @timeouts[token][:seconds]
end