Skip AnalyzeJob when blob has no attachments

Upload-then-delete races cause AnalyzeJob to hit S3 after PurgeJob has
already removed the object, producing Aws::S3::Errors::NoSuchKey noise
in solid_queue_failed_executions. Since PurgeOnLastAttachment destroys
the attachment row before enqueueing PurgeJob, checking
blob.attachments.exists? catches this — a fast DB query that avoids the
S3 round-trip entirely.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Joseph Hale
2026-03-02 14:13:00 -07:00
parent f4c35a1ca4
commit 143508fc22
2 changed files with 25 additions and 0 deletions
@@ -4,6 +4,8 @@
# there is currently a bug https://github.com/rails/rails/issues/55144
module ActiveStorageAnalyzeJobSuppressBroadcasts
def perform(blob)
return unless blob.attachments.exists?
Board.suppressing_turbo_broadcasts do
Card.suppressing_turbo_broadcasts do
super
@@ -0,0 +1,23 @@
require "test_helper"
class ActiveStorageAnalyzeJobSuppressBroadcastsTest < ActiveSupport::TestCase
test "skips analysis when blob has no attachments" do
blob = ActiveStorage::Blob.create_and_upload!(
io: StringIO.new("x" * 1024), filename: "orphan.txt", content_type: "text/plain"
)
blob.expects(:analyze).never
ActiveStorage::AnalyzeJob.perform_now(blob)
end
test "performs analysis when blob has attachments" do
card = cards(:logo)
card.image.attach io: StringIO.new("x" * 1024), filename: "test.png", content_type: "image/png"
blob = card.image.blob
blob.expects(:analyze).once
ActiveStorage::AnalyzeJob.perform_now(blob)
end
end