Class: ScoutApm::BackgroundJobIntegrations::DelayedJob

Inherits:
Object
  • Object
show all
Defined in:
lib/scout_apm/background_job_integrations/delayed_job.rb

Constant Summary collapse

ACTIVE_JOB_KLASS =
'ActiveJob::QueueAdapters::DelayedJobAdapter::JobWrapper'.freeze
DJ_PERFORMABLE_METHOD =
'Delayed::PerformableMethod'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#loggerObject (readonly)

Returns the value of attribute logger.



7
8
9
# File 'lib/scout_apm/background_job_integrations/delayed_job.rb', line 7

def logger
  @logger
end

Instance Method Details

#forking?Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/scout_apm/background_job_integrations/delayed_job.rb', line 17

def forking?
  false
end

#installObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# 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

#nameObject



9
10
11
# File 'lib/scout_apm/background_job_integrations/delayed_job.rb', line 9

def name
  :delayed_job
end

#present?Boolean

Returns:

  • (Boolean)


13
14
15
# File 'lib/scout_apm/background_job_integrations/delayed_job.rb', line 13

def present?
  defined?(::Delayed::Job)
end