From dd92cd9d770ccf1feca8c767c93312b731621efb Mon Sep 17 00:00:00 2001 From: JP Camara Date: Mon, 24 Aug 2026 12:54:05 -0400 Subject: [PATCH] Ask the cheap question first when a batched job finishes Every job that finishes asks whether it was the last one in its batch, and almost never is. The batch row was being read before that question was asked, so all but one job per batch paid for a row they didn't use. Checking for outstanding tracking rows first drops the completion path from five statements to four. #finish still repeats the check, and the CAS it guards is unchanged, so nothing here decides anything on its own. Co-Authored-By: Claude Fable 5 --- app/models/solid_queue/batch_execution.rb | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/app/models/solid_queue/batch_execution.rb b/app/models/solid_queue/batch_execution.rb index 97223faf8..659ff6793 100644 --- a/app/models/solid_queue/batch_execution.rb +++ b/app/models/solid_queue/batch_execution.rb @@ -43,6 +43,12 @@ def count_new_jobs_among(jobs) private def finish_batch + # Every finishing job asks whether it was the last one, and almost never + # is. Asking the cheap question first means the batch row is only read + # for the job that actually finishes the batch. #finish asks it again, so + # this decides nothing on its own. + return if self.class.where(batch_id: batch_id).exists? + # Skip the serialized callback and metadata columns on this hot path if batch = Batch.select(:id, :finished_at, :enqueued_at).find_by(id: batch_id) batch.finish