Add constants for delay in services (#38375)

This commit is contained in:
Matt Jankowski 2026-03-25 05:59:38 -04:00 committed by GitHub
parent d42182d9a2
commit 52a8bf4118
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 23 additions and 12 deletions

View File

@ -1,6 +1,9 @@
# frozen_string_literal: true # frozen_string_literal: true
class ActivityPub::Activity::Create < ActivityPub::Activity class ActivityPub::Activity::Create < ActivityPub::Activity
DISTRIBUTE_DELAY = 1.minute
PROCESSING_DELAY = (30.seconds)..(10.minutes)
def perform def perform
@account.schedule_refresh_if_stale! @account.schedule_refresh_if_stale!
@ -73,7 +76,7 @@ class ActivityPub::Activity::Create < ActivityPub::Activity
def distribute def distribute
# Spread out crawling randomly to avoid DDoSing the link # Spread out crawling randomly to avoid DDoSing the link
LinkCrawlWorker.perform_in(rand(1..59).seconds, @status.id) LinkCrawlWorker.perform_in(rand(DISTRIBUTE_DELAY), @status.id)
# Distribute into home and list feeds and notify mentioned accounts # Distribute into home and list feeds and notify mentioned accounts
::DistributionWorker.perform_async(@status.id, { 'silenced_account_ids' => @silenced_account_ids }) if @options[:override_timestamps] || @status.within_realtime_window? ::DistributionWorker.perform_async(@status.id, { 'silenced_account_ids' => @silenced_account_ids }) if @options[:override_timestamps] || @status.within_realtime_window?
@ -315,7 +318,7 @@ class ActivityPub::Activity::Create < ActivityPub::Activity
media_attachment.download_thumbnail! media_attachment.download_thumbnail!
media_attachment.save media_attachment.save
rescue Mastodon::UnexpectedResponseError, *Mastodon::HTTP_CONNECTION_ERRORS rescue Mastodon::UnexpectedResponseError, *Mastodon::HTTP_CONNECTION_ERRORS
RedownloadMediaWorker.perform_in(rand(30..600).seconds, media_attachment.id) RedownloadMediaWorker.perform_in(rand(PROCESSING_DELAY), media_attachment.id)
rescue Seahorse::Client::NetworkingError => e rescue Seahorse::Client::NetworkingError => e
Rails.logger.warn "Error storing media attachment: #{e}" Rails.logger.warn "Error storing media attachment: #{e}"
RedownloadMediaWorker.perform_async(media_attachment.id) RedownloadMediaWorker.perform_async(media_attachment.id)
@ -371,7 +374,7 @@ class ActivityPub::Activity::Create < ActivityPub::Activity
def resolve_unresolved_mentions(status) def resolve_unresolved_mentions(status)
@unresolved_mentions.uniq.each do |uri| @unresolved_mentions.uniq.each do |uri|
MentionResolveWorker.perform_in(rand(30...600).seconds, status.id, uri, { 'request_id' => @options[:request_id] }) MentionResolveWorker.perform_in(rand(PROCESSING_DELAY), status.id, uri, { 'request_id' => @options[:request_id] })
end end
end end
@ -394,7 +397,7 @@ class ActivityPub::Activity::Create < ActivityPub::Activity
embedded_quote = safe_prefetched_embed(@account, @status_parser.quoted_object, @json['context']) embedded_quote = safe_prefetched_embed(@account, @status_parser.quoted_object, @json['context'])
ActivityPub::VerifyQuoteService.new.call(@quote, @quote_approval_uri, fetchable_quoted_uri: @quote_uri, prefetched_quoted_object: embedded_quote, request_id: @options[:request_id], depth: @options[:depth]) ActivityPub::VerifyQuoteService.new.call(@quote, @quote_approval_uri, fetchable_quoted_uri: @quote_uri, prefetched_quoted_object: embedded_quote, request_id: @options[:request_id], depth: @options[:depth])
rescue Mastodon::RecursionLimitExceededError, Mastodon::UnexpectedResponseError, *Mastodon::HTTP_CONNECTION_ERRORS rescue Mastodon::RecursionLimitExceededError, Mastodon::UnexpectedResponseError, *Mastodon::HTTP_CONNECTION_ERRORS
ActivityPub::RefetchAndVerifyQuoteWorker.perform_in(rand(30..600).seconds, @quote.id, @quote_uri, { 'request_id' => @options[:request_id], 'approval_uri' => @quote_approval_uri }) ActivityPub::RefetchAndVerifyQuoteWorker.perform_in(rand(PROCESSING_DELAY), @quote.id, @quote_uri, { 'request_id' => @options[:request_id], 'approval_uri' => @quote_approval_uri })
end end
def conversation_from_uri(uri) def conversation_from_uri(uri)

View File

@ -10,6 +10,9 @@ class ActivityPub::ProcessAccountService < BaseService
SUBDOMAINS_RATELIMIT = 10 SUBDOMAINS_RATELIMIT = 10
DISCOVERIES_PER_REQUEST = 400 DISCOVERIES_PER_REQUEST = 400
PROCESSING_DELAY = (30.seconds)..(10.minutes)
VERIFY_DELAY = 10.minutes
VALID_URI_SCHEMES = %w(http https).freeze VALID_URI_SCHEMES = %w(http https).freeze
# Should be called with confirmed valid JSON # Should be called with confirmed valid JSON
@ -152,7 +155,7 @@ class ActivityPub::ProcessAccountService < BaseService
@account.avatar = nil if @account.avatar_remote_url.blank? @account.avatar = nil if @account.avatar_remote_url.blank?
@account.avatar_description = avatar_description || '' @account.avatar_description = avatar_description || ''
rescue Mastodon::UnexpectedResponseError, *Mastodon::HTTP_CONNECTION_ERRORS rescue Mastodon::UnexpectedResponseError, *Mastodon::HTTP_CONNECTION_ERRORS
RedownloadAvatarWorker.perform_in(rand(30..600).seconds, @account.id) RedownloadAvatarWorker.perform_in(rand(PROCESSING_DELAY), @account.id)
end end
begin begin
header_url, header_description = image_url_and_description('image') header_url, header_description = image_url_and_description('image')
@ -160,7 +163,7 @@ class ActivityPub::ProcessAccountService < BaseService
@account.header = nil if @account.header_remote_url.blank? @account.header = nil if @account.header_remote_url.blank?
@account.header_description = header_description || '' @account.header_description = header_description || ''
rescue Mastodon::UnexpectedResponseError, *Mastodon::HTTP_CONNECTION_ERRORS rescue Mastodon::UnexpectedResponseError, *Mastodon::HTTP_CONNECTION_ERRORS
RedownloadHeaderWorker.perform_in(rand(30..600).seconds, @account.id) RedownloadHeaderWorker.perform_in(rand(PROCESSING_DELAY), @account.id)
end end
@account.statuses_count = outbox_total_items if outbox_total_items.present? @account.statuses_count = outbox_total_items if outbox_total_items.present?
@account.following_count = following_total_items if following_total_items.present? @account.following_count = following_total_items if following_total_items.present?
@ -210,7 +213,7 @@ class ActivityPub::ProcessAccountService < BaseService
end end
def check_links! def check_links!
VerifyAccountLinksWorker.perform_in(rand(10.minutes.to_i), @account.id) VerifyAccountLinksWorker.perform_in(rand(VERIFY_DELAY), @account.id)
end end
def process_duplicate_accounts! def process_duplicate_accounts!

View File

@ -5,6 +5,8 @@ class ActivityPub::ProcessFeaturedItemService
include Lockable include Lockable
include Redisable include Redisable
PROCESSING_DELAY = (30.seconds)..(10.minutes)
def call(collection, uri_or_object, position: nil, request_id: nil) def call(collection, uri_or_object, position: nil, request_id: nil)
@collection = collection @collection = collection
@request_id = request_id @request_id = request_id
@ -47,6 +49,6 @@ class ActivityPub::ProcessFeaturedItemService
def verify_authorization! def verify_authorization!
ActivityPub::VerifyFeaturedItemService.new.call(@collection_item, @approval_uri, request_id: @request_id) ActivityPub::VerifyFeaturedItemService.new.call(@collection_item, @approval_uri, request_id: @request_id)
rescue Mastodon::RecursionLimitExceededError, Mastodon::UnexpectedResponseError, *Mastodon::HTTP_CONNECTION_ERRORS rescue Mastodon::RecursionLimitExceededError, Mastodon::UnexpectedResponseError, *Mastodon::HTTP_CONNECTION_ERRORS
ActivityPub::VerifyFeaturedItemWorker.perform_in(rand(30..600).seconds, @collection_item.id, @approval_uri, @request_id) ActivityPub::VerifyFeaturedItemWorker.perform_in(rand(PROCESSING_DELAY), @collection_item.id, @approval_uri, @request_id)
end end
end end

View File

@ -5,6 +5,9 @@ class ActivityPub::ProcessStatusUpdateService < BaseService
include Redisable include Redisable
include Lockable include Lockable
CRAWL_DELAY = 1.minute
PROCESSING_DELAY = (30.seconds)..(10.minutes)
def call(status, activity_json, object_json, request_id: nil) def call(status, activity_json, object_json, request_id: nil)
raise ArgumentError, 'Status has unsaved changes' if status.changed? raise ArgumentError, 'Status has unsaved changes' if status.changed?
@ -128,7 +131,7 @@ class ActivityPub::ProcessStatusUpdateService < BaseService
media_attachment.download_thumbnail! if media_attachment.thumbnail_remote_url_previously_changed? media_attachment.download_thumbnail! if media_attachment.thumbnail_remote_url_previously_changed?
media_attachment.save media_attachment.save
rescue Mastodon::UnexpectedResponseError, *Mastodon::HTTP_CONNECTION_ERRORS rescue Mastodon::UnexpectedResponseError, *Mastodon::HTTP_CONNECTION_ERRORS
RedownloadMediaWorker.perform_in(rand(30..600).seconds, media_attachment.id) RedownloadMediaWorker.perform_in(rand(PROCESSING_DELAY), media_attachment.id)
rescue Seahorse::Client::NetworkingError => e rescue Seahorse::Client::NetworkingError => e
Rails.logger.warn "Error storing media attachment: #{e}" Rails.logger.warn "Error storing media attachment: #{e}"
end end
@ -279,7 +282,7 @@ class ActivityPub::ProcessStatusUpdateService < BaseService
# Queue unresolved mentions for later # Queue unresolved mentions for later
unresolved_mentions.uniq.each do |uri| unresolved_mentions.uniq.each do |uri|
MentionResolveWorker.perform_in(rand(30...600).seconds, @status.id, uri, { 'request_id' => @request_id }) MentionResolveWorker.perform_in(rand(PROCESSING_DELAY), @status.id, uri, { 'request_id' => @request_id })
end end
end end
@ -361,7 +364,7 @@ class ActivityPub::ProcessStatusUpdateService < BaseService
embedded_quote = safe_prefetched_embed(@account, @status_parser.quoted_object, @activity_json['context']) embedded_quote = safe_prefetched_embed(@account, @status_parser.quoted_object, @activity_json['context'])
ActivityPub::VerifyQuoteService.new.call(quote, approval_uri, fetchable_quoted_uri: quote_uri, prefetched_quoted_object: embedded_quote, request_id: @request_id) ActivityPub::VerifyQuoteService.new.call(quote, approval_uri, fetchable_quoted_uri: quote_uri, prefetched_quoted_object: embedded_quote, request_id: @request_id)
rescue Mastodon::UnexpectedResponseError, *Mastodon::HTTP_CONNECTION_ERRORS rescue Mastodon::UnexpectedResponseError, *Mastodon::HTTP_CONNECTION_ERRORS
ActivityPub::RefetchAndVerifyQuoteWorker.perform_in(rand(30..600).seconds, quote.id, quote_uri, { 'request_id' => @request_id, 'approval_uri' => approval_uri }) ActivityPub::RefetchAndVerifyQuoteWorker.perform_in(rand(PROCESSING_DELAY), quote.id, quote_uri, { 'request_id' => @request_id, 'approval_uri' => approval_uri })
end end
def update_counts! def update_counts!
@ -419,7 +422,7 @@ class ActivityPub::ProcessStatusUpdateService < BaseService
def reset_preview_card! def reset_preview_card!
@status.reset_preview_card! @status.reset_preview_card!
LinkCrawlWorker.perform_in(rand(1..59).seconds, @status.id) LinkCrawlWorker.perform_in(rand(CRAWL_DELAY), @status.id)
end end
def broadcast_updates! def broadcast_updates!