Class: Temporalio::RetryPolicy

Inherits:
Object
  • Object
show all
Defined in:
lib/temporalio/retry_policy.rb

Overview

Options for retrying workflows and activities.

Defined Under Namespace

Classes: Invalid

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(initial_interval: 1, backoff: 2.0, max_interval: nil, max_attempts: 0, non_retriable_errors: []) ⇒ RetryPolicy

Returns a new instance of RetryPolicy.

Parameters:

  • initial_interval (Integer) (defaults to: 1)

    Backoff interval (in seconds) for the first retry.

  • backoff (Float) (defaults to: 2.0)

    Coefficient to multiply previous backoff interval by to get new interval.

  • max_interval (Integer) (defaults to: nil)

    Maximum backoff interval between retries. Default 100x #initial_interval.

  • max_attempts (Integer) (defaults to: 0)

    Maximum number of attempts. If 0, there is no maximum.

  • non_retriable_errors (Array<String>) (defaults to: [])

    List of error types that are not retryable.



34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/temporalio/retry_policy.rb', line 34

def initialize(
  initial_interval: 1,
  backoff: 2.0,
  max_interval: nil,
  max_attempts: 0,
  non_retriable_errors: []
)
  @initial_interval = initial_interval
  @backoff = backoff
  @max_interval = max_interval
  @max_attempts = max_attempts
  @non_retriable_errors = non_retriable_errors
end

Instance Attribute Details

#backoffFloat (readonly)

Returns Coefficient to multiply previous backoff interval by to get new interval.

Returns:

  • (Float)

    Coefficient to multiply previous backoff interval by to get new interval.



15
16
17
# File 'lib/temporalio/retry_policy.rb', line 15

def backoff
  @backoff
end

#initial_intervalInteger (readonly)

Returns Backoff interval for the first retry.

Returns:

  • (Integer)

    Backoff interval for the first retry.



12
13
14
# File 'lib/temporalio/retry_policy.rb', line 12

def initial_interval
  @initial_interval
end

#max_attemptsInteger (readonly)

Returns Maximum number of attempts. If 0, the default, there is no maximum.

Returns:

  • (Integer)

    Maximum number of attempts. If 0, the default, there is no maximum.



22
23
24
# File 'lib/temporalio/retry_policy.rb', line 22

def max_attempts
  @max_attempts
end

#max_intervalInteger? (readonly)

Returns Maximum backoff interval between retries. Default 100x #initial_interval.

Returns:

  • (Integer, nil)

    Maximum backoff interval between retries. Default 100x #initial_interval.



19
20
21
# File 'lib/temporalio/retry_policy.rb', line 19

def max_interval
  @max_interval
end

#non_retriable_errorsArray<String> (readonly)

Returns List of error types that are not retryable.

Returns:

  • (Array<String>)

    List of error types that are not retryable.



25
26
27
# File 'lib/temporalio/retry_policy.rb', line 25

def non_retriable_errors
  @non_retriable_errors
end

Instance Method Details

#to_protoObject



72
73
74
75
76
77
78
79
80
# File 'lib/temporalio/retry_policy.rb', line 72

def to_proto
  Temporalio::Api::Common::V1::RetryPolicy.new(
    initial_interval: Google::Protobuf::Duration.new(seconds: initial_interval),
    backoff_coefficient: backoff,
    maximum_interval: max_interval ? Google::Protobuf::Duration.new(seconds: max_interval) : nil,
    maximum_attempts: max_attempts,
    non_retryable_error_types: non_retriable_errors.map(&:name).compact,
  )
end

#validate!Object

Raises:



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/temporalio/retry_policy.rb', line 48

def validate!
  # Retries disabled
  return if max_attempts == 1

  # Maximum attempts
  raise Invalid, 'Maximum attempts must be specified' unless max_attempts
  raise Invalid, 'Maximum attempts cannot be negative' if max_attempts.negative?

  # Initial interval
  raise Invalid, 'Initial interval must be specified' unless initial_interval
  raise Invalid, 'Initial interval cannot be negative' if initial_interval.negative?
  raise Invalid, 'Initial interval must be in whole seconds' unless initial_interval.is_a?(Integer)

  # Backoff coefficient
  raise Invalid, 'Backoff coefficient must be specified' unless backoff
  raise Invalid, 'Backoff coefficient cannot be less than 1' if backoff < 1

  # Maximum interval
  if max_interval
    raise Invalid, 'Maximum interval cannot be negative' if max_interval.negative?
    raise Invalid, 'Maximum interval cannot be less than initial interval' if max_interval < initial_interval
  end
end