From 2c2a1c4b4a429ceb95c844b5f6982d4acc2d236f Mon Sep 17 00:00:00 2001 From: Mike Dalessio Date: Thu, 20 Nov 2025 18:47:42 -0500 Subject: [PATCH] Make sure jobs are enqueued only after all transactions are committed MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This should fix the recurring ActiveStorage::FileNotFoundError errors from ActiveStorage::AnalyzeJob. The issue is that, when images are not direct uploaded (e.g. a card background image or avatar image), the AnalyzeJob has been getting enqueued before the file itself was uploaded to blob storage. Setting ActiveJob::Base.enqueue_after_transaction_commit = true makes sure that jobs are always enqueued only once *all* transactions have been committed. This will be the Rails default at some point, but for now we still need to explicitly set it. Big thanks to @jeremy for the assist. 👏 --- config/initializers/active_job.rb | 1 + .../active_storage_analyze_job_retries.rb | 23 ------------------- 2 files changed, 1 insertion(+), 23 deletions(-) delete mode 100644 lib/rails_ext/active_storage_analyze_job_retries.rb diff --git a/config/initializers/active_job.rb b/config/initializers/active_job.rb index 340585f3b..d8eed109d 100644 --- a/config/initializers/active_job.rb +++ b/config/initializers/active_job.rb @@ -6,6 +6,7 @@ module FizzyActiveJobExtensions prepended do attr_reader :account + self.enqueue_after_transaction_commit = true end def initialize(...) diff --git a/lib/rails_ext/active_storage_analyze_job_retries.rb b/lib/rails_ext/active_storage_analyze_job_retries.rb deleted file mode 100644 index 1d9f2c9b8..000000000 --- a/lib/rails_ext/active_storage_analyze_job_retries.rb +++ /dev/null @@ -1,23 +0,0 @@ -# Avoid sporadic ActiveStorage::FileNotFoundError errors -# -# Our direct-uploads aren't really direct. They first get buffered by CloudFlare -# and then get sent to our storage servers. This can lead to situations where -# a form was submitted, and ActiveStorage::Attachment created, and an -# AnalyzeJob enqueued, but the file associated with the Blob doesn't yet exist -# in our storage service. -# -# A simple olution ot this problem is just to retry the job a few times with -# some backoff. -# -# Discussion: https://app.fizzy.do/5986089/cards/3056 -module ActiveStorageAnalyzeJobRetires - extend ActiveSupport::Concern - - included do - retry_on ActiveStorage::FileNotFoundError, attempts: 15, wait: 2.seconds - end -end - -ActiveSupport.on_load :active_storage_blob do - ActiveStorage::AnalyzeJob.prepend ActiveStorageAnalyzeJobRetires -end