Conversation
jason-raitz
commented
Feb 13, 2026
- 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
- this commit should eliminate that problem
- 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 - this commit should eliminate that problem
danschmidt5189
left a comment
There was a problem hiding this comment.
Curious about the test failures.
app/jobs/application_job.rb
Outdated
| around_perform :log_job_metadata | ||
| def request_id | ||
| if !defined? @request_id | ||
| if arguments.last.is_a?(Hash) && arguments.last.key?(:request_id) |
There was a problem hiding this comment.
Since it came up on our call, I'll just note you could use find to make this more robust to argument order:
@request_id = args.select { |arg| arg.is_a? Hash }
.find(-> {{}}) { |arg| arg.key?(:request_id) }
.fetch(:request_id)I'm sure there's a more syntactically sugary way to write that, too, but you get the gist. (And yes, it's inefficient to select -> find, I've just done that for clarity.)
There was a problem hiding this comment.
I meant to post this as a reply to your comment.
- replaces the positional removal with a destructuring and reassignment.
app/jobs/application_job.rb
Outdated
| 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 |
There was a problem hiding this comment.
I went with a destructering and reassignment.
There was a problem hiding this comment.
FWIW I found it hard to parse the intention here, so I'd recommend refactoring for clarity. There's also a subtle bug in that if r_id_hash.any? is false, then @request_id remains undefined.
Off the dome I came up with:
def request_id
if !defined? @request_id
request_id_arg, other_args = arguments.partition { |arg| arg.is_a?(Hash) && arg.key?(:request_id) }
@request_id = request_id_arg.first[:request_id] rescue nil
self.arguments = other_args if other_args.any?
end
@request_id
endThis retains the usual if/end/return pattern and should fix the nil assignment bug.
There was a problem hiding this comment.
Also maybe this is more along the lines of what @awilfox suggested, but I wonder if we need to use arguments here at all? We should be able to just set a request_id attribute directly on the job object using a callback. Only question there would be making sure it gets serialized/deserialized properly.
CurrentAttributes should be of help here.
awilfox
left a comment
There was a problem hiding this comment.
This is good, but I am a bit concerned that the mutation of arguments is done in a method. If we change how or when (or if) that method is called, suddenly the arguments will change too. I think it's better to do this in a dedicated callback, i.e. before_perform :set_request_id or something.
There was a problem hiding this comment.
looks good. one clarificatory question about your docs in comments, but otherwise r+.
edit: this might be related to @awilfox's concern, too.
app/jobs/application_job.rb
Outdated
| 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. |
There was a problem hiding this comment.
does this comment still apply with the restructuring you did?
There was a problem hiding this comment.
yeah. I'll fix things to go with something more like @awilfox's suggestion.
I think you're right. I'll redo it. I think the reason some of the jobs are not getting the request_id right now is possibly because of multiple before_performs and around_performs making it a race condition? I'll try something different in a bit. |
app/jobs/application_job.rb
Outdated
| 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 |
There was a problem hiding this comment.
FWIW I found it hard to parse the intention here, so I'd recommend refactoring for clarity. There's also a subtle bug in that if r_id_hash.any? is false, then @request_id remains undefined.
Off the dome I came up with:
def request_id
if !defined? @request_id
request_id_arg, other_args = arguments.partition { |arg| arg.is_a?(Hash) && arg.key?(:request_id) }
@request_id = request_id_arg.first[:request_id] rescue nil
self.arguments = other_args if other_args.any?
end
@request_id
endThis retains the usual if/end/return pattern and should fix the nil assignment bug.
anarchivist
left a comment
There was a problem hiding this comment.
👏 i like the direction this took! r+.
danschmidt5189
left a comment
There was a problem hiding this comment.
Looks good, I like how this turned out, very clean / Rails-y.
awilfox
left a comment
There was a problem hiding this comment.
Looks great. Thanks for cleaning it up so well! r+