Skip to content

Commit 5c9b1e5

Browse files
committed
better request_id removal from args
- replaces the positional removal with a destructuring and reassignment.
1 parent bb5fd3c commit 5c9b1e5

1 file changed

Lines changed: 7 additions & 7 deletions

File tree

app/jobs/application_job.rb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
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.
5+
26
before_enqueue do
37
arguments << { request_id: Current.request_id } if Current.request_id
48
end
59

610
before_perform :log_job_metadata
711

812
def request_id
9-
if !defined? @request_id
10-
if arguments.last.is_a?(Hash) && arguments.last.key?(:request_id)
11-
@request_id = arguments.pop[:request_id]
12-
else
13-
@request_id = nil
14-
end
15-
end
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?
1616
@request_id
1717
end
1818

0 commit comments

Comments
 (0)