FIX: Infinite loop in migrate_to_s3 rake task

FIX: Infinite loop in migrate_to_s3 rake task

diff --git a/lib/file_store/to_s3_migration.rb b/lib/file_store/to_s3_migration.rb
index 01c69dc..6792b94 100644
--- a/lib/file_store/to_s3_migration.rb
+++ b/lib/file_store/to_s3_migration.rb
@@ -6,6 +6,8 @@ module FileStore
   ToS3MigrationError = Class.new(RuntimeError)
 
   class ToS3Migration
+    MISSING_UPLOADS_RAKE_TASK_NAME ||= 'posts:missing_uploads'
+
     def initialize(s3_options:, dry_run: false, migrate_to_multisite: false, skip_etag_verify: false)
 
       @s3_bucket = s3_options[:bucket]
@@ -85,8 +87,8 @@ module FileStore
         success = false
       end
 
-      Discourse::Application.load_tasks
-      Rake::Task['posts:missing_uploads']
+      Discourse::Application.load_tasks unless Rake::Task.task_defined?(MISSING_UPLOADS_RAKE_TASK_NAME)
+      Rake::Task[MISSING_UPLOADS_RAKE_TASK_NAME]
       count = DB.query_single(<<~SQL, Post::MISSING_UPLOADS, Post::MISSING_UPLOADS_IGNORED).first
         SELECT COUNT(1)
         FROM posts p

GitHub sha: baae0e74

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

https://meta.discourse.org/t/rake-uploads-migrate-to-s3-runs-forever/147245/4