Sha256: 35630eb2dfdfe4e75ab9181952ff430cc7e2af864e0f9d62df6066635b7a23e6

Contents?: true

Size: 1.46 KB

Versions: 9

Compression:

Stored size: 1.46 KB

Contents

require 'delayed_job'

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

    class JobData
      attr_reader :job

      def initialize(job)
        @job = job
      end

      def to_hash
        job_data = job.as_json
        # Here job_data['handler'] is a YAML object comming
        # from the storage backend
        job_data['handler'] = job.payload_object.as_json

        job_data
      end
    end

    class RollbarPlugin < ::Delayed::Plugin
      callbacks do |lifecycle|
        lifecycle.around(:invoke_job, &Delayed::invoke_job_callback)
      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 => 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)
      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

9 entries across 9 versions & 1 rubygems

Version Path
rollbar-2.10.0 lib/rollbar/delayed_job.rb
rollbar-2.9.1 lib/rollbar/delayed_job.rb
rollbar-2.9.0 lib/rollbar/delayed_job.rb
rollbar-2.8.3 lib/rollbar/delayed_job.rb
rollbar-2.8.2 lib/rollbar/delayed_job.rb
rollbar-2.8.1 lib/rollbar/delayed_job.rb
rollbar-2.8.0 lib/rollbar/delayed_job.rb
rollbar-2.7.1 lib/rollbar/delayed_job.rb
rollbar-2.7.0 lib/rollbar/delayed_job.rb