Sha256: 8eff88f21ae62bbf0a6429e1c51780cd30a43c641b54acf0123bd9d9c522adc6

Contents?: true

Size: 1.98 KB

Versions: 9

Compression:

Stored size: 1.98 KB

Contents

# frozen_string_literal: true

module Delayed
  module Plugins
    # Provides integration with Delayed Job.
    # rubocop:disable Metrics/BlockLength
    class Airbrake < ::Delayed::Plugin
      callbacks do |lifecycle|
        lifecycle.around(:invoke_job) do |job, *args, &block|
          begin
            timing = ::Airbrake::Benchmark.measure do
              # Forward the call to the next callback in the callback chain
              block.call(job, *args)
            end
          rescue Exception => exception # rubocop:disable Lint/RescueException
            params = job.as_json

            # If DelayedJob is used through ActiveJob, it contains extra info.
            if job.payload_object.respond_to?(:job_data)
              params[:active_job] = job.payload_object.job_data
              job_class = job.payload_object.job_data['job_class']
            end

            action = job_class || job.payload_object.class.name

            ::Airbrake.notify(exception, params) do |notice|
              notice[:context][:component] = 'delayed_job'
              notice[:context][:action] = action
            end

            ::Airbrake.notify_queue(
              queue: action,
              error_count: 1,
              timing: 0.01,
            )

            raise exception
          else
            ::Airbrake.notify_queue(
              queue: job_class || job.payload_object.class.name,
              error_count: 0,
              timing: timing,
            )
          end
        end
      end
    end
    # rubocop:enable Metrics/BlockLength
  end
end

if RUBY_ENGINE == 'jruby' && defined?(Delayed::Backend::ActiveRecord::Job)
  # Workaround against JRuby bug:
  # https://github.com/jruby/jruby/issues/3338
  # rubocop:disable Style/ClassAndModuleChildren
  class Delayed::Backend::ActiveRecord::Job
    alias old_to_ary to_ary

    def to_ary
      old_to_ary || [self]
    end
  end
  # rubocop:enable Style/ClassAndModuleChildren
end

Delayed::Worker.plugins << Delayed::Plugins::Airbrake

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
airbrake-11.0.3 lib/airbrake/delayed_job.rb
airbrake-11.0.2 lib/airbrake/delayed_job.rb
airbrake-11.0.1 lib/airbrake/delayed_job.rb
airbrake-10.0.6 lib/airbrake/delayed_job.rb
airbrake-11.0.0 lib/airbrake/delayed_job.rb
airbrake-10.1.0.rc.1 lib/airbrake/delayed_job.rb
airbrake-10.0.5 lib/airbrake/delayed_job.rb
airbrake-10.0.4 lib/airbrake/delayed_job.rb
airbrake-10.0.3 lib/airbrake/delayed_job.rb