Merge pull request #2646 from basecamp/skip-analyze-job-for-detached-blobs

🤖 Skip AnalyzeJob when blob has no attachments
This commit is contained in:
Joseph Hale, MS SE
2026-03-02 15:55:46 -07:00
committed by GitHub
2 changed files with 37 additions and 0 deletions
@@ -0,0 +1,14 @@
# Skip analysis for blobs whose attachments have already been destroyed.
# e.g. when a user uploads a file but deletes it before the analysis runs.
# Avoids `Aws::S3::Errors::NoSuchKey` when an upload is deleted before AnalyzeJob runs.
module ActiveStorageAnalyzeJobSkipDetached
def perform(blob)
return unless blob.attachments.exists?
super
end
end
ActiveSupport.on_load :active_storage_blob do
ActiveStorage::AnalyzeJob.prepend ActiveStorageAnalyzeJobSkipDetached
end
@@ -0,0 +1,23 @@
require "test_helper"
class ActiveStorageAnalyzeJobSkipDetachedTest < 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