class ScoutApm::BackgroundJobIntegrations::DelayedJob

Constants

ACTIVE_JOB_KLASS
DJ_PERFORMABLE_METHOD

Attributes

logger[R]

Public Instance Methods

forking?() click to toggle source
# File lib/scout_apm/background_job_integrations/delayed_job.rb, line 17
def forking?
  false
end
install() click to toggle source
# File lib/scout_apm/background_job_integrations/delayed_job.rb, line 21
def install
  plugin = Class.new(Delayed::Plugin) do
    require 'delayed_job'

    callbacks do |lifecycle|
      lifecycle.around(:invoke_job) do |job, *args, &block|
        ScoutApm::Agent.instance.start_background_worker unless ScoutApm::Agent.instance.background_worker_running?

        name = begin
                 case job.payload_object.class.to_s

                 # ActiveJob's class wraps the actual job class
                 when ACTIVE_JOB_KLASS
                   job.payload_object.job_data["job_class"]

                 # An adhoc job, called like `@user.delay.fib(10)`.
                 # returns a string like "User#fib"
                 when DJ_PERFORMABLE_METHOD
                   job.name

                 # A "real" job called like `Delayed::Job.enqueue(MyJob.new)`
                 # returns "MyJob"
                 else
                   job.payload_object.class.to_s
                 end
               rescue
                 # Fall back to whatever DJ thinks the name is.
                 job.name
               end

        queue = job.queue || "default"

        req = ScoutApm::RequestManager.lookup

        begin
          latency = Time.now - [job.created_at, job.run_at].max
          req.annotate_request(:queue_latency => latency)
        rescue
        end

        queue_layer = ScoutApm::Layer.new('Queue', queue)
        job_layer = ScoutApm::Layer.new('Job', name)

        begin
          req.start_layer(queue_layer)
          started_queue = true
          req.start_layer(job_layer)
          started_job = true

          # Call the job itself.
          block.call(job, *args)
        rescue
          req.error!
          raise
        ensure
          req.stop_layer if started_job
          req.stop_layer if started_queue
        end
      end
    end
  end

  Delayed::Worker.plugins << plugin # ScoutApm::BackgroundJobIntegrations::DelayedJobPlugin
end
name() click to toggle source
# File lib/scout_apm/background_job_integrations/delayed_job.rb, line 9
def name
  :delayed_job
end
present?() click to toggle source
# File lib/scout_apm/background_job_integrations/delayed_job.rb, line 13
def present?
  defined?(::Delayed::Worker)
end