Sha256: ca14b37de0ce3f2385d8d6f8418ddfd4325b5ed535e387e3c73a894a1d155606

Contents?: true

Size: 1.95 KB

Versions: 7

Compression:

Stored size: 1.95 KB

Contents

require 'delayed_job'
require 'rollbar/plugins/delayed_job/job_data'

module Rollbar
  module Delayed
    class << self
      attr_accessor :wrapped
    end

    class RollbarPlugin < ::Delayed::Plugin
      callbacks do |lifecycle|
        lifecycle.around(:invoke_job, &Delayed.invoke_job_callback)
        lifecycle.after(:failure) do |_, job, _, _|
          data = Rollbar::Delayed.build_job_data(job)

          # DelayedJob < 4.1 doesn't provide job#error
          if job.class.method_defined? :error
            if job.error
              ::Rollbar.scope(:request => data)
                       .error(job.error, :use_exception_level_filters => true)
            end
          elsif job.last_error
            ::Rollbar.scope(:request => data).error(
              "Job has failed and won't be retried anymore: #{job.last_error}",
              :use_exception_level_filters => true
            )
          end
        end
      end
    end

    self.wrapped = false

    def self.wrap_worker
      return if wrapped

      ::Delayed::Worker.plugins << RollbarPlugin

      self.wrapped = true
    end

    def self.wrap_worker!
      self.wrapped = false

      wrap_worker
    end

    def self.invoke_job_callback
      proc do |job, *args, &block|
        begin
          block.call(job, *args)
        rescue StandardError => e
          report(e, job)

          raise e
        end
      end
    end

    def self.report(e, job)
      return if skip_report?(job)

      data = build_job_data(job)

      ::Rollbar.scope(:request => data)
               .error(e, :use_exception_level_filters => true)
    end

    def self.skip_report?(job)
      handler = ::Rollbar.configuration.async_skip_report_handler

      return handler.call(job) if handler.respond_to?(:call)

      job.attempts < ::Rollbar.configuration.dj_threshold
    end

    def self.build_job_data(job)
      return nil unless ::Rollbar.configuration.report_dj_data

      JobData.new(job).to_hash
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
rollbar-3.4.2 lib/rollbar/plugins/delayed_job/plugin.rb
rollbar-3.4.1 lib/rollbar/plugins/delayed_job/plugin.rb
rollbar-3.4.0 lib/rollbar/plugins/delayed_job/plugin.rb
rollbar-3.3.3 lib/rollbar/plugins/delayed_job/plugin.rb
rollbar-3.3.2 lib/rollbar/plugins/delayed_job/plugin.rb
rollbar-3.3.1 lib/rollbar/plugins/delayed_job/plugin.rb
rollbar-3.3.0 lib/rollbar/plugins/delayed_job/plugin.rb