module DeadMan::Switch
Constants
- CALLBACKS
- SWITCHES
Public Instance Methods
adjusted_interval(interval)
click to toggle source
Gives a padding on interval e.g. 1 minute -> 6 minutes
# File lib/dead_man/switch.rb, line 47 def adjusted_interval(interval) interval + 5.minutes end
alert(switch, timestamp)
click to toggle source
# File lib/dead_man/switch.rb, line 51 def alert(switch, timestamp) notify("#{switch} died. The switch was last triggered at #{timestamp}") end
alertable?(heartbeat_at, interval)
click to toggle source
# File lib/dead_man/switch.rb, line 41 def alertable?(heartbeat_at, interval) heartbeat_at < adjusted_interval(interval).ago end
check(switch)
click to toggle source
# File lib/dead_man/switch.rb, line 21 def check(switch) if SWITCHES.has_key?(switch) timestamp = last_heartbeat_at(switch) raise 'Unrecorded switch' if timestamp.nil? alert(switch, timestamp) if alertable?(timestamp, SWITCHES[switch]) else return false end rescue notify("Check failed for #{switch}. This is usually because an improper heartbeat timestamp was stored.") end
last_heartbeat_at(switch)
click to toggle source
# File lib/dead_man/switch.rb, line 33 def last_heartbeat_at(switch) key = DeadMan.key(switch) timestamp = DeadMan.redis.get(key) Time.parse(timestamp) rescue return nil end
notify(message)
click to toggle source
# File lib/dead_man/switch.rb, line 55 def notify(message) CALLBACKS.each do |callback| callback.call(message) end end
register_callback(callback)
click to toggle source
# File lib/dead_man/switch.rb, line 13 def register_callback(callback) CALLBACKS << callback end
register_switch(name, interval)
click to toggle source
# File lib/dead_man/switch.rb, line 17 def register_switch(name, interval) SWITCHES[name] = interval end
run()
click to toggle source
# File lib/dead_man/switch.rb, line 7 def run SWITCHES.each_key do |switch| check(switch) end end