diff --git a/lib/rails_ext/active_storage_analyze_job_skip_detached.rb b/lib/rails_ext/active_storage_analyze_job_skip_detached.rb new file mode 100644 index 000000000..efa671335 --- /dev/null +++ b/lib/rails_ext/active_storage_analyze_job_skip_detached.rb @@ -0,0 +1,17 @@ +# Skip analysis for blobs whose attachments have already been destroyed. +# +# When an upload is deleted before AnalyzeJob runs, PurgeOnLastAttachment has +# already destroyed the attachment row and enqueued PurgeJob for the blob's S3 +# object. Analyzing at this point hits a missing key. Same check as +# PurgeOnLastAttachment#purge_blob_if_last. +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 diff --git a/lib/rails_ext/active_storage_analyze_job_suppress_broadcasts.rb b/lib/rails_ext/active_storage_analyze_job_suppress_broadcasts.rb index 030232ee4..ba22603b6 100644 --- a/lib/rails_ext/active_storage_analyze_job_suppress_broadcasts.rb +++ b/lib/rails_ext/active_storage_analyze_job_suppress_broadcasts.rb @@ -4,8 +4,6 @@ # 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 diff --git a/test/lib/rails_ext/active_storage_analyze_job_suppress_broadcasts_test.rb b/test/lib/rails_ext/active_storage_analyze_job_skip_detached_test.rb similarity index 88% rename from test/lib/rails_ext/active_storage_analyze_job_suppress_broadcasts_test.rb rename to test/lib/rails_ext/active_storage_analyze_job_skip_detached_test.rb index 1015c957c..5ab5edaff 100644 --- a/test/lib/rails_ext/active_storage_analyze_job_suppress_broadcasts_test.rb +++ b/test/lib/rails_ext/active_storage_analyze_job_skip_detached_test.rb @@ -1,6 +1,6 @@ require "test_helper" -class ActiveStorageAnalyzeJobSuppressBroadcastsTest < ActiveSupport::TestCase +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"