Class: Axn::Async::RetryContext

Inherits:
Object
  • Object
show all
Defined in:
lib/axn/async/retry_context.rb

Overview

Holds retry context information for async job execution. This context is used to determine when on_exception should be triggered and to provide retry information to exception handlers.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(adapter:, attempt:, max_retries:, job_id: nil) ⇒ RetryContext

Returns a new instance of RetryContext.



11
12
13
14
15
16
# File 'lib/axn/async/retry_context.rb', line 11

def initialize(adapter:, attempt:, max_retries:, job_id: nil)
  @adapter = adapter
  @attempt = attempt
  @max_retries = max_retries
  @job_id = job_id
end

Instance Attribute Details

#adapterObject (readonly)

Returns the value of attribute adapter.



9
10
11
# File 'lib/axn/async/retry_context.rb', line 9

def adapter
  @adapter
end

#attemptObject (readonly)

Returns the value of attribute attempt.



9
10
11
# File 'lib/axn/async/retry_context.rb', line 9

def attempt
  @attempt
end

#job_idObject (readonly)

Returns the value of attribute job_id.



9
10
11
# File 'lib/axn/async/retry_context.rb', line 9

def job_id
  @job_id
end

#max_retriesObject (readonly)

Returns the value of attribute max_retries.



9
10
11
# File 'lib/axn/async/retry_context.rb', line 9

def max_retries
  @max_retries
end

Instance Method Details

#first_attempt?Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/axn/async/retry_context.rb', line 18

def first_attempt?
  attempt == 1
end

#retries_exhausted?Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/axn/async/retry_context.rb', line 22

def retries_exhausted?
  attempt > max_retries
end

#should_trigger_on_exception?(config_mode = nil, from_exhaustion_handler: false) ⇒ Boolean

Determines if on_exception should be triggered based on config and retry state.

For :first_and_exhausted and :only_exhausted modes, exhaustion reporting is handled by:

  • Sidekiq: Death handler (calls this with from_exhaustion_handler: true)

  • ActiveJob: after_discard callback (calls this with from_exhaustion_handler: true) Note: ActiveJob adapter requires Rails 7.1+ for these modes (raises error on older Rails)

Parameters:

  • config_mode (Symbol, nil) (defaults to: nil)

    The exception reporting mode. If nil, falls back to global config.

  • from_exhaustion_handler (Boolean) (defaults to: false)

    if true, called from exhaustion/discard handler

Returns:

  • (Boolean)


35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/axn/async/retry_context.rb', line 35

def should_trigger_on_exception?(config_mode = nil, from_exhaustion_handler: false)
  # Fall back to global config when no per-class override is set
  resolved_mode = config_mode || Axn.config.async_exception_reporting

  case resolved_mode
  when :first_and_exhausted
    # Regular flow reports on first attempt only.
    # Exhaustion handler reports only when retries were exhausted after multiple attempts,
    # NOT when job was discarded on first attempt (perform already reported).
    if from_exhaustion_handler
      !first_attempt? # avoid double-report when job discarded on first attempt
    else
      first_attempt?
    end
  when :only_exhausted
    # Only exhaustion handler should report
    from_exhaustion_handler
  else
    # :every_attempt and unknown modes default to triggering
    true
  end
end

#to_hObject



58
59
60
61
62
63
64
65
66
67
# File 'lib/axn/async/retry_context.rb', line 58

def to_h
  {
    adapter:,
    attempt:,
    max_retries:,
    job_id:,
    first_attempt: first_attempt?,
    retries_exhausted: retries_exhausted?,
  }.compact
end