Class: Tasker::Types::BackoffConfig

Inherits:
BaseConfig
  • Object
show all
Defined in:
lib/tasker/types/backoff_config.rb

Overview

Configuration type for retry backoff calculation settings

This configuration exposes previously hardcoded backoff timing constants used in retry logic and exponential backoff calculations.

Examples:

Basic usage

config = BackoffConfig.new(
  max_backoff_seconds: 120
)

With custom backoff progression

config = BackoffConfig.new(
  default_backoff_seconds: [1, 2, 5, 10, 20, 60],
  max_backoff_seconds: 300,
  backoff_multiplier: 2.5,
  jitter_enabled: true,
  jitter_max_percentage: 0.15
)

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from BaseConfig

#initialize

Constructor Details

This class inherits a constructor from Tasker::Types::BaseConfig

Instance Attribute Details

#backoff_multiplierFloat (readonly)

Returns Exponential backoff multiplier.

Returns:

  • (Float)

    Exponential backoff multiplier



54
# File 'lib/tasker/types/backoff_config.rb', line 54

attribute :backoff_multiplier, Types::Float.default(2.0)

#buffer_secondsInteger (readonly)

Returns Buffer time in seconds.

Returns:

  • (Integer)

    Buffer time in seconds



111
# File 'lib/tasker/types/backoff_config.rb', line 111

attribute :buffer_seconds, Types::Integer.default(5)

#default_backoff_secondsArray<Integer> (readonly)

Returns Backoff progression in seconds.

Returns:

  • (Array<Integer>)

    Backoff progression in seconds



35
36
# File 'lib/tasker/types/backoff_config.rb', line 35

attribute :default_backoff_seconds, Types::Array.of(Types::Integer)
.default([1, 2, 4, 8, 16, 32].freeze)

#default_reenqueue_delayInteger (readonly)

Returns Default reenqueue delay in seconds.

Returns:

  • (Integer)

    Default reenqueue delay in seconds



102
# File 'lib/tasker/types/backoff_config.rb', line 102

attribute :default_reenqueue_delay, Types::Integer.default(30)

#jitter_enabledBoolean (readonly)

Returns Whether jitter is enabled.

Returns:

  • (Boolean)

    Whether jitter is enabled



63
# File 'lib/tasker/types/backoff_config.rb', line 63

attribute :jitter_enabled, Types::Bool.default(true)

#jitter_max_percentageFloat (readonly)

Returns Maximum jitter as a decimal percentage.

Returns:

  • (Float)

    Maximum jitter as a decimal percentage



73
# File 'lib/tasker/types/backoff_config.rb', line 73

attribute :jitter_max_percentage, Types::Float.default(0.1)

#max_backoff_secondsInteger (readonly)

Returns Maximum backoff time in seconds.

Returns:

  • (Integer)

    Maximum backoff time in seconds



45
# File 'lib/tasker/types/backoff_config.rb', line 45

attribute :max_backoff_seconds, Types::Integer.default(300)

#reenqueue_delaysHash (readonly)

Returns Mapping of execution status to delay seconds.

Returns:

  • (Hash)

    Mapping of execution status to delay seconds



83
84
85
86
87
88
89
90
91
92
93
# File 'lib/tasker/types/backoff_config.rb', line 83

attribute :reenqueue_delays, Types::Hash.schema(
  has_ready_steps: Types::Integer.default { 0 },              # Steps ready - immediate processing
  waiting_for_dependencies: Types::Integer.default { 45 },    # Waiting for deps - moderate delay
  processing: Types::Integer.default { 10 }                   # Processing - short delay
).constructor { |value|
  value.respond_to?(:deep_symbolize_keys) ? value.deep_symbolize_keys : value
}.default({
  has_ready_steps: 0,
  waiting_for_dependencies: 45,
  processing: 10
}.freeze)

Instance Method Details

#calculate_backoff_seconds(attempt_number) ⇒ Integer

Calculate backoff time for a given attempt

Parameters:

  • attempt_number (Integer)

    The retry attempt number (1-based)

Returns:

  • (Integer)

    Backoff time in seconds



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/tasker/types/backoff_config.rb', line 117

def calculate_backoff_seconds(attempt_number)
  return 0 if attempt_number <= 0

  # Use predefined progression if available
  base_backoff = if attempt_number <= default_backoff_seconds.length
                   default_backoff_seconds[attempt_number - 1]
                 else
                   # Use exponential backoff for attempts beyond predefined progression
                   (attempt_number**backoff_multiplier).to_i
                 end

  # Apply maximum limit
  backoff_time = [base_backoff, max_backoff_seconds].min

  # Apply jitter if enabled
  if jitter_enabled
    jitter_range = (backoff_time * jitter_max_percentage).round
    jitter = Random.rand(-jitter_range..jitter_range)
    backoff_time = [backoff_time + jitter, 1].max # Ensure minimum 1 second
  end

  backoff_time
end