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
35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/bogo/retry.rb', line 35 def initialize(args={}, &block) unless(block) raise ArgumentError, '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.
16 17 18 |
# File 'lib/bogo/retry.rb', line 16 def action @action end |
#attempts ⇒ Integer (readonly)
Returns number of attempts.
18 19 20 |
# File 'lib/bogo/retry.rb', line 18 def attempts @attempts end |
#dead ⇒ TrueClass, FalseClass (readonly)
Returns retry is dead.
20 21 22 |
# File 'lib/bogo/retry.rb', line 20 def dead @dead end |
#description ⇒ String (readonly)
Returns description of action.
22 23 24 |
# File 'lib/bogo/retry.rb', line 22 def description @description end |
#max_attempts ⇒ Integer (readonly)
Returns maximum number of attempts.
24 25 26 |
# File 'lib/bogo/retry.rb', line 24 def max_attempts @max_attempts end |
#ui ⇒ Bogo::Ui (readonly)
Returns UI to direct warnings.
26 27 28 |
# File 'lib/bogo/retry.rb', line 26 def ui @ui end |
Class Method Details
.build(type, *args) { ... } ⇒ Retry
Create a type of retry
10 11 12 13 |
# File 'lib/bogo/retry.rb', line 10 def self.build(type, *args, &block) klass = self.const_get(Bogo::Utility.camel(type)) klass.new(*args, &block) end |
Instance Method Details
#retries ⇒ Integer
90 91 92 |
# File 'lib/bogo/retry.rb', line 90 def retries attempts > 0 ? attempts - 1 : 0 end |
#run! {|exception| ... } ⇒ Object
Run action until success
56 57 58 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 |
# File 'lib/bogo/retry.rb', line 56 def run! if(dead) raise RuntimeError, "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 |