Class: Axn::Configuration

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

Constant Summary collapse

ASYNC_EXCEPTION_REPORTING_OPTIONS =

Controls when on_exception is triggered in async context (Sidekiq/ActiveJob). Options:

:every_attempt - trigger on every retry attempt (includes retry context)
:first_and_exhausted - trigger on first attempt and when retries exhausted (default)
:only_exhausted - only trigger when retries exhausted (via death handler)
%i[every_attempt first_and_exhausted only_exhausted].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#_include_retry_command_in_exceptionsObject

EXPERIMENTAL: When true, automatically generates a retry command in exception context. This is marked experimental because the retry command generation may not work well for all action types (e.g., actions with complex object dependencies).



46
47
48
# File 'lib/axn/configuration.rb', line 46

def _include_retry_command_in_exceptions
  @_include_retry_command_in_exceptions.nil? ? false : @_include_retry_command_in_exceptions
end

#additional_includesObject



41
# File 'lib/axn/configuration.rb', line 41

def additional_includes = @additional_includes ||= []

#async_max_retriesObject

Optional override for max retries across all async jobs. When nil (default), each adapter uses its own default (Sidekiq: 25, ActiveJob: 5). When explicitly set, this value overrides the adapter’s default for retry context tracking.



37
38
39
# File 'lib/axn/configuration.rb', line 37

def async_max_retries
  @async_max_retries
end

#emit_metricsObject

Returns the value of attribute emit_metrics.



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

def emit_metrics
  @emit_metrics
end

#envObject



117
118
119
120
# File 'lib/axn/configuration.rb', line 117

def env
  @env ||= ENV["RACK_ENV"].presence || ENV["RAILS_ENV"].presence || "development"
  ActiveSupport::StringInquirer.new(@env)
end

#log_levelObject



39
# File 'lib/axn/configuration.rb', line 39

def log_level = @log_level ||= :info

#loggerObject



102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/axn/configuration.rb', line 102

def logger
  @logger ||= begin
    # Use sidekiq logger if in background
    if Axn::Util::ExecutionContext.background? && defined?(Sidekiq)
      Sidekiq.logger
    else
      Rails.logger
    end
  rescue NameError
    Logger.new($stdout).tap do |l|
      l.level = Logger::INFO
    end
  end
end

#on_exception(e, action:, context: {}) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/axn/configuration.rb', line 82

def on_exception(e, action:, context: {})
  if action.respond_to?(:result) && action.result.respond_to?(:error)
    resolved_error = action.result.error
    # Compare with the default fallback message instead of calling default_error
    # to avoid triggering error message resolution multiple times
    detail = resolved_error == Axn::Core::Flow::Handlers::Resolvers::MessageResolver::DEFAULT_ERROR ? e.message : resolved_error
  else
    detail = e.message
  end

  msg = "Handled exception (#{e.class.name}): #{detail}"
  msg = ("#" * 10) + " #{msg} " + ("#" * 10) unless Axn.config.env.production?
  action.log(msg)

  return unless @on_exception

  # Only pass the args and kwargs that the given block expects
  Axn::Internal::Callable.call_with_desired_shape(@on_exception, args: [e], kwargs: { action:, context: })
end

#railsObject



80
# File 'lib/axn/configuration.rb', line 80

def rails = @rails ||= RailsConfiguration.new

#raise_piping_errors_in_devObject

Returns the value of attribute raise_piping_errors_in_dev.



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

def raise_piping_errors_in_dev
  @raise_piping_errors_in_dev
end

Instance Method Details

#_default_async_adapterObject



50
# File 'lib/axn/configuration.rb', line 50

def _default_async_adapter = @default_async_adapter ||= false

#_default_async_configObject



51
# File 'lib/axn/configuration.rb', line 51

def _default_async_config = @default_async_config ||= {}

#_default_async_config_blockObject



52
# File 'lib/axn/configuration.rb', line 52

def _default_async_config_block = @default_async_config_block

#_enqueue_all_async_adapterObject

Async configuration for EnqueueAllOrchestrator (used by enqueue_all_async) Defaults to the default async config if not explicitly set



67
# File 'lib/axn/configuration.rb', line 67

def _enqueue_all_async_adapter = @enqueue_all_async_adapter || _default_async_adapter

#_enqueue_all_async_configObject



68
# File 'lib/axn/configuration.rb', line 68

def _enqueue_all_async_config = @enqueue_all_async_config || _default_async_config

#_enqueue_all_async_config_blockObject



69
# File 'lib/axn/configuration.rb', line 69

def _enqueue_all_async_config_block = @enqueue_all_async_config_block || _default_async_config_block

#async_exception_reportingObject



19
20
21
# File 'lib/axn/configuration.rb', line 19

def async_exception_reporting
  @async_exception_reporting ||= :first_and_exhausted
end

#async_exception_reporting=(value) ⇒ Object



23
24
25
26
27
28
29
30
31
32
# File 'lib/axn/configuration.rb', line 23

def async_exception_reporting=(value)
  unless ASYNC_EXCEPTION_REPORTING_OPTIONS.include?(value)
    raise ArgumentError, "async_exception_reporting must be one of: #{ASYNC_EXCEPTION_REPORTING_OPTIONS.join(', ')}"
  end

  @async_exception_reporting = value

  # Auto-register Sidekiq middleware/death handler if needed and Sidekiq is available
  _auto_configure_sidekiq_for_async_exception_reporting(value)
end

#set_default_async(adapter = false, **config, &block) ⇒ Object

rubocop:disable Style/OptionalBooleanParameter

Raises:

  • (ArgumentError)


54
55
56
57
58
59
60
61
62
63
# File 'lib/axn/configuration.rb', line 54

def set_default_async(adapter = false, **config, &block) # rubocop:disable Style/OptionalBooleanParameter
  raise ArgumentError, "Cannot set default async adapter to nil as it would cause infinite recursion" if adapter.nil?

  @default_async_adapter = adapter unless adapter.nil?
  @default_async_config = config.any? ? config : {}
  @default_async_config_block = block_given? ? block : nil

  _ensure_async_exception_reporting_registered_for_adapter(adapter)
  _apply_async_to_enqueue_all_orchestrator
end

#set_enqueue_all_async(adapter, **config, &block) ⇒ Object



71
72
73
74
75
76
77
78
# File 'lib/axn/configuration.rb', line 71

def set_enqueue_all_async(adapter, **config, &block)
  @enqueue_all_async_adapter = adapter
  @enqueue_all_async_config = config.any? ? config : {}
  @enqueue_all_async_config_block = block_given? ? block : nil

  _ensure_async_exception_reporting_registered_for_adapter(adapter)
  _apply_async_to_enqueue_all_orchestrator
end