Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

wait gem: executes a block until there's a result

tree: 5dd34af7f8

Fetching latest commit…

Octocat-spinner-32-eaf2f5

Cannot retrieve the latest commit at this time

Octocat-spinner-32 lib
Octocat-spinner-32 test
Octocat-spinner-32 .gitignore
Octocat-spinner-32 .rbenv-version
Octocat-spinner-32 .travis.yml
Octocat-spinner-32 Gemfile
Octocat-spinner-32 Gemfile.lock
Octocat-spinner-32 LICENCE
Octocat-spinner-32 NOTICE
Octocat-spinner-32 README.md
Octocat-spinner-32 Rakefile
Octocat-spinner-32 jenkins-test.sh
Octocat-spinner-32 wait.gemspec
README.md

Build Status

Description

The wait gem executes a block until there's a result. Useful for blocking script execution until:

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

Installation

Add to your Gemfile:

gem 'wait', :git => 'git@github.com:foursquare/wait.git'

Examples

wait = Wait.new
# => #<Wait>
wait.until { Time.now.sec.even? }
# Rescued exception while waiting: Wait::NoResultError: result was false
# Attempt 1/5 failed, delaying for 1s
# => 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
# Rescued exception while waiting: Wait::NoResultError: result was nil
# Attempt 1/5 failed, delaying for 1s
# Rescued exception while waiting: RuntimeError: RuntimeError
# Attempt 2/5 failed, delaying for 2s
# => "foo"

Options

:attempts
Number of times to attempt the block. Default is 5.
:timeout
Seconds until the block times out. Default is 15.
:delay
Initial (grows exponentially) delay (in seconds) to wait in between attempts. Default is 1.
:rescue
One or an array of exceptions to rescue. Default is nil.
:debug
If true, logs debugging output. Default is false.

Documentation

RDoc-generated documentation available here.

Something went wrong with that request. Please try again.