From 6d9ad30bf861b2422c5951f7593a657675fedc24 Mon Sep 17 00:00:00 2001
From: Eugen Rochko <eugen@zeonfederated.com>
Date: Wed, 5 May 2021 23:46:59 +0200
Subject: [PATCH] Fix media redownload worker retrying on unexpected response
 codes (#16111)

---
 app/workers/redownload_media_worker.rb | 11 ++++++++++-
 lib/exceptions.rb                      |  4 ++++
 2 files changed, 14 insertions(+), 1 deletion(-)

diff --git a/app/workers/redownload_media_worker.rb b/app/workers/redownload_media_worker.rb
index 0638cd0f09..343caa32c2 100644
--- a/app/workers/redownload_media_worker.rb
+++ b/app/workers/redownload_media_worker.rb
@@ -3,6 +3,7 @@
 class RedownloadMediaWorker
   include Sidekiq::Worker
   include ExponentialBackoff
+  include JsonLdHelper
 
   sidekiq_options queue: 'pull', retry: 3
 
@@ -15,6 +16,14 @@ class RedownloadMediaWorker
     media_attachment.download_thumbnail!
     media_attachment.save
   rescue ActiveRecord::RecordNotFound
-    true
+    # Do nothing
+  rescue Mastodon::UnexpectedResponseError => e
+    response = e.response
+
+    if response_error_unsalvageable?(response)
+      # Give up
+    else
+      raise e
+    end
   end
 end
diff --git a/lib/exceptions.rb b/lib/exceptions.rb
index 7c8e778713..eb472abaab 100644
--- a/lib/exceptions.rb
+++ b/lib/exceptions.rb
@@ -12,7 +12,11 @@ module Mastodon
   class RateLimitExceededError < Error; end
 
   class UnexpectedResponseError < Error
+    attr_reader :response
+
     def initialize(response = nil)
+      @response = response
+
       if response.respond_to? :uri
         super("#{response.uri} returned code #{response.code}")
       else
-- 
GitLab