Class: VORuby::Misc::ConnectionMonitor
- Inherits:
-
Object
- Object
- VORuby::Misc::ConnectionMonitor
- 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
-
#on_failure ⇒ Object
A Proc triggered when a ping fails.
-
#on_interrupt ⇒ Object
A Proc triggered when the monitor is interrupted (i.e via Ctrl-C).
-
#on_success ⇒ Object
A Proc triggered when a ping succeeds.
-
#pinger ⇒ Object
readonly
The Net::PingHTTP object doing the actual pinging.
-
#polling_freq ⇒ Object
The frequency (in seconds) at which to poll.
-
#timeout ⇒ Object
The amount of time to wait (in seconds) before timing out a connection.
-
#url ⇒ Object
The URL being pinged.
Instance Method Summary collapse
-
#initialize(url, polling_freq = 600, timeout = 5) ⇒ ConnectionMonitor
constructor
Place a monitor on the specified URL.
-
#start ⇒ Object
Start monitoring.
-
#start_threaded ⇒ Object
Start monitoring, but do so in a thread.
-
#stop ⇒ Object
Stop monitoring.
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_failure ⇒ Object
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_interrupt ⇒ Object
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_success ⇒ Object
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 |
#pinger ⇒ Object (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_freq ⇒ Object
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 |
#timeout ⇒ Object
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 |
#url ⇒ Object
The URL being pinged.
24 25 26 |
# File 'lib/voruby/misc/connection_monitor.rb', line 24 def url @url end |
Instance Method Details
#start ⇒ Object
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_threaded ⇒ Object
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 |
#stop ⇒ Object
Stop monitoring.
89 90 91 |
# File 'lib/voruby/misc/connection_monitor.rb', line 89 def stop self.on_interrupt.call end |