Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 12 additions & 9 deletions app/jobs/application_job.rb
Original file line number Diff line number Diff line change
@@ -1,24 +1,27 @@
class ApplicationJob < ActiveJob::Base
attr_reader :request_id

before_enqueue do
arguments << { request_id: Current.request_id } if Current.request_id
end

around_perform do |job, block|
@request_id = job.arguments.pop[:request_id] if job.arguments.last.is_a?(Hash) && job.arguments.last.key?(:request_id)
block.call
end
before_perform :log_job_metadata

around_perform :log_job_metadata
def request_id
if !defined? @request_id
if arguments.last.is_a?(Hash) && arguments.last.key?(:request_id)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since it came up on our call, I'll just note you could use find to make this more robust to argument order:

@request_id = args.select { |arg| arg.is_a? Hash }
  .find(-> {{}}) { |arg| arg.key?(:request_id) }
  .fetch(:request_id)

I'm sure there's a more syntactically sugary way to write that, too, but you get the gist. (And yes, it's inefficient to select -> find, I've just done that for clarity.)

@request_id = arguments.pop[:request_id]
else
@request_id = nil
end
end
@request_id
end

def today
@today ||= Time.zone.now.strftime('%Y%m%d')
end

def log_job_metadata
logger.with_fields = { activejob_id: job_id, request_id: @request_id }
yield
logger.with_fields = { activejob_id: job_id, request_id: }
end

# Log an exception
Expand Down
Loading