class Doberman::WatchDog

Attributes

error_message[R]
timeout[R]

Public Class Methods

new(options = {}) click to toggle source
# File lib/doberman.rb, line 9
def initialize(options = {})
  @error_message = options[:error_message] || "WatchDog timeout reached"
  @timeout = options[:timeout] || 60
  @resolution = options[:resolution] || 0.1
  @current = Thread.current
end

Public Instance Methods

ping() click to toggle source
# File lib/doberman.rb, line 23
def ping
  if @watchdog.nil?
    start
  else
    @reset = true
  end
end
start() click to toggle source
# File lib/doberman.rb, line 16
def start
  if @watchdog.nil?
    reset_ttl
    create_thread
  end
end
stop() click to toggle source
# File lib/doberman.rb, line 31
def stop
  kill_thread
end

Private Instance Methods

create_thread() click to toggle source
# File lib/doberman.rb, line 37
def create_thread
  @watchdog = Thread.new do
    while @ttl > 0
      sleep @resolution
      if @reset
        reset_ttl
      else
        @ttl -= @resolution
      end
    end
    @current.raise Timeout.new(@error_message)
  end
end
kill_thread() click to toggle source
# File lib/doberman.rb, line 56
def kill_thread
  unless @watchdog.nil?
    @watchdog.kill
    @watchdog = nil
  end
end
reset_ttl() click to toggle source
# File lib/doberman.rb, line 51
def reset_ttl
  @ttl = timeout.to_f
  @reset = false
end