Module: Waiting

Defined in:
lib/waiting.rb,
lib/waiting/waiter.rb,
lib/waiting/version.rb,
lib/waiting/timed_out_error.rb

Defined Under Namespace

Classes: TimedOutError, Waiter

Constant Summary collapse

VERSION =
'0.2.0'

Class Method Summary collapse

Class Method Details

.default_intervalFixnum

get the default wait interval

Returns:

  • (Fixnum)

    the interval in seconds



8
9
10
# File 'lib/waiting.rb', line 8

def self.default_interval
  @@default_interval ||= 5
end

.default_interval=(interval) ⇒ Object

set the default wait interval

Parameters:

  • interval (Fixnum)

    the interval in seconds



14
15
16
# File 'lib/waiting.rb', line 14

def self.default_interval=(interval)
  @@default_interval = interval
end

.default_max_attemptsFixnum

get the default max attempts

Returns:

  • (Fixnum)

    the default max attempts



20
21
22
# File 'lib/waiting.rb', line 20

def self.default_max_attempts
  @@default_max_attempts ||= 60
end

.default_max_attempts=(max_attempts) ⇒ Object

set the default max attempts

Parameters:

  • max_attempts (Fixnum)

    the default max attempts



26
27
28
# File 'lib/waiting.rb', line 26

def self.default_max_attempts=(max_attempts)
  @@default_max_attempts = max_attempts
end

.wait(opts = {}) ⇒ Object

wait for something, call #ok on the waiter to signal the wait is over

Parameters:

  • opts (Hash) (defaults to: {})

    the options to wait with.

Options Hash (opts):

  • :interval (Fixnum)

    polling interval in seconds for checking

  • :max_attempts (Fixnum)

    number of attempts before failing



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/waiting.rb', line 34

def self.wait(opts = {})
  interval = opts.fetch(:interval) { default_interval }
  max_attempts = opts.fetch(:max_attempts) { default_max_attempts }

  waiter = Waiter.new

  attempts = 0

  loop do
    if attempts >= max_attempts
      fail(TimedOutError, "Timed out after #{interval * max_attempts}s")
    end
    yield(waiter)
    break if waiter.done?
    sleep interval
    attempts += 1
  end
end