Skip to content

Commit 68e4764

Browse files
committed
serializes request_id when queued
1 parent 5c9b1e5 commit 68e4764

2 files changed

Lines changed: 28 additions & 23 deletions

File tree

app/jobs/application_job.rb

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,33 @@
11
class ApplicationJob < ActiveJob::Base
2-
# This is a little unorthodox, but we want the request_id to be available as an instance variable on the job,
3-
# so we add it to the arguments before the job is enqueued and then pull it out in a before_perform callback.
4-
# Admittedly, the request_id method mutates the arguments as a side effect of pulling the request_id out.
2+
attr_accessor :request_id
53

6-
before_enqueue do
7-
arguments << { request_id: Current.request_id } if Current.request_id
4+
before_enqueue do |job|
5+
job.request_id = Current.request_id
86
end
97

10-
before_perform :log_job_metadata
8+
before_perform :restore_request_id, :log_job_metadata
119

12-
def request_id
13-
r_id_hash, rest = arguments.partition { |arg| arg.is_a?(Hash) && arg.key?(:request_id) } unless defined? @request_id
14-
self.arguments = rest if rest.any?
15-
@request_id = r_id_hash.first[:request_id] if r_id_hash.any?
16-
@request_id
10+
def serialize
11+
super.merge('request_id' => request_id)
12+
end
13+
14+
def deserialize(job_data)
15+
super
16+
self.request_id = job_data['request_id']
1717
end
1818

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

23+
private
24+
25+
def restore_request_id
26+
self.request_id ||= Current.request_id
27+
end
28+
2329
def log_job_metadata
24-
logger.with_fields = { activejob_id: job_id, request_id: }
30+
logger.with_fields = { activejob_id: job_id, request_id: request_id }
2531
end
2632

2733
# 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)