Class: ScoutApm::BackgroundJobIntegrations::DelayedJob
- Inherits:
-
Object
- Object
- ScoutApm::BackgroundJobIntegrations::DelayedJob
- 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
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
Instance Method Summary collapse
Instance Attribute Details
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
8 9 10 |
# File 'lib/scout_apm/background_job_integrations/delayed_job.rb', line 8 def logger @logger end |
Instance Method Details
#forking? ⇒ Boolean
18 19 20 |
# File 'lib/scout_apm/background_job_integrations/delayed_job.rb', line 18 def forking? false end |
#install ⇒ Object
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 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/scout_apm/background_job_integrations/delayed_job.rb', line 22 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 Exception => exception # Capture the error for further processing and shipping req.error! # Abusing this key to pass job info params_key = 'action_dispatch.request.parameters' env = {} # Get job data safely - check for job_data first (ActiveJob), then fall back to args (PerformableMethod) env[params_key] = if job.payload_object.respond_to?(:job_data) job.payload_object.job_data elsif job.payload_object.respond_to?(:args) # For PerformableMethod, create a hash with relevant info { 'args' => job.payload_object.args, 'method_name' => job.payload_object.method_name, 'object' => job.payload_object.object.class.to_s } else {} end env[:custom_controller] = name env[:custom_action] = queue context = ScoutApm::Agent.instance.context context.error_buffer.capture(exception, env) 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 ⇒ Object
10 11 12 |
# File 'lib/scout_apm/background_job_integrations/delayed_job.rb', line 10 def name :delayed_job end |
#present? ⇒ Boolean
14 15 16 |
# File 'lib/scout_apm/background_job_integrations/delayed_job.rb', line 14 def present? defined?(::Delayed::Worker) end |