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 that will be called for each retry attempt.

  • options

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



109
110
111
112
113
114
115
116
117
118
# File 'lib/aws/decider/async_retrying_executor.rb', line 109

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; `false` otherwise.

Parameters:

  • failure

    The failure to test.

Returns:

  • (true, false)

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



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/aws/decider/async_retrying_executor.rb', line 127

def isRetryable(failure)
  if failure.respond_to?(:cause) && !failure.cause.nil?
    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 with an initial delay.

Parameters:

  • first_attempt

    The time to delay before the first retry attempt is made.

  • time_of_recorded_failure
  • attempts

    The number of retry attempts to make before the task is considered to be failed.

  • failure (defaults to: nil)

    The type of failure to retry. If not specified, then all types of failures will be retried.

  • execution_id


162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/aws/decider/async_retrying_executor.rb', line 162

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