Skip to content

Commit 3d7c5d0

Browse files
ydahrosa
authored andcommitted
Keep batch completion errors from blocking subsequent jobs
1 parent 7323f03 commit 3d7c5d0

2 files changed

Lines changed: 58 additions & 0 deletions

File tree

‎app/models/solid_queue/batch_execution.rb‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ def finish_batch
4747
if batch = Batch.select(:id, :finished_at, :enqueued_at).find_by(id: batch_id)
4848
batch.finish
4949
end
50+
rescue => error
51+
# The job is already committed; let the sweeper retry batch completion.
52+
SolidQueue.instrument(:batch_progress_error, batch_id: batch_id, job_id: job_id, error: error)
5053
end
5154
end
5255
end

‎test/models/solid_queue/batch_test.rb‎

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,61 @@ def perform; end
192192
assert_equal 1, SolidQueue::Job.where(class_name: OtherAdapterCallbackJob.name).count
193193
end
194194

195+
[ :finished, :failed, :destroyed ].each do |outcome|
196+
[ SolidQueue::Job::EnqueueError, ActiveRecord::Deadlocked, NameError ].each do |error_class|
197+
test "batch completion #{error_class} does not prevent #{outcome} jobs from unblocking the next job" do
198+
result = JobResult.create!(queue_name: "default", status: "")
199+
batch = SolidQueue::Batch.enqueue(on_finish: BatchCompletionJob) do
200+
NonOverlappingUpdateResultJob.perform_later(result, name: "A", exception: outcome == :failed ? ExpectedTestError : nil)
201+
end
202+
job = batch.jobs.sole
203+
next_active_job = NonOverlappingUpdateResultJob.perform_later(result, name: "B")
204+
next_job = SolidQueue::Job.find_by!(active_job_id: next_active_job.job_id)
205+
process = SolidQueue::Process.register(kind: "Worker", pid: 42, name: "worker-123")
206+
claimed_execution = SolidQueue::ReadyExecution.claim(job.queue_name, 1, process.id).sole
207+
callback_error = error_class.new("batch callback failed")
208+
events = []
209+
210+
assert next_job.blocked?
211+
SolidQueue::Batch.any_instance.stubs(:enqueue_callback_jobs).raises(callback_error)
212+
213+
SolidQueue.with(preserve_finished_jobs: outcome != :destroyed) do
214+
ActiveSupport::Notifications.subscribed(->(event) { events << event }, "batch_progress_error.solid_queue") do
215+
if outcome == :failed
216+
assert_raises(ExpectedTestError) { claimed_execution.perform }
217+
else
218+
assert_nothing_raised { claimed_execution.perform }
219+
end
220+
end
221+
end
222+
223+
assert_not SolidQueue::ClaimedExecution.exists?(claimed_execution.id)
224+
if outcome == :destroyed
225+
assert_not SolidQueue::Job.exists?(job.id)
226+
else
227+
assert_equal outcome, job.reload.status
228+
end
229+
assert next_job.reload.ready?
230+
assert_not next_job.blocked?
231+
assert_equal 0, SolidQueue::Semaphore.find_by!(key: job.concurrency_key).value
232+
assert_not batch.reload.finished?
233+
assert_empty batch.batch_executions
234+
assert_equal 0, SolidQueue::Job.where(class_name: BatchCompletionJob.name).count
235+
assert_equal 1, events.size
236+
assert_equal batch.id, events.sole.payload[:batch_id]
237+
assert_equal job.id, events.sole.payload[:job_id]
238+
assert_same callback_error, events.sole.payload[:error]
239+
240+
SolidQueue::Batch.any_instance.unstub(:enqueue_callback_jobs)
241+
SolidQueue::Batch.sweep_stalled
242+
243+
assert batch.reload.finished?
244+
assert_equal outcome == :failed ? 1 : 0, batch.failed_jobs
245+
assert_equal 1, SolidQueue::Job.where(class_name: BatchCompletionJob.name).count
246+
end
247+
end
248+
end
249+
195250
test "assigns a reserved active_job_batch_id on create" do
196251
batch = SolidQueue::Batch.enqueue { NiceJob.perform_later("world") }
197252

0 commit comments

Comments
 (0)