module Kernel

Extend the Kernel module with a simple interface for the Attempt class.

Public Instance Methods

attempt(tries: 3, interval: 60, timeout: 10){ # some op } click to toggle source

Attempt to perform the operation in the provided block up to tries times, sleeping interval between each try. By default the number of tries defaults to 3, the interval defaults to 60 seconds, and there is no timeout specified.

If timeout is provided then the operation is wrapped in a Timeout block as well. This is handy for those rare occasions when an IO connection could hang indefinitely, for example.

If the operation still fails the (last) error is then re-raised.

This is really just a convenient wrapper for Attempt.new + Attempt#attempt.

Example:

# Make 3 attempts to connect to the database, 60 seconds apart.
attempt{ DBI.connect(dsn, user, passwd) }
# File lib/attempt.rb, line 134
def attempt(**kwargs, &block)
  object = Attempt.new(**kwargs)
  object.attempt(&block)
end