Skip to content

Commit

Permalink
Implement ScoutApm::BackgroundJobIntegrations::GoodJob class
Browse files Browse the repository at this point in the history
  • Loading branch information
benngarcia committed Aug 3, 2024
1 parent ff64d3e commit 995da5f
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions lib/scout_apm/background_job_integrations/good_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
module ScoutApm
module BackgroundJobIntegrations
class GoodJob
attr_reader :logger

def initialize(logger)
@logger = logger
end

def name
:good_job
end

def forking?
false
end

def install
require 'good_job'
GoodJob::ActiveRecordParentClass.class_eval do
include ScoutApm::Tracer

around_perform do |job, block|
# I have a sneaking suspicion there is a better way to handle Agent starting
# Maybe hook into GoodJob lifecycle events?
ScoutApm::Agent.instance.start_background_worker unless ScoutApm::Agent.instance.background_worker_running?
req = ScoutApm::RequestManager.lookup
latency = Time.now - job.scheduled_at rescue 0
req.annotate_request(queue_latency: latency)

begin
req.start_layer ScoutApm::Layer.new("Queue", job.queue_name)
started_queue = true # Following Convention
req.start_layer ScoutApm::Layer.new("Job", job.job_class)
started_job = true # Following Convention

block.call
rescue
req.error!
raise
ensure
req.stop_layer if started_job
req.stop_layer if started_queue
end
end
end
end
end
end
end

0 comments on commit 995da5f

Please sign in to comment.