Class: RailsErrorDashboard::AsyncErrorLoggingJob

Inherits:
ApplicationJob
  • Object
show all
Defined in:
app/jobs/rails_error_dashboard/async_error_logging_job.rb

Overview

Background job for asynchronous error logging This prevents error logging from blocking the main request/response cycle

Instance Method Summary collapse

Instance Method Details

#perform(exception_data, context) ⇒ Object

Performs async error logging

Parameters:

  • exception_data (Hash)

    Serialized exception data

  • context (Hash)

    Error context (request, user, etc.)



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'app/jobs/rails_error_dashboard/async_error_logging_job.rb', line 12

def perform(exception_data, context)
  # Normalize string keys (ActiveJob may deserialize with string keys)
  exception_data = exception_data.symbolize_keys if exception_data.respond_to?(:symbolize_keys)
  context = context.symbolize_keys if context.respond_to?(:symbolize_keys)

  # Reconstruct the exception from serialized data
  exception = reconstruct_exception(exception_data)

  # Pass pre-extracted cause chain via context so LogError can use it
  # (reconstructed exceptions don't have Ruby's built-in cause set)
  if exception_data[:cause_chain]
    context[:_serialized_cause_chain] = exception_data[:cause_chain]
  end

  # Log the error synchronously in the background job
  # Call .new().call to bypass async check (we're already async)
  Commands::LogError.new(exception, context).call
rescue => e
  # Don't let async job errors break the job queue
  Rails.logger.error("AsyncErrorLoggingJob failed: #{e.message}")
  Rails.logger.error("Backtrace: #{e.backtrace&.first(5)&.join("\n")}")
end