PERF: Update post uploads secure status in a job (#13459)

PERF: Update post uploads secure status in a job (#13459)

When secure uploads are enabled, editing a post with many uploads can cause a timeout because the store has to be contacted for each upload.

diff --git a/app/jobs/regular/update_post_uploads_secure_status.rb b/app/jobs/regular/update_post_uploads_secure_status.rb
new file mode 100644
index 0000000..b27b4c9
--- /dev/null
+++ b/app/jobs/regular/update_post_uploads_secure_status.rb
@@ -0,0 +1,14 @@
+# frozen_string_literal: true
+
+module Jobs
+  class UpdatePostUploadsSecureStatus < ::Jobs::Base
+    def execute(args)
+      post = Post.find_by(id: args[:post_id])
+      return if post.blank?
+
+      post.uploads.each do |upload|
+        upload.update_secure_status(source: args[:source])
+      end
+    end
+  end
+end
diff --git a/app/models/post.rb b/app/models/post.rb
index 306bf38..b761dee 100644
--- a/app/models/post.rb
+++ b/app/models/post.rb
@@ -960,7 +960,7 @@ class Post < ActiveRecord::Base
 
   def update_uploads_secure_status(source:)
     if Discourse.store.external?
-      self.uploads.each { |upload| upload.update_secure_status(source: source) }
+      Jobs.enqueue(:update_post_uploads_secure_status, post_id: self.id, source: source)
     end
   end
 
diff --git a/spec/components/post_revisor_spec.rb b/spec/components/post_revisor_spec.rb
index 5d54fbd..6832e9f 100644
--- a/spec/components/post_revisor_spec.rb
+++ b/spec/components/post_revisor_spec.rb
@@ -1119,6 +1119,7 @@ describe PostRevisor do
       context "secure media uploads" do
         let!(:image5) { Fabricate(:secure_upload) }
         before do
+          Jobs.run_immediately!
           setup_s3
           SiteSetting.authorized_extensions = "png|jpg|gif|mp4"
           SiteSetting.secure_media = true
diff --git a/spec/models/post_spec.rb b/spec/models/post_spec.rb
index 1e19d97..d9c3369 100644
--- a/spec/models/post_spec.rb
+++ b/spec/models/post_spec.rb
@@ -1481,6 +1481,8 @@ describe Post do
       end
 
       before do
+        Jobs.run_immediately!
+
         setup_s3
         SiteSetting.authorized_extensions = "pdf|png|jpg|csv"
         SiteSetting.secure_media = true

GitHub sha: d1b2e9db3bcb7693094511edc415a953dc8a9116

This commit appears in #13459 which was approved by ZogStriP. It was merged by nbianca.

This commit has been mentioned on Discourse Meta. There might be relevant details there: