Skip to content

Commit a756c8e

Browse files
Gargronhiyuki2578
authored andcommitted
Add retry for failed media downloads and tootctl media refresh (mastodon#11775)
1 parent a15f447 commit a756c8e

5 files changed

Lines changed: 92 additions & 16 deletions

File tree

app/lib/activitypub/activity/create.rb

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -189,22 +189,25 @@ def process_attachments
189189
media_attachments = []
190190

191191
as_array(@object['attachment']).each do |attachment|
192-
next if attachment['url'].blank?
192+
next if attachment['url'].blank? || media_attachments.size >= 4
193193

194-
href = Addressable::URI.parse(attachment['url']).normalize.to_s
195-
media_attachment = MediaAttachment.create(account: @account, remote_url: href, description: attachment['name'].presence, focus: attachment['focalPoint'], blurhash: supported_blurhash?(attachment['blurhash']) ? attachment['blurhash'] : nil)
196-
media_attachments << media_attachment
194+
begin
195+
href = Addressable::URI.parse(attachment['url']).normalize.to_s
196+
media_attachment = MediaAttachment.create(account: @account, remote_url: href, description: attachment['name'].presence, focus: attachment['focalPoint'], blurhash: supported_blurhash?(attachment['blurhash']) ? attachment['blurhash'] : nil)
197+
media_attachments << media_attachment
197198

198-
next if unsupported_media_type?(attachment['mediaType']) || skip_download?
199+
next if unsupported_media_type?(attachment['mediaType']) || skip_download?
199200

200-
media_attachment.file_remote_url = href
201-
media_attachment.save
201+
media_attachment.file_remote_url = href
202+
media_attachment.save
203+
rescue Mastodon::UnexpectedResponseError, HTTP::TimeoutError, HTTP::ConnectionError, OpenSSL::SSL::SSLError
204+
RedownloadMediaWorker.perform_in(rand(30..600).seconds, media_attachment.id)
205+
end
202206
end
203207

204208
media_attachments
205209
rescue Addressable::URI::InvalidURIError => e
206-
Rails.logger.debug e
207-
210+
Rails.logger.debug "Invalid URL in attachment: #{e}"
208211
media_attachments
209212
end
210213

app/models/concerns/remotable.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ module Remotable
44
extend ActiveSupport::Concern
55

66
class_methods do
7-
def remotable_attachment(attachment_name, limit)
7+
def remotable_attachment(attachment_name, limit, suppress_errors: true)
88
attribute_name = "#{attachment_name}_remote_url".to_sym
99
method_name = "#{attribute_name}=".to_sym
1010
alt_method_name = "reset_#{attachment_name}!".to_sym
@@ -22,7 +22,7 @@ def remotable_attachment(attachment_name, limit)
2222

2323
begin
2424
Request.new(:get, url).perform do |response|
25-
next if response.code != 200
25+
raise Mastodon::UnexpectedResponseError, response unless (200...300).cover?(response.code)
2626

2727
content_type = parse_content_type(response.headers.get('content-type').last)
2828
extname = detect_extname_from_content_type(content_type)
@@ -41,11 +41,11 @@ def remotable_attachment(attachment_name, limit)
4141

4242
self[attribute_name] = url if has_attribute?(attribute_name)
4343
end
44-
rescue HTTP::TimeoutError, HTTP::ConnectionError, OpenSSL::SSL::SSLError, Paperclip::Errors::NotIdentifiedByImageMagickError, Addressable::URI::InvalidURIError, Mastodon::HostValidationError, Mastodon::LengthValidationError => e
44+
rescue Mastodon::UnexpectedResponseError, HTTP::TimeoutError, HTTP::ConnectionError, OpenSSL::SSL::SSLError => e
45+
Rails.logger.debug "Error fetching remote #{attachment_name}: #{e}"
46+
raise e unless suppress_errors
47+
rescue Paperclip::Errors::NotIdentifiedByImageMagickError, Addressable::URI::InvalidURIError, Mastodon::HostValidationError, Mastodon::LengthValidationError, Paperclip::Error, Mastodon::DimensionsValidationError => e
4548
Rails.logger.debug "Error fetching remote #{attachment_name}: #{e}"
46-
nil
47-
rescue Paperclip::Error, Mastodon::DimensionsValidationError => e
48-
Rails.logger.debug "Error processing remote #{attachment_name}: #{e}"
4949
nil
5050
end
5151
end

app/models/media_attachment.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ class MediaAttachment < ApplicationRecord
118118
validates_attachment_content_type :file, content_type: IMAGE_MIME_TYPES + VIDEO_MIME_TYPES + AUDIO_MIME_TYPES
119119
validates_attachment_size :file, less_than: IMAGE_LIMIT, unless: :larger_media_format?
120120
validates_attachment_size :file, less_than: VIDEO_LIMIT, if: :larger_media_format?
121-
remotable_attachment :file, VIDEO_LIMIT
121+
remotable_attachment :file, VIDEO_LIMIT, suppress_errors: false
122122

123123
include Attachmentable
124124

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# frozen_string_literal: true
2+
3+
class RedownloadMediaWorker
4+
include Sidekiq::Worker
5+
include ExponentialBackoff
6+
7+
sidekiq_options queue: 'pull', retry: 3
8+
9+
def perform(id)
10+
media_attachment = MediaAttachment.find(id)
11+
12+
return if media_attachment.remote_url.blank?
13+
14+
media_attachment.reset_file!
15+
media_attachment.save
16+
rescue ActiveRecord::RecordNotFound
17+
true
18+
end
19+
end

lib/mastodon/media_cli.rb

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,59 @@ def remove
4343

4444
say("Removed #{processed} media attachments (approx. #{number_to_human_size(aggregate)}) #{dry_run}", :green, true)
4545
end
46+
47+
option :account, type: :string
48+
option :domain, type: :string
49+
option :status, type: :numeric
50+
option :concurrency, type: :numeric, default: 5, aliases: [:c]
51+
option :verbose, type: :boolean, default: false, aliases: [:v]
52+
option :dry_run, type: :boolean, default: false
53+
desc 'refresh', 'Fetch remote media files'
54+
long_desc <<-DESC
55+
Re-downloads media attachments from other servers. You must specify the
56+
source of media attachments with one of the following options:
57+
58+
Use the --status option to download attachments from a specific status,
59+
using the status local numeric ID.
60+
61+
Use the --account option to download attachments from a specific account,
62+
using username@domain handle of the account.
63+
64+
Use the --domain option to download attachments from a specific domain.
65+
DESC
66+
def refresh
67+
dry_run = options[:dry_run] ? ' (DRY RUN)' : ''
68+
69+
if options[:status]
70+
scope = MediaAttachment.where(status_id: options[:status])
71+
elsif options[:account]
72+
username, domain = username.split('@')
73+
account = Account.find_remote(username, domain)
74+
75+
if account.nil?
76+
say('No such account', :red)
77+
exit(1)
78+
end
79+
80+
scope = MediaAttachment.where(account_id: account.id)
81+
elsif options[:domain]
82+
scope = MediaAttachment.joins(:account).merge(Account.by_domain_and_subdomains(options[:domain]))
83+
else
84+
exit(1)
85+
end
86+
87+
processed, aggregate = parallelize_with_progress(scope) do |media_attachment|
88+
next if media_attachment.remote_url.blank?
89+
90+
unless options[:dry_run]
91+
media_attachment.reset_file!
92+
media_attachment.save
93+
end
94+
95+
media_attachment.file_file_size
96+
end
97+
98+
say("Downloaded #{processed} media attachments (approx. #{number_to_human_size(aggregate)})#{dry_run}", :green, true)
99+
end
46100
end
47101
end

0 commit comments

Comments
 (0)