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
-
.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_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
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
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
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 |