Class: AWS::Flow::RetryPolicy

Inherits:
Object
  • Object
show all
Defined in:
lib/aws/decider/async_retrying_executor.rb

Overview

Represents a policy for retrying failed tasks.

Instance Method Summary collapse

Constructor Details

#initialize(retry_function, options) ⇒ RetryPolicy

Creates a new ‘RetryPolicy` instance.

Parameters:

  • retry_function

    The method to be called for each retry attempt.

  • options

    A set of AWS::Flow::RetryOptions to modify the retry behavior.



103
104
105
106
107
108
109
110
111
112
# File 'lib/aws/decider/async_retrying_executor.rb', line 103

def initialize(retry_function, options)
  @retry_function = retry_function
  @exceptions_to_exclude = options.exceptions_to_exclude
  @exceptions_to_include = options.exceptions_to_include
  @max_attempts = options.maximum_attempts
  @retries_per_exception = options.retries_per_exception
  @should_jitter = options.should_jitter
  @jitter_function = options.jitter_function
  @options = options
end

Instance Method Details

#isRetryable(failure) ⇒ true, false

Returns ‘true` if the task can be retried for this failure.

Parameters:

  • failure

    The failure to test.

Returns:

  • (true, false)

    Returns ‘true` if the task can be retried for this failure.



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/aws/decider/async_retrying_executor.rb', line 120

def isRetryable(failure)
  if failure.respond_to? :cause
    failure_class = failure.cause.class
  else
    failure_class = failure.class
  end

  return true if @exceptions_to_exclude.empty? && @exceptions_to_include.empty?
  raise "#{failure} appears in both exceptions_to_include and exceptions_to_exclude" if @exceptions_to_exclude.include?(failure_class) && @exceptions_to_include.include?(failure_class)
  # In short, default to false
  # the second part of the statement does an intersection of the 2 arrays to see if any of the ancestors of
  # failure exists in @exceptions_to_include
  return (!@exceptions_to_exclude.include?(failure_class) && !(@exceptions_to_include & failure_class.ancestors).empty?)

   #return (!@exceptions_to_exclude.include?(failure) && @exceptions_to_include.include?(failure))
end

#next_retry_delay_seconds(first_attempt, time_of_recorded_failure, attempts, failure = nil, execution_id) ⇒ Object

Schedules a new retry attempt.

Parameters:

  • first_attempt
  • time_of_recorded_failure
  • attempts
  • failure (defaults to: nil)


147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/aws/decider/async_retrying_executor.rb', line 147

def next_retry_delay_seconds(first_attempt, time_of_recorded_failure, attempts, failure = nil, execution_id)
  if attempts.values.reduce(0, :+) < 2
    raise "This is bad, you have less than 2 attempts. More precisely, #{attempts} attempts"
  end
  if @max_attempts && @max_attempts != "NONE"
    return -1 if attempts.values.reduce(0, :+) > @max_attempts + 1
  end
  if failure && @retries_per_exception && @retries_per_exception.keys.include?(failure.class)
    return -1 if attempts[failure.class] > @retries_per_exception[failure.class]
  end
  return -1 if failure == nil

  # For reverse compatbility purposes, we must ensure that this function
  # can take 3 arguments. However, we must also consume options in order
  # for the default retry function to work correctly. Because we support
  # ruby 1.9, we cannot use default arguments in a lambda, so we resort to
  # the following workaround to supply a 4th argument if the function
  # expects it.
  call_args = [first_attempt, time_of_recorded_failure, attempts]
  call_args << @options if @retry_function.arity == 4
  retry_seconds = @retry_function.call(*call_args)
  # Check to see if we should jitter or not and pass in the jitter
  # function to retry function accordingly.
  if @should_jitter && retry_seconds > 0
    retry_seconds += @jitter_function.call(execution_id, retry_seconds/2)
  end
  return retry_seconds
end