Skip to content

Commit 7cde61c

Browse files
committed
adds a few values to good_job logs when available
When active_jobs are enqueued, this will add current_attributes for jobid, request_id, and ip_address.
1 parent c703c97 commit 7cde61c

File tree

3 files changed

+30
-0
lines changed

3 files changed

+30
-0
lines changed

app/controllers/application_controller.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ class ApplicationController < ActionController::Base
33
include AuthSupport
44
include ExceptionHandling
55

6+
before_action :set_current_request_id_and_ip_address
7+
68
# @!group Class Attributes
79
# @!attribute [rw]
810
# Value of the "Questions?" mailto link in the footer
@@ -19,6 +21,11 @@ class ApplicationController < ActionController::Base
1921

2022
private
2123

24+
def set_current_request_id_and_ip_address
25+
Current.request_id = request.request_id
26+
Current.ip_address = request.remote_ip
27+
end
28+
2229
helper_method :authenticated?
2330

2431
# @return Regexp Pattern determining whether a request should be "hidden"

app/jobs/application_job.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,27 @@
11
class ApplicationJob < ActiveJob::Base
2+
before_enqueue do
3+
self.arguments << { current_attributes: Current.attributes }
4+
end
5+
6+
around_perform do |job, block|
7+
current_attributes = job.arguments.pop[:current_attributes] || {}
8+
Current.set(current_attributes) do
9+
block.call
10+
end
11+
end
12+
13+
around_perform :log_activejob_id
14+
215
def today
316
@today ||= Time.zone.now.strftime('%Y%m%d')
417
end
518

19+
# AP-186: Add the ActiveJob ID to job logs
20+
def log_activejob_id
21+
logger.with_fields = { activejob_id: job_id, request_id: Current.request_id, ip_address: Current.ip_address }
22+
yield
23+
end
24+
625
# Log an exception
726
def log_error(error)
827
# TODO: share code w/ApplicationController

app/models/current.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
class Current < ActiveSupport::CurrentAttributes
2+
attribute :request_id, :ip_address
3+
4+
end

0 commit comments

Comments
 (0)