Class: Bogo::Retry
- Inherits:
-
Object
- Object
- Bogo::Retry
- Defined in:
- lib/bogo/retry.rb
Overview
Perform action and retry until successful or abort
Direct Known Subclasses
Defined Under Namespace
Classes: Exponential, Flat, Linear
Instance Attribute Summary collapse
-
#action ⇒ Proc
readonly
Action to perform.
-
#attempts ⇒ Integer
readonly
Number of attempts.
-
#dead ⇒ TrueClass, FalseClass
readonly
Retry is dead.
-
#description ⇒ String
readonly
Description of action.
-
#max_attempts ⇒ Integer
readonly
Maximum number of attempts.
-
#ui ⇒ Bogo::Ui
readonly
UI to direct warnings.
Class Method Summary collapse
-
.build(type, *args) { ... } ⇒ Retry
Create a type of retry.
Instance Method Summary collapse
-
#initialize(args = {}, &block) ⇒ self
constructor
Create a new retry instance.
- #retries ⇒ Integer
-
#run! {|exception| ... } ⇒ Object
Run action until success.
Constructor Details
#initialize(args = {}, &block) ⇒ self
Create a new retry instance
39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/bogo/retry.rb', line 39 def initialize(args={}, &block) unless(block) raise ArgumentError.new 'Expecting block but no block was provided!' end args = args.to_smash @ui = args[:ui] @description = args.fetch(:description, 'Task') @max_attempts = args[:max_attempts] @action = block @attempts = 0 @dead = false run! unless args[:auto_run] == false end |
Instance Attribute Details
#action ⇒ Proc (readonly)
Returns action to perform.
20 21 22 |
# File 'lib/bogo/retry.rb', line 20 def action @action end |
#attempts ⇒ Integer (readonly)
Returns number of attempts.
22 23 24 |
# File 'lib/bogo/retry.rb', line 22 def attempts @attempts end |
#dead ⇒ TrueClass, FalseClass (readonly)
Returns retry is dead.
24 25 26 |
# File 'lib/bogo/retry.rb', line 24 def dead @dead end |
#description ⇒ String (readonly)
Returns description of action.
26 27 28 |
# File 'lib/bogo/retry.rb', line 26 def description @description end |
#max_attempts ⇒ Integer (readonly)
Returns maximum number of attempts.
28 29 30 |
# File 'lib/bogo/retry.rb', line 28 def max_attempts @max_attempts end |
#ui ⇒ Bogo::Ui (readonly)
Returns UI to direct warnings.
30 31 32 |
# File 'lib/bogo/retry.rb', line 30 def ui @ui end |
Class Method Details
Instance Method Details
#retries ⇒ Integer
92 93 94 |
# File 'lib/bogo/retry.rb', line 92 def retries attempts > 0 ? attempts - 1 : 0 end |
#run! {|exception| ... } ⇒ Object
Run action until success
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/bogo/retry.rb', line 59 def run! if(dead) raise RuntimeError.new "Action has already reached maximum allowed attempts (#{max_attempts})!" else begin log_attempt! action.call rescue => e if(block_given?) raise unless yield(e) end if(max_attempts.nil? || attempts < max_attempts) interval = wait_on_failure(e) if(ui) if(max_attempts) attempt_info = "[Attempt #{attempts}/#{max_attempts}]" end ui.warn "#{description} failed (#{e.class}: #{e}) - Retry in #{interval.to_i} seconds #{attempt_info}" end sleep(interval) retry else if(ui && max_attempts.to_i > 0) ui.error "#{description} failed (#{e.class}: #{e}) - Maximum number of attempts reached!" end @dead = true raise e end end end end |