module Poller::Poller

Public Class Methods

new(probe, timeout_seconds, period_seconds, name = nil) click to toggle source
# File lib/poller/poller.rb, line 16
def initialize(probe, timeout_seconds, period_seconds, name = nil)
  @probe = probe
  @timeout_seconds = timeout_seconds
  @period = period_seconds
  @name = name.nil? ? "no name given" : name
end

Public Instance Methods

check() click to toggle source
# File lib/poller/poller.rb, line 23
def check
  @timeout ||= Timeout.new(@timeout_seconds) # allow injecting a Timeout object from within tests

  tries = 0
  check_started_at = Time.now
  last_sample_took = 0

  while !@timeout.occured?
    Kernel.sleep sleep_time(@period, last_sample_took)
    sample_started_at = Time.now
    response = @probe.sample
    satisfied = @probe.satisfied?
    last_sample_took = Time.now - sample_started_at
    tries += 1
    return [response, Time.now - sample_started_at] if satisfied
  end

  raise RuntimeError, "Timeout period has been exceeded for Poller (#{@name})." \
    + " Poller tried #{tries} times which in total took #{Time.now - check_started_at} seconds."
end

Private Instance Methods

sleep_time(period, last_sample_took) click to toggle source
# File lib/poller/poller.rb, line 45
def sleep_time(period, last_sample_took)
  st = period - last_sample_took
  st < 0 ? 0 : st
end