module Kernel
Extend the Kernel
module with a simple interface for the Attempt
class.
Public Instance Methods
Source
# File lib/attempt.rb, line 507 def attempt(**kwargs, &block) raise ArgumentError, 'No block given' unless block_given? object = Attempt.new(**kwargs) object.attempt(&block) end
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
.
All options supported by Attempt.new
are also supported here.
Example:
# Make 3 attempts to connect to the database, 60 seconds apart. attempt{ DBI.connect(dsn, user, passwd) } # Make 5 attempts with exponential backoff attempt(tries: 5, interval: 1, increment: 2) { risky_operation }