Class: Nanite::Reaper

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

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 Method Details

#reset(token) ⇒ Object



20
21
22
# File 'lib/nanite/reaper.rb', line 20

def reset(token)
  @timeouts[token][:timestamp] = Time.now + @timeouts[token][:seconds]
end

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



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

def reset_with_autoregister_hack(token,seconds,&blk)
  unless @timeouts[token]
    timeout(token, seconds, &blk)
  end
  reset(token)
end

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



9
10
11
# File 'lib/nanite/reaper.rb', line 9

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