module Bugsnag::Rails::ActiveJob

Constants

EXISTING_INTEGRATIONS
INLINE_ADAPTER
MAYBE_MISSING_METHODS

these methods were added after the first Active Job release so may not be present, depending on the Rails version

SEVERITY
SEVERITY_REASON

Public Class Methods

included(base) click to toggle source
# File lib/bugsnag/integrations/rails/active_job.rb, line 29
def self.included(base)
  base.class_eval do
    around_perform do |job, block|
      adapter = _bugsnag_get_adapter_name(job)

      # if we have an integration for this queue adapter already then we should
      # leave this job alone or we'll end up with duplicate metadata
      next block.call if EXISTING_INTEGRATIONS.include?(adapter)

      Bugsnag.configuration.detected_app_type = 'active job'

      begin
        Bugsnag.configuration.set_request_data(:active_job, _bugsnag_extract_metadata(job))

        block.call
      rescue Exception => e
        Bugsnag.notify(e, true) do |report|
          report.severity = SEVERITY
          report.severity_reason = SEVERITY_REASON
        end

        # when using the "inline" adapter the job is run immediately, which
        # will result in our Rack integration catching the re-raised error
        # and reporting it a second time if it's run in a web request
        if adapter == INLINE_ADAPTER
          e.instance_eval do
            def skip_bugsnag
              true
            end
          end
        end

        raise
      ensure
        Bugsnag.configuration.clear_request_data
      end
    end
  end
end

Public Instance Methods

skip_bugsnag() click to toggle source
# File lib/bugsnag/integrations/rails/active_job.rb, line 55
def skip_bugsnag
  true
end

Private Instance Methods

_bugsnag_extract_metadata(job) click to toggle source
# File lib/bugsnag/integrations/rails/active_job.rb, line 81
def _bugsnag_extract_metadata(job)
  metadata = {
    job_id: job.job_id,
    job_name: job.class.name,
    queue: job.queue_name,
    arguments: job.arguments,
    locale: job.locale
  }

  MAYBE_MISSING_METHODS.each do |method_name|
    next unless job.respond_to?(method_name)

    metadata[method_name] = job.send(method_name)
  end

  metadata.compact!
  metadata
end
_bugsnag_get_adapter_name(job) click to toggle source
# File lib/bugsnag/integrations/rails/active_job.rb, line 71
def _bugsnag_get_adapter_name(job)
  adapter = job.class.queue_adapter

  # in Rails 4 queue adapters were references to a class. In Rails 5+
  # they are an instance of that class instead
  return adapter.name if adapter.is_a?(Class)

  adapter.class.name
end