Module: Waiting

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

Defined Under Namespace

Classes: Waiter

Constant Summary collapse

VERSION =
'0.1.0'

Class Method Summary collapse

Class Method Details

.default_intervalFixnum

get the default wait interval

Returns:

  • (Fixnum)

    the interval in seconds



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

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

.default_interval=(interval) ⇒ Object

set the default wait interval

Parameters:

  • interval (Fixnum)

    the interval in seconds



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

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

.default_max_attemptsFixnum

get the default max attempts

Returns:

  • (Fixnum)

    the default max attempts



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

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



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

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



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

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
    fail "Timed out after #{interval * max_attempts}s" if attempts >= max_attempts
    yield(waiter)
    break if waiter.done?
    sleep interval
    attempts += 1
  end
end