Method: Attempt#initialize

Defined in:
lib/attempt.rb

#initialize(**options) ⇒ Attempt

:call-seq:

Attempt.new(**kwargs)

Creates and returns a new Attempt object. The supported keyword options are as follows:

  • tries - The number of attempts to make before giving up. Must be positive. The default is 3.

  • interval - The delay in seconds between each attempt. Must be non-negative. The default is 60.

  • log - An IO handle or Logger instance where warnings/errors are logged to. The default is nil.

  • increment - The amount to increment the interval between tries. Must be non-negative. The default is 0.

  • level - The level of exception to be caught. The default is StandardError (recommended over Exception).

  • warnings - Boolean value that indicates whether or not errors are treated as warnings

    until the maximum number of attempts has been made. The default is true.
    
  • timeout - Timeout in seconds to automatically wrap your proc in a Timeout block.

    Must be positive if provided. The default is nil (no timeout).
    
  • timeout_strategy - Strategy for timeout implementation. Options: :auto (default), :custom, :thread, :process, :fiber, :ruby_timeout

Example:

a = Attempt.new(tries: 5, increment: 10, timeout: 30, timeout_strategy: :process)
a.attempt{ http.get("http://something.foo.com") }

Raises ArgumentError if any parameters are invalid.



286
287
288
289
290
291
292
293
294
295
296
297
# File 'lib/attempt.rb', line 286

def initialize(**options)
  @tries     = validate_tries(options[:tries] || 3)
  @interval  = validate_interval(options[:interval] || 60)
  @log       = validate_log(options[:log])
  @increment = validate_increment(options[:increment] || 0)
  @timeout   = validate_timeout(options[:timeout])
  @timeout_strategy = options[:timeout_strategy] || :auto
  @level     = options[:level] || StandardError  # More appropriate default than Exception
  @warnings  = options.fetch(:warnings, true)    # More explicit than ||

  freeze_configuration if options[:freeze_config]
end