Class: ZTK::RescueRetry

Inherits:
Object
  • Object
show all
Defined in:
lib/ztk/rescue_retry.rb

Overview

RescueRetry Class

This class contains an exception handling tool, which will allowing retry of all or specific Exceptions based on a set number of attempts to make.

The block is yielded and if a valid exception occurs the block will be re-executed for the set number of attempts.

Examples:

Retry specific exceptions


counter = 0
ZTK::RescueRetry.try(:tries => 3, :on => EOFError) do
  counter += 1
  raise EOFError
end
puts counter.inspect

Retry all exceptions


counter = 0
ZTK::RescueRetry.try(:tries => 3) do
  counter += 1
  raise "OMGWTFBBQ"
end
puts counter.inspect

Retry exception is skipped because it does not match conditions


counter = 0
ZTK::RescueRetry.try(:tries => 3, :on => EOFError) do
  counter += 1
  raise "OMGWTFBBQ"
end
puts counter.inspect

Author:

Class Method Summary (collapse)

Class Method Details

+ (Object) try(options = {}) { ... }

Rescue and Retry the supplied block.

When no options are supplied, if an Exception is encounter it is surfaced immediately and no retry is performed.

It is advisable to at least leave the delay option at 1. You could optionally set this to 0, but this is generally a bad idea.

Parameters:

  • options (Hash) (defaults to: {})

    Configuration options hash.

Options Hash (options):

  • :tries (Integer) — default: 1

    How many attempts at executing the block before we give up and surface the Exception.

  • :on (Exception, Array<Exception>) — default: Exception

    Watch for specific exceptions instead of performing retry on all exceptions.

  • :raise (Exception, Array<Exception>) — default: Exception

    Watch for specific exceptions and do not attempt to retry if they are raised.

  • :delay (Float, Integer) — default: 1

    How long to sleep for between each retry.

  • :on_retry (Lambda, Proc) — default: nil

    A proc or lambda to call when we catch an exception and retry.

Yields:

  • Block should execute the tasks to be rescued and retried if needed.

Returns:

  • (Object)

    The return value of the block.



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/ztk/rescue_retry.rb', line 72

def try(options={}, &block)
  options = Base.build_config({
    :tries => 1,
    :on => Exception,
    :delay => 1,
    :raise => nil
  }, options)

  !block_given? and Base.log_and_raise(options.ui.logger, RescueRetryError, "You must supply a block!")

  raise_exceptions = [options.raise].flatten.compact
  retry_exceptions = [options.on].flatten.compact

  begin
    return block.call

  rescue *retry_exceptions => e

    options.tries -= 1

    if ((options.tries > 0) && !raise_exceptions.include?(e.class))
      options.ui.logger.warn { "Caught #{e.inspect}, we will give it #{options.tries} more tr#{options.tries > 1 ? 'ies' : 'y'}." }

      sleep(options.delay)

      options.on_retry and options.on_retry.call(e)

      retry
    else
      options.ui.logger.fatal { "Caught #{e.inspect} and we have no more tries left! We have to give up now!" }

      raise e
    end
  end

end