Class: KingKong::Pinger

Inherits:
Object
  • Object
show all
Includes:
EventMachine::Deferrable, Logging
Defined in:
lib/kingkong/pinger.rb

Overview

Executes pings within a specificed duration.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logging

#logger

Constructor Details

#initialize(wait = 5, &block) ⇒ Pinger

Returns a new instance of Pinger.



14
15
16
17
18
# File 'lib/kingkong/pinger.rb', line 14

def initialize(wait=5,&block)
  @wait = wait
  @block = block if block_given?
  self
end

Instance Attribute Details

#waitObject (readonly)

Returns the value of attribute wait.



12
13
14
# File 'lib/kingkong/pinger.rb', line 12

def wait
  @wait
end

Instance Method Details

#on_ping(&block) ⇒ Object

Fire this if when a ping completes



34
35
36
# File 'lib/kingkong/pinger.rb', line 34

def on_ping(&block)
  @on_ping = block
end

#startObject

Starts the pinger



21
22
23
24
25
# File 'lib/kingkong/pinger.rb', line 21

def start
  logger.debug "Starting pinger #{self}"
  @timer = EventMachine::PeriodicTimer.new(wait){ ping }
  ping # Start a ping right away, the timer above will fire later.
end

#stopObject

Stop the pinger from executing



28
29
30
31
# File 'lib/kingkong/pinger.rb', line 28

def stop
  logger.debug "Stopping pinger #{self}"
  @timer.cancel if @timer
end