Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tootctl preview_cards remove #11320

Merged
merged 5 commits into from
Jul 28, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions config/sidekiq.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,6 @@
ip_cleanup_scheduler:
cron: '<%= Random.rand(0..59) %> <%= Random.rand(3..5) %> * * *'
class: Scheduler::IpCleanupScheduler
preview_cards_cleanup_scheduler:
cron: '<%= Random.rand(0..59) %> <%= Random.rand(3..5) %> * * *'
class: Scheduler::PreviewCardsCleanupScheduler
mayaeh marked this conversation as resolved.
Show resolved Hide resolved
email_scheduler:
cron: '0 10 * * 2'
class: Scheduler::EmailScheduler
Expand Down
4 changes: 4 additions & 0 deletions lib/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
require_relative 'mastodon/settings_cli'
require_relative 'mastodon/statuses_cli'
require_relative 'mastodon/domains_cli'
require_relative 'mastodon/preview_cards_cli'
require_relative 'mastodon/cache_cli'
require_relative 'mastodon/version'

Expand Down Expand Up @@ -42,6 +43,9 @@ def self.exit_on_failure?
desc 'domains SUBCOMMAND ...ARGS', 'Manage account domains'
subcommand 'domains', Mastodon::DomainsCLI

desc 'preview_cards SUBCOMMAND ...ARGS', 'Manage preview cards'
subcommand 'preview_cards', Mastodon::PreviewCardsCLI

desc 'cache SUBCOMMAND ...ARGS', 'Manage cache'
subcommand 'cache', Mastodon::CacheCLI

Expand Down
91 changes: 91 additions & 0 deletions lib/mastodon/preview_cards_cli.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
# frozen_string_literal: true

require_relative '../../config/boot'
require_relative '../../config/environment'
require_relative 'cli_helper'

module Mastodon
class PreviewCardsCLI < Thor
include ActionView::Helpers::NumberHelper

def self.exit_on_failure?
true
end

option :days, type: :numeric, default: 180
option :background, type: :boolean, default: false
option :verbose, type: :boolean, default: false
option :dry_run, type: :boolean, default: false
option :link, type: :boolean, default: false
desc 'remove', 'Remove preview cards'
long_desc <<-DESC
Removes locally thumbnails for previews.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does locally thumbnails means?


The --days option specifies how old preview cards have to be before
they are removed. It defaults to 180 days.

With the --background option, instead of deleting the files sequentially,
they will be queued into Sidekiq and the command will exit as soon as
possible. In Sidekiq they will be processed with higher concurrency, but
it may impact other operations of the Mastodon server, and it may overload
the underlying file storage.

With the --dry-run option, no work will be done.

With the --verbose option, when preview cards are processed sequentially in the
foreground, the IDs of the preview cards will be printed.

With the --link option, delete only link-type preview cards.
DESC
def remove
time_ago = options[:days].days.ago
queued = 0
processed = 0
size = 0
dry_run = options[:dry_run] ? '(DRY RUN)' : ''

if options[:link] && options[:background]
mayaeh marked this conversation as resolved.
Show resolved Hide resolved
PreviewCard.where.not(image_file_name: nil).where(type: :link).where('updated_at < ?', time_ago).select(:id, :image_file_size).reorder(nil).find_in_batches do |preview_cards|
queued += preview_cards.size
size += preview_cards.reduce(0) { |sum, p| sum + (p.image_file_size || 0) }
Maintenance::UncachePreviewWorker.push_bulk(preview_cards.map(&:id)) unless options[:dry_run]
end

elsif options[:link] && !options[:background]
PreviewCard.where.not(image_file_name: nil).where(type: :link).where('updated_at < ?', time_ago).select(:id, :image_file_size).reorder(nil).find_in_batches do |preview_cards|
preview_cards.each do |p|
size += p.image_file_size || 0
Maintenance::UncachePreviewWorker.new.perform(p.id) unless options[:dry_run]
options[:verbose] ? say(p.id) : say('.', :green, false)
processed += 1
end
end

elsif !options[:link] && options[:background]
PreviewCard.where.not(image_file_name: nil).where('updated_at < ?', time_ago).select(:id, :image_file_size).reorder(nil).find_in_batches do |preview_cards|
queued += preview_cards.size
size += preview_cards.reduce(0) { |sum, p| sum + (p.image_file_size || 0) }
Maintenance::UncachePreviewWorker.push_bulk(preview_cards.map(&:id)) unless options[:dry_run]
end

else
PreviewCard.where.not(image_file_name: nil).where('updated_at < ?', time_ago).select(:id, :image_file_size).reorder(nil).find_in_batches do |preview_cards|
preview_cards.each do |p|
size += p.image_file_size || 0
Maintenance::UncachePreviewWorker.new.perform(p.id) unless options[:dry_run]
options[:verbose] ? say(p.id) : say('.', :green, false)
processed += 1
end
end
end

say

if options[:background]
say("Scheduled the deletion of #{queued} preview cards (approx. #{number_to_human_size(size)}) #{dry_run}", :green, true)
else
say("Removed #{processed} preview cards (approx. #{number_to_human_size(size)}) #{dry_run}", :green, true)
end
end
end
end