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



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

#actionProc (readonly)

Returns action to perform.

Returns:

  • (Proc)

    action to perform



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

def action
  @action
end

#attemptsInteger (readonly)

Returns number of attempts.

Returns:

  • (Integer)

    number of attempts



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

def attempts
  @attempts
end

#deadTrueClass, FalseClass (readonly)

Returns retry is dead.

Returns:

  • (TrueClass, FalseClass)

    retry is dead



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

def dead
  @dead
end

#descriptionString (readonly)

Returns description of action.

Returns:

  • (String)

    description of action



26
27
28
# File 'lib/bogo/retry.rb', line 26

def description
  @description
end

#max_attemptsInteger (readonly)

Returns maximum number of attempts.

Returns:

  • (Integer)

    maximum number of attempts



28
29
30
# File 'lib/bogo/retry.rb', line 28

def max_attempts
  @max_attempts
end

#uiBogo::Ui (readonly)

Returns UI to direct warnings.

Returns:

  • (Bogo::Ui)

    UI to direct warnings



30
31
32
# File 'lib/bogo/retry.rb', line 30

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



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

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)


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

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



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