Skip to content

Commit eab0984

Browse files
authored
AP-513 improving order of operations (#21)
* AP-513 improving logging order of operations - co-authored with @danschmidt5189 - some logs were still not getting request_id logged - we believe there an issue with when the two around_performs were called * serializes request_id when queued
1 parent 65db360 commit eab0984

2 files changed

Lines changed: 28 additions & 20 deletions

File tree

app/jobs/application_job.rb

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,33 @@
11
class ApplicationJob < ActiveJob::Base
2-
attr_reader :request_id
2+
attr_accessor :request_id
33

4-
before_enqueue do
5-
arguments << { request_id: Current.request_id } if Current.request_id
4+
before_enqueue do |job|
5+
job.request_id = Current.request_id
66
end
77

8-
around_perform do |job, block|
9-
@request_id = job.arguments.pop[:request_id] if job.arguments.last.is_a?(Hash) && job.arguments.last.key?(:request_id)
10-
block.call
8+
before_perform :restore_request_id, :log_job_metadata
9+
10+
def serialize
11+
super.merge('request_id' => request_id)
1112
end
1213

13-
around_perform :log_job_metadata
14+
def deserialize(job_data)
15+
super
16+
self.request_id = job_data['request_id']
17+
end
1418

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

23+
private
24+
25+
def restore_request_id
26+
self.request_id ||= Current.request_id
27+
end
28+
1929
def log_job_metadata
20-
logger.with_fields = { activejob_id: job_id, request_id: @request_id }
21-
yield
30+
logger.with_fields = { activejob_id: job_id, request_id: request_id }
2231
end
2332

2433
# Log an exception

spec/jobs/application_job_spec.rb

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,15 @@ def perform(*args); end
2222
it 'enqueues the job with the request_id' do
2323
expect do
2424
TestJob.perform_later('some_arg')
25-
end.to have_enqueued_job(TestJob).with('some_arg', { request_id: request_id })
26-
end
27-
28-
it 'sets @request_id and removes it from the arguments' do
29-
job = TestJob.new('some_arg', { request_id: request_id })
30-
perform_enqueued_jobs { job.perform_now }
25+
end.to have_enqueued_job(TestJob)
3126

32-
expect(job.instance_variable_get(:@request_id)).to eq(request_id)
33-
expect(job.arguments).to eq(['some_arg'])
27+
enqueued_job = ActiveJob::Base.queue_adapter.enqueued_jobs.last
28+
expect(enqueued_job[:args]).to eq(['some_arg'])
29+
expect(enqueued_job['request_id']).to eq(request_id)
3430
end
3531

3632
it 'logs the activejob_id and request_id' do
37-
job = TestJob.new('some_arg', { request_id: request_id })
33+
job = TestJob.new('some_arg')
3834
allow(job.logger).to receive(:with_fields=)
3935

4036
perform_enqueued_jobs { job.perform_now }
@@ -50,14 +46,17 @@ def perform(*args); end
5046
expect do
5147
TestJob.perform_later('some_arg')
5248
end.to have_enqueued_job(TestJob).with('some_arg')
49+
50+
enqueued_job = ActiveJob::Base.queue_adapter.enqueued_jobs.last
51+
expect(enqueued_job['request_id']).to be_nil
5352
end
5453

55-
it 'does not set @request_id if not provided' do
54+
it 'does not set request_id if not provided' do
5655
job = TestJob.new('some_arg')
5756
perform_enqueued_jobs { job.perform_now }
5857

59-
expect(job.instance_variable_get(:@request_id)).to be_nil
6058
expect(job.arguments).to eq(['some_arg'])
59+
expect(job.request_id).to be_nil
6160
end
6261

6362
it 'logs the activejob_id without a request_id' do

0 commit comments

Comments
 (0)