Class: VORuby::Misc::ConnectionMonitor

Inherits:
Object
  • Object
show all
Defined in:
lib/voruby/misc/connection_monitor.rb

Overview

Monitors (by way of “pinging”) a URL.

monitor = ConnectionMonitor.new('http://www.noao.edu/')
monitor.on_success = Proc.new{ |pinger, monitor|
  puts "#{monitor.url} is awake!"
}
monitor.on_failure = Proc.new{ |pinger, monitor|
  puts "Connection to #{monitor.url} failed: #{pinger.exception}"
}

monitor.start
  => http://www.noao.edu/ is awake!           # Wait 10 minutes...
  => http://www.noao.edu/ is awake!           # Wait 10 minutes...
  => Connection to http://www.noao.edu/ failed: some error message  # Uh oh! Something happened...

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, polling_freq = 600, timeout = 5) ⇒ ConnectionMonitor

Place a monitor on the specified URL. By default the URL will be “pinged” every 600 seconds and will timeout if it doesn’t receive a response within 5 seconds.



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/voruby/misc/connection_monitor.rb', line 41

def initialize(url, polling_freq=600, timeout=5)
  @pinger = Net::Ping::HTTP.new(url)
    
  self.polling_freq = polling_freq
  self.url = url
  self.timeout = timeout
    
  @interrupted = false
    
  self.on_failure = Proc.new{ |pinger, monitor|
    puts "Connection to #{monitor.url} failed: #{pinger.exception}."
  }
  self.on_success = Proc.new{ |pinger, monitor| }
  self.on_interrupt = Proc.new{ @interrupted = true }
    
  trap("INT"){ self.on_interrupt.call }
end

Instance Attribute Details

#on_failureObject

A Proc triggered when a ping fails. Takes the Net::PingHTTP object and the monitor itself.



31
32
33
# File 'lib/voruby/misc/connection_monitor.rb', line 31

def on_failure
  @on_failure
end

#on_interruptObject

A Proc triggered when the monitor is interrupted (i.e via Ctrl-C)



35
36
37
# File 'lib/voruby/misc/connection_monitor.rb', line 35

def on_interrupt
  @on_interrupt
end

#on_successObject

A Proc triggered when a ping succeeds. Takes the Net::PingHTTP object and the monitor itself.



33
34
35
# File 'lib/voruby/misc/connection_monitor.rb', line 33

def on_success
  @on_success
end

#pingerObject (readonly)

The Net::PingHTTP object doing the actual pinging.



22
23
24
# File 'lib/voruby/misc/connection_monitor.rb', line 22

def pinger
  @pinger
end

#polling_freqObject

The frequency (in seconds) at which to poll.



29
30
31
# File 'lib/voruby/misc/connection_monitor.rb', line 29

def polling_freq
  @polling_freq
end

#timeoutObject

The amount of time to wait (in seconds) before timing out a connection.



26
27
28
# File 'lib/voruby/misc/connection_monitor.rb', line 26

def timeout
  @timeout
end

#urlObject

The URL being pinged.



24
25
26
# File 'lib/voruby/misc/connection_monitor.rb', line 24

def url
  @url
end

Instance Method Details

#startObject

Start monitoring.



79
80
81
82
83
84
85
86
# File 'lib/voruby/misc/connection_monitor.rb', line 79

def start
  while true
    return if @interrupted
    
    pinger.ping ? on_success.call(pinger, self): on_failure.call(pinger, self)
    sleep(polling_freq)
  end
end

#start_threadedObject

Start monitoring, but do so in a thread.



73
74
75
76
# File 'lib/voruby/misc/connection_monitor.rb', line 73

def start_threaded
  @t = Thread.new { start }
  @t.run
end

#stopObject

Stop monitoring.



89
90
91
# File 'lib/voruby/misc/connection_monitor.rb', line 89

def stop
  self.on_interrupt.call
end