Sha256: b766b558836eafbebd59689292a6434f4e850c57ee9970ee6af497c7b51b4a0e

Contents?: true

Size: 1.51 KB

Versions: 1

Compression:

Stored size: 1.51 KB

Contents

# frozen_string_literal: true

require 'airbrake/sidekiq/retryable_jobs_filter'

module Airbrake
  module Sidekiq
    # Provides integration with Sidekiq v2+.
    class ErrorHandler
      def call(_worker, context, _queue)
        timing = Airbrake::Benchmark.measure do
          yield
        end
      rescue Exception => exception # rubocop:disable Lint/RescueException
        notify_airbrake(exception, context)
        Airbrake.notify_queue(
          queue: context['class'],
          error_count: 1,
          timing: 0.01,
        )
        raise exception
      else
        Airbrake.notify_queue(
          queue: context['class'],
          error_count: 0,
          timing: timing,
        )
      end

      private

      def notify_airbrake(exception, context)
        Airbrake.notify(exception, job: context) do |notice|
          notice[:context][:component] = 'sidekiq'
          notice[:context][:action] = action(context)
        end
      end

      # @return [String] job's name. When ActiveJob is present, retrieve
      #   job_class. When used directly, use worker's name
      def action(context)
        klass = context['class'] || context[:job] && context[:job]['class']
        return klass unless context[:job] && context[:job]['args'].first.is_a?(Hash)
        return klass unless (job_class = context[:job]['args'].first['job_class'])
        job_class
      end
    end
  end
end

Sidekiq.configure_server do |config|
  config.server_middleware do |chain|
    chain.add(Airbrake::Sidekiq::ErrorHandler)
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
airbrake-10.1.0.rc.1 lib/airbrake/sidekiq.rb