Class: StatesLanguageMachine::States::RetryPolicy

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_slm/states/task.rb

Overview

Retry policy class

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(definition) ⇒ RetryPolicy

Returns a new instance of RetryPolicy.



439
440
441
442
443
444
445
# File 'lib/ruby_slm/states/task.rb', line 439

def initialize(definition)
  @error_equals = Array(definition["ErrorEquals"])
  @interval_seconds = definition["IntervalSeconds"] || 1
  @max_attempts = definition["MaxAttempts"] || 3
  @backoff_rate = definition["BackoffRate"] || 2.0
  @max_delay = definition["MaxDelay"] || 3600 # 1 hour default
end

Instance Attribute Details

#backoff_rateObject (readonly)

Returns the value of attribute backoff_rate.



437
438
439
# File 'lib/ruby_slm/states/task.rb', line 437

def backoff_rate
  @backoff_rate
end

#error_equalsObject (readonly)

Returns the value of attribute error_equals.



437
438
439
# File 'lib/ruby_slm/states/task.rb', line 437

def error_equals
  @error_equals
end

#interval_secondsObject (readonly)

Returns the value of attribute interval_seconds.



437
438
439
# File 'lib/ruby_slm/states/task.rb', line 437

def interval_seconds
  @interval_seconds
end

#max_attemptsObject (readonly)

Returns the value of attribute max_attempts.



437
438
439
# File 'lib/ruby_slm/states/task.rb', line 437

def max_attempts
  @max_attempts
end

Instance Method Details

#matches?(error, attempt) ⇒ Boolean

Returns:

  • (Boolean)


447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
# File 'lib/ruby_slm/states/task.rb', line 447

def matches?(error, attempt)
  return false if attempt >= @max_attempts

  @error_equals.any? do |error_match|
    case error_match
    when "States.ALL"
      true
    when "States.Timeout"
      error.is_a?(TaskTimeoutError)
    when "States.TaskFailed"
      error.is_a?(StandardError) && !error.is_a?(TaskTimeoutError)
    when "States.Permissions"
      error.is_a?(SecurityError) || error.message.include?("permission")
    else
      error.class.name == error_match || error.message.include?(error_match)
    end
  end
end

#validate!Object



466
467
468
469
470
471
472
473
474
475
476
477
478
# File 'lib/ruby_slm/states/task.rb', line 466

def validate!
  if @error_equals.empty?
    raise DefinitionError, "Retry policy must specify ErrorEquals"
  end

  if @interval_seconds < 0
    raise DefinitionError, "IntervalSeconds must be non-negative"
  end

  if @max_attempts < 0
    raise DefinitionError, "MaxAttempts must be non-negative"
  end
end