Class: Bogo::Retry

Inherits:
Object
  • Object
show all
Defined in:
lib/bogo/retry.rb

Overview

Perform action and retry until successful or abort

Direct Known Subclasses

Exponential, Flat, Linear

Defined Under Namespace

Classes: Exponential, Flat, Linear

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}, &block) ⇒ self

Create a new retry instance

Parameters:

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

Options Hash (args):

  • :description (String)

    action description

  • :ui (Bogo::Ui)

    output failure/retry notifications

  • :max_attempts (Integer)

    maximum number of retries



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

#actionProc (readonly)

Returns action to perform.

Returns:

  • (Proc)

    action to perform



16
17
18
# File 'lib/bogo/retry.rb', line 16

def action
  @action
end

#attemptsInteger (readonly)

Returns number of attempts.

Returns:

  • (Integer)

    number of attempts



18
19
20
# File 'lib/bogo/retry.rb', line 18

def attempts
  @attempts
end

#deadTrueClass, FalseClass (readonly)

Returns retry is dead.

Returns:

  • (TrueClass, FalseClass)

    retry is dead



20
21
22
# File 'lib/bogo/retry.rb', line 20

def dead
  @dead
end

#descriptionString (readonly)

Returns description of action.

Returns:

  • (String)

    description of action



22
23
24
# File 'lib/bogo/retry.rb', line 22

def description
  @description
end

#max_attemptsInteger (readonly)

Returns maximum number of attempts.

Returns:

  • (Integer)

    maximum number of attempts



24
25
26
# File 'lib/bogo/retry.rb', line 24

def max_attempts
  @max_attempts
end

#uiBogo::Ui (readonly)

Returns UI to direct warnings.

Returns:

  • (Bogo::Ui)

    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

Parameters:

  • type (String, Symbol)

    name of retry type

  • args (Object)

    instantiation arguments

Yields:

  • instantiation block

Returns:

  • (Retry)

    specific subclass instance



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

#retriesInteger

Returns:

  • (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

Yields:

  • optional to allow custom exception check

Yield Parameters:

  • exception (Exception)

    caught

Yield Returns:

  • (TrueClass, FalseClass)

    if retry should be peformed

Returns:

  • (Object)

    result of action



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