-
Notifications
You must be signed in to change notification settings - Fork 98
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement ScoutApm::BackgroundJobIntegrations::GoodJob class
- Loading branch information
1 parent
ff64d3e
commit 995da5f
Showing
1 changed file
with
50 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |