diff --git a/app/jobs/application_job.rb b/app/jobs/application_job.rb index 76d43bdb..4b2de030 100644 --- a/app/jobs/application_job.rb +++ b/app/jobs/application_job.rb @@ -1,24 +1,27 @@ class ApplicationJob < ActiveJob::Base - attr_reader :request_id + # This is a little unorthodox, but we want the request_id to be available as an instance variable on the job, + # so we add it to the arguments before the job is enqueued and then pull it out in a before_perform callback. + # Admittedly, the request_id method mutates the arguments as a side effect of pulling the request_id out. 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 + r_id_hash, rest = arguments.partition { |arg| arg.is_a?(Hash) && arg.key?(:request_id) } unless defined? @request_id + self.arguments = rest if rest.any? + @request_id = r_id_hash.first[:request_id] if r_id_hash.any? + @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