Skip to content

Commit cdceac8

Browse files
committed
Track Que bulk enqueue with our own flag
The inner enqueues in a bulk_enqueue block detected the batch by reading Que's internal :que_jobs_to_bulk_insert thread-local. Set our own flag in the bulk_enqueue wrapper instead, so the pass-through doesn't depend on Que internals.
1 parent 986d604 commit cdceac8

1 file changed

Lines changed: 11 additions & 7 deletions

File tree

  • lib/appsignal/integrations

lib/appsignal/integrations/que.rb

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -110,11 +110,11 @@ def _run(*args)
110110
# transparent pass-through.
111111
module QueClientPlugin
112112
def enqueue(*args, job_options: {}, **rest)
113-
# Inside a Que 2 `bulk_enqueue` block the per-job enqueue must stay a
113+
# Inside a `bulk_enqueue` block the per-job enqueue must stay a
114114
# pass-through: tags come from `bulk_enqueue`'s own `job_options` (Que
115115
# raises if an inner enqueue passes them), and the batch's event and
116116
# propagation are recorded once by the `bulk_enqueue` wrapper.
117-
return super if bulk_insert_in_progress?
117+
return super if Thread.current[:appsignal_que_bulk_enqueue]
118118

119119
# Resolve the job class the way Que does: an explicit `:job_class`, else
120120
# the class `enqueue` was called on.
@@ -151,10 +151,6 @@ def job_options_with_context(job_options)
151151
tags = QueTraceContext.inject(job_options[:tags])
152152
tags.empty? ? job_options : job_options.merge(:tags => tags)
153153
end
154-
155-
def bulk_insert_in_progress?
156-
!Thread.current[:que_jobs_to_bulk_insert].nil?
157-
end
158154
end
159155

160156
# @!visibility private
@@ -167,7 +163,15 @@ def bulk_insert_in_progress?
167163
module QueBulkClientPlugin
168164
def bulk_enqueue(job_options: {}, **rest, &block)
169165
record_enqueue(job_options, "bulk_enqueue.que", bulk_enqueue_title(job_options)) do |merged|
170-
super(:job_options => merged, **rest, &block)
166+
# Flag the batch so the enqueues this block triggers pass through
167+
# without recording, without reading Que's internal bulk state.
168+
was_bulk = Thread.current[:appsignal_que_bulk_enqueue]
169+
Thread.current[:appsignal_que_bulk_enqueue] = true
170+
begin
171+
super(:job_options => merged, **rest, &block)
172+
ensure
173+
Thread.current[:appsignal_que_bulk_enqueue] = was_bulk
174+
end
171175
end
172176
end
173177

0 commit comments

Comments
 (0)