Class: Lackie::Poller

Inherits:
Object
  • Object
show all
Defined in:
lib/lackie/poller.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Poller

Returns a new instance of Poller.



3
4
5
6
7
# File 'lib/lackie/poller.rb', line 3

def initialize(options={})
  @timeout_seconds = options.delete(:timeout_seconds) || 3
  @interval_seconds = options.delete(:interval_seconds) || 0.2
  @sleeper = options.delete(:sleeper) || Kernel
end

Instance Method Details

#await(outcome, options = {}) ⇒ Object

Raises:



9
10
11
12
13
14
15
16
17
18
# File 'lib/lackie/poller.rb', line 9

def await(outcome, options={})
  seconds_waited = 0
  timeout_seconds = options[:timeout_seconds] || @timeout_seconds
  while seconds_waited <= timeout_seconds
    return if yield
    @sleeper.sleep @interval_seconds
    seconds_waited += @interval_seconds
  end
  raise TimeoutError.new("Timed out after #{timeout_seconds} seconds awaiting #{outcome}")
end