GoodJob: Ensure you get notified about background exceptions

Updated . Posted . Visible to the public. Repeats.

GoodJob and ActiveJob rescue exceptions internally, preventing exception_notification Show archive.org snapshot from triggering. This can cause silent job failures.To get notified, subscribe to ActiveJob events and configure GoodJob's on_thread_error hook. This lets you manually call your exception notifier for every retry, discard, or internal GoodJob error.

# config/initializers/good_job.rb

# Manually notify on job failures, as they are handled internally by ActiveJob/GoodJob.
ActiveSupport::Notifications.subscribe(/(enqueue_retry|retry_stopped|discard)\.active_job/) do |event_name, *, payload|
  exception = payload[:error]
  job = payload[:job]

  url = begin
    job_path = GoodJob::Engine.routes.url_helpers.job_path(job)
    URI.join(Router.instance.root_url, job_path).to_s
  rescue StandardError; end

  ExceptionNotifier.notify_exception(
    exception,
    data: {
      event_name: event_name,
      job_id: job.job_id,
      executions: job.executions,
      queue_name: job.queue_name,
      url: url,
    },
  )
end

Rails.application.configure do
  # Also capture errors not handled by Active Job
  config.good_job.on_thread_error = ExceptionNotifier.method(:notify_exception)
end

Then, update your exception_notification configuration to render the custom data payload. This makes useful context, like a direct URL to the failed job, visible in the notification.

# config/initializers/exception_notification.rb

ExceptionNotification.configure do |config|
  # ...
  config.add_notifier :email, {
    # ...
    background_sections: %w[backtrace data],
  }
end
Profile picture of Michael Leimstädtner
Michael Leimstädtner
Last edit
Dominik Schöler
License
Source code in this card is licensed under the MIT License.
Posted by Michael Leimstädtner to makandra dev (2025-09-18 10:56)