Class: Tasker::Orchestration::BackoffCalculator

Inherits:
Object
  • Object
show all
Includes:
Concerns::EventPublisher
Defined in:
lib/tasker/orchestration/backoff_calculator.rb

Overview

BackoffCalculator handles all backoff logic for API retries

This component provides unified handling of both server-requested backoff (via Retry-After headers) and exponential backoff calculations. It publishes appropriate events for observability.

Instance Method Summary collapse

Methods included from Concerns::EventPublisher

#infer_step_event_type_from_state, #publish_custom_event, #publish_no_viable_steps, #publish_step_backoff, #publish_step_before_handle, #publish_step_cancelled, #publish_step_completed, #publish_step_event_for_context, #publish_step_failed, #publish_step_retry_requested, #publish_step_started, #publish_steps_execution_completed, #publish_steps_execution_started, #publish_task_completed, #publish_task_enqueue, #publish_task_failed, #publish_task_finalization_completed, #publish_task_finalization_started, #publish_task_pending_transition, #publish_task_reenqueue_delayed, #publish_task_reenqueue_failed, #publish_task_reenqueue_requested, #publish_task_reenqueue_started, #publish_task_retry_requested, #publish_task_started, #publish_viable_steps_discovered, #publish_workflow_state_unclear, #publish_workflow_step_completed, #publish_workflow_task_started

Constructor Details

#initialize(config: nil, retry_parser: RetryHeaderParser.new) ⇒ BackoffCalculator

Initialize the backoff calculator

Parameters:

  • config (Object) (defaults to: nil)

    Configuration object with backoff settings

  • retry_parser (RetryHeaderParser) (defaults to: RetryHeaderParser.new)

    Parser for Retry-After headers (injectable for testing)



20
21
22
23
# File 'lib/tasker/orchestration/backoff_calculator.rb', line 20

def initialize(config: nil, retry_parser: RetryHeaderParser.new)
  @config = config
  @retry_parser = retry_parser
end

Instance Method Details

#calculate_and_apply_backoff(step, context) ⇒ Object

Calculate and apply backoff for a step based on response context

This method determines whether to use server-requested backoff or exponential backoff, calculates the appropriate delay, and updates the step with backoff information.

Parameters:

  • step (Tasker::WorkflowStep)

    The step requiring backoff

  • context (Hash)

    Response context containing headers and metadata



33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/tasker/orchestration/backoff_calculator.rb', line 33

def calculate_and_apply_backoff(step, context)
  retry_after = extract_retry_after_header(context)

  if retry_after.present?
    apply_server_requested_backoff(step, retry_after)
  elsif backoff_enabled?
    apply_exponential_backoff(step, context)
  else
    Rails.logger.warn(
      "BackoffCalculator: No backoff strategy available for step #{step.name} (#{step.workflow_step_id})"
    )
  end
end