Add rake task to check and purge accounts that are missing in origin (#6085)

* Add rake task to check and purge accounts that are missing in origin

* Add progress bar and --force options to mastodon:maintenance:purge_removed_accounts
This commit is contained in:
Eugen Rochko 2017-12-24 16:14:33 +01:00 committed by GitHub
parent 7e6214b869
commit cafe27fb29
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,5 +1,8 @@
# frozen_string_literal: true # frozen_string_literal: true
require 'optparse'
require 'colorize'
namespace :mastodon do namespace :mastodon do
desc 'Execute daily tasks (deprecated)' desc 'Execute daily tasks (deprecated)'
task :daily do task :daily do
@ -338,5 +341,75 @@ namespace :mastodon do
PreviewCard.where(embed_url: '', type: :photo).delete_all PreviewCard.where(embed_url: '', type: :photo).delete_all
LinkCrawlWorker.push_bulk status_ids LinkCrawlWorker.push_bulk status_ids
end end
desc 'Check every known remote account and delete those that no longer exist in origin'
task purge_removed_accounts: :environment do
prepare_for_options!
options = {}
OptionParser.new do |opts|
opts.banner = 'Usage: rails mastodon:maintenance:purge_removed_accounts [options]'
opts.on('-f', '--force', 'Remove all encountered accounts without asking for confirmation') do
options[:force] = true
end
opts.on('-h', '--help', 'Display this message') do
puts opts
exit
end
end.parse!
disable_log_stdout!
total = Account.remote.where(protocol: :activitypub).count
progress_bar = ProgressBar.create(total: total, format: '%c/%C |%w>%i| %e')
Account.remote.where(protocol: :activitypub).partitioned.find_each do |account|
progress_bar.increment
begin
res = Request.new(:head, account.uri).perform
rescue StandardError
# This could happen due to network timeout, DNS timeout, wrong SSL cert, etc,
# which should probably not lead to perceiving the account as deleted, so
# just skip till next time
next
end
if [404, 410].include?(res.code)
if options[:force]
account.destroy
else
progress_bar.pause
progress_bar.clear
print "\nIt seems like #{account.acct} no longer exists. Purge the account from the database? [Y/n]: ".colorize(:yellow)
confirm = STDIN.gets.chomp
puts ''
progress_bar.resume
if confirm.casecmp('n').zero?
next
else
account.destroy
end
end
end
end
end
end end
end end
def disable_log_stdout!
dev_null = Logger.new('/dev/null')
Rails.logger = dev_null
ActiveRecord::Base.logger = dev_null
HttpLog.configuration.logger = dev_null
Paperclip.options[:log] = false
end
def prepare_for_options!
2.times { ARGV.shift }
end