Module: Kernel

Defined in:
lib/attempt.rb

Overview

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

Instance Method Summary collapse

Instance Method Details

#attempt(**kwargs, &block) ⇒ Object

:call-seq:

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

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) }


134
135
136
137
# File 'lib/attempt.rb', line 134

def attempt(**kwargs, &block)
  object = Attempt.new(**kwargs)
  object.attempt(&block)
end