Project

waiting

0.0
No commit activity in last 3 years
No release in over 3 years
Waits so you don't have to!
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.11
~> 10.0
~> 3.0
~> 0.13.0
~> 0.9.8
 Project Readme

Build Status

Waiting

Waits so you don't have to!

Installation

Add this line to your application's Gemfile:

gem 'waiting'

And then execute:

$ bundle

Or install it yourself as:

$ gem install waiting

Usage

require 'waiting'


# Optionally set defaults.
#
Waiting.exp_base = exp_base         # defaults to 1
Waiting.interval = interval         # defaults to 5
Waiting.max_attempts = max_attempts # defaults to 60
Waiting.max_interval = max_interval # defaults to nil


# Will poll every interval, max_attempts times until something is true
#
Waiting.wait do |waiter|
  waiter.done if something
end


# Override the defaults here
#
Waiting.wait(exp_base: exp_base,
             interval: interval,
             max_attempts: max_attempts,
             max_interval: max_interval
            ) do |waiter|
  waiter.done if something
end


# Or make an instance of Waiting to pass around
#
waiting = Waiting.new(exp_base: exp_base,
                      interval: interval,
                      max_attempts: max_attempts,
                      max_interval: max_interval
                     ) do |waiter|
  waiter.done if something
end


# And get it to wait
#
waiting.wait


# Or overide any parameters here again
#
waiting.wait(exp_base: exp_base,
             interval: interval,
             max_attempts: max_attempts,
             max_interval: max_interval
            ) do |waiter|
  waiter.done if something
end


# Access the wait parameters during the wait
#
waiting.wait do |waiter|
  puts "attempts: #{waiter.attempts}"
  puts "exp_base: #{waiter.exp_base}"
  puts "interval: #{waiter.interval}"
  puts "max_attempts: #{waiter.max_attempts}"
  puts "max_interval: #{waiter.max_interval}"
  waiter.done if something
end
# =>
#   attempts: 0
#   exp_base: 1
#   interval: 5
#   max_attempts: 60
#   max_interval: nil


# Leverage exp base for exponential back off
#
# Will wait for: 1, 2, 4, 8, 16, 16, 16...
#
waiting.wait(exp_base: 2,
             interval: 1,
             max_attempts: 16
            ) do |waiter|
  waiter.done if something
end

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/meringu/waiting.