Module: Poller::Poller

Included in:
HTTP::HttpPoller
Defined in:
lib/poller/poller.rb

Instance Method Summary collapse

Instance Method Details

#checkObject

Raises:

  • (RuntimeError)


23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# 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

#initialize(probe, timeout_seconds, period_seconds, name = nil) ⇒ Object



16
17
18
19
20
21
# 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