Project

wait

0.02
No commit activity in last 3 years
No release in over 3 years
Executes a block until there's a valid result.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies
 Project Readme

Build Status

Description

The wait gem executes a block until there's a valid (by default, truthy) result. Useful for blocking script execution until:

  • an HTTP request was successful
  • a port has opened
  • a process has started
  • etc.

Installation

Add to your Gemfile:

gem "wait", "~> 0.5.3"

Examples

wait = Wait.new
# => #<Wait>
wait.until { Time.now.sec.even? }
# [Counter] attempt 1/5
# [Tester]  result: false
# [Rescuer] rescued: Wait::ResultInvalid
# [Raiser]  not raising: Wait::ResultInvalid
# [Delayer] delaying for 1s
# [Counter] attempt 2/5
# [Tester]  result: true
# => true

If you wish to handle an exception by attempting the block again, pass one or an array of exceptions with the :rescue option.

wait = Wait.new(:rescue => RuntimeError)
# => #<Wait>
wait.until do |attempt|
  case attempt
  when 1 then nil
  when 2 then raise RuntimeError
  when 3 then "foo"
  end
end
# [Counter] attempt 1/5
# [Tester]  result: nil
# [Rescuer] rescued: Wait::ResultInvalid
# [Raiser]  not raising: Wait::ResultInvalid
# [Delayer] delaying for 1s
# [Counter] attempt 2/5
# [Rescuer] rescued: RuntimeError
# [Raiser]  not raising: RuntimeError
# [Delayer] delaying for 1s
# [Counter] attempt 3/5
# [Tester]  result: "foo"
# => "foo"

Basic Options

attempts
Number of times to attempt the block. Default is 5.
timeout
Seconds until the block times out. Default is 15.
delay
Seconds to delay in between attempts. Default is 1.
rescue
One or an array of exceptions to rescue. Default is nil.
debug
If true, debug logging is enabled. Default is false.

Advanced Options

logger
Ruby logger used. Default is Wait::BaseLogger.
counter
Strategy used to count attempts. Default is Wait::BaseCounter.
delayer
Strategy used to delay in between attempts. Default is Wait::RegularDelayer.
rescuer
Strategy used to rescue exceptions. Default is Wait::BaseRescuer.
tester
Strategy used to test the result. Default is Wait::TruthyTester.
raiser
Strategy used to raise specific exceptions. Default is Wait::SignalRaiser.

Documentation

RDoc-generated documentation available here.