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
-
.default_exp_base ⇒ Float
get the default exponential base.
-
.default_exp_base=(exp_base) ⇒ Object
set the default exponential base.
-
.default_interval ⇒ Fixnum
get the default wait interval.
-
.default_interval=(interval) ⇒ Object
set the default wait interval.
-
.default_max_attempts ⇒ Fixnum
get the default max attempts.
-
.default_max_attempts=(max_attempts) ⇒ Object
set the default max attempts.
-
.wait(opts = {}) ⇒ Object
wait for something, call #ok on the waiter to signal the wait is over.
Class Method Details
.default_exp_base ⇒ Float
get the default exponential base
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
26 27 28 |
# File 'lib/waiting.rb', line 26 def self.default_exp_base=(exp_base) @@default_exp_base = exp_base end |
.default_interval ⇒ Fixnum
get the default wait interval
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
14 15 16 |
# File 'lib/waiting.rb', line 14 def self.default_interval=(interval) @@default_interval = interval end |
.default_max_attempts ⇒ Fixnum
get 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
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
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 |