Class: ZTK::RescueRetry
- Inherits:
-
Object
- Object
- ZTK::RescueRetry
- 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.
Class Method Summary (collapse)
-
+ (Object) try(options = {}) { ... }
Rescue and Retry the supplied block.
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.
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(={}, &block) = Base.build_config({ :tries => 1, :on => Exception, :delay => 1, :raise => nil }, ) !block_given? and Base.log_and_raise(.ui.logger, RescueRetryError, "You must supply a block!") raise_exceptions = [.raise].flatten.compact retry_exceptions = [.on].flatten.compact begin return block.call rescue *retry_exceptions => e .tries -= 1 if ((.tries > 0) && !raise_exceptions.include?(e.class)) .ui.logger.warn { "Caught #{e.inspect}, we will give it #{.tries} more tr#{.tries > 1 ? 'ies' : 'y'}." } sleep(.delay) .on_retry and .on_retry.call(e) retry else .ui.logger.fatal { "Caught #{e.inspect} and we have no more tries left! We have to give up now!" } raise e end end end |