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.3.0'

Class Method Summary collapse

Class Method Details

.default_exp_baseFloat

get the default exponential base

Returns:

  • (Float)

    the base for exponential backoff



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

def self.default_exp_base
  @@default_exp_base ||= 1
end

.default_exp_base=(exp_base) ⇒ Object

set the default exponential base

Parameters:

  • exp_base (Float)

    the base for exponential backoff



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

def self.default_exp_base=(exp_base)
  @@default_exp_base = exp_base
end

.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



32
33
34
# File 'lib/waiting.rb', line 32

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



38
39
40
# File 'lib/waiting.rb', line 38

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



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/waiting.rb', line 46

def self.wait(opts = {})
  interval = opts.fetch(:interval) { default_interval }
  exp_base = opts.fetch(:exp_base) { default_exp_base }
  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

    waiter.attempts = attempts
    waiter.exp_base = exp_base
    waiter.interval = interval
    waiter.max_attempts = max_attempts

    yield(waiter)
    break if waiter.done?
    sleep exp_base ** attempts * interval
    attempts += 1
  end
end