Class: Desiru::Jobs::RetryStrategies::RetryPolicy

Inherits:
Object
  • Object
show all
Defined in:
lib/desiru/jobs/retry_strategies.rb

Overview

Custom retry policy

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(max_retries: 5, retry_strategy: ExponentialBackoff.new, retriable_errors: nil, non_retriable_errors: nil) ⇒ RetryPolicy

Returns a new instance of RetryPolicy.



66
67
68
69
70
71
72
73
74
75
76
# File 'lib/desiru/jobs/retry_strategies.rb', line 66

def initialize(
  max_retries: 5,
  retry_strategy: ExponentialBackoff.new,
  retriable_errors: nil,
  non_retriable_errors: nil
)
  @max_retries = max_retries
  @retry_strategy = retry_strategy
  @retriable_errors = Array(retriable_errors) if retriable_errors
  @non_retriable_errors = Array(non_retriable_errors) if non_retriable_errors
end

Instance Attribute Details

#max_retriesObject (readonly)

Returns the value of attribute max_retries.



64
65
66
# File 'lib/desiru/jobs/retry_strategies.rb', line 64

def max_retries
  @max_retries
end

#non_retriable_errorsObject (readonly)

Returns the value of attribute non_retriable_errors.



64
65
66
# File 'lib/desiru/jobs/retry_strategies.rb', line 64

def non_retriable_errors
  @non_retriable_errors
end

#retriable_errorsObject (readonly)

Returns the value of attribute retriable_errors.



64
65
66
# File 'lib/desiru/jobs/retry_strategies.rb', line 64

def retriable_errors
  @retriable_errors
end

#retry_strategyObject (readonly)

Returns the value of attribute retry_strategy.



64
65
66
# File 'lib/desiru/jobs/retry_strategies.rb', line 64

def retry_strategy
  @retry_strategy
end

Instance Method Details

#retriable?(error) ⇒ Boolean

Check if error is retriable

Returns:

  • (Boolean)


79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/desiru/jobs/retry_strategies.rb', line 79

def retriable?(error)
  # If non-retriable errors are specified, check those first
  return false if non_retriable_errors&.any? { |klass| error.is_a?(klass) }

  # If retriable errors are specified, only retry those
  if retriable_errors
    # Only retry if the error matches one of the specified retriable errors
    retriable_errors.any? { |klass| error.is_a?(klass) }
  else
    # By default, retry all errors except non-retriable ones
    true
  end
end

#retry_delay(retry_count) ⇒ Object

Get delay for the current retry



99
100
101
# File 'lib/desiru/jobs/retry_strategies.rb', line 99

def retry_delay(retry_count)
  retry_strategy.delay_for(retry_count)
end

#should_retry?(retry_count, error) ⇒ Boolean

Check if we should retry based on count

Returns:

  • (Boolean)


94
95
96
# File 'lib/desiru/jobs/retry_strategies.rb', line 94

def should_retry?(retry_count, error)
  retry_count < max_retries && retriable?(error)
end