|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require 'tty-prompt' |
| 4 | +require_relative '../../config/boot' |
| 5 | +require_relative '../../config/environment' |
| 6 | +require_relative 'cli_helper' |
| 7 | + |
| 8 | +module Mastodon |
| 9 | + class PreviewCardsCLI < Thor |
| 10 | + include ActionView::Helpers::NumberHelper |
| 11 | + |
| 12 | + def self.exit_on_failure? |
| 13 | + true |
| 14 | + end |
| 15 | + |
| 16 | + option :days, type: :numeric, default: 180 |
| 17 | + option :background, type: :boolean, default: false |
| 18 | + option :verbose, type: :boolean, default: false |
| 19 | + option :dry_run, type: :boolean, default: false |
| 20 | + option :link, type: :boolean, default: false |
| 21 | + desc 'remove', 'Remove preview cards' |
| 22 | + long_desc <<-DESC |
| 23 | + Removes locally thumbnails for previews. |
| 24 | +
|
| 25 | + The --days option specifies how old preview cards have to be before |
| 26 | + they are removed. It defaults to 180 days. |
| 27 | +
|
| 28 | + With the --background option, instead of deleting the files sequentially, |
| 29 | + they will be queued into Sidekiq and the command will exit as soon as |
| 30 | + possible. In Sidekiq they will be processed with higher concurrency, but |
| 31 | + it may impact other operations of the Mastodon server, and it may overload |
| 32 | + the underlying file storage. |
| 33 | +
|
| 34 | + With the --dry-run option, no work will be done. |
| 35 | +
|
| 36 | + With the --verbose option, when preview cards are processed sequentially in the |
| 37 | + foreground, the IDs of the preview cards will be printed. |
| 38 | +
|
| 39 | + With the --link option, delete only link-type preview cards. |
| 40 | + DESC |
| 41 | + def remove |
| 42 | + prompt = TTY::Prompt.new |
| 43 | + time_ago = options[:days].days.ago |
| 44 | + queued = 0 |
| 45 | + processed = 0 |
| 46 | + size = 0 |
| 47 | + dry_run = options[:dry_run] ? '(DRY RUN)' : '' |
| 48 | + link = options[:link] ? 'link-type ' : '' |
| 49 | + scope = PreviewCard.where.not(image_file_name: nil) |
| 50 | + scope = scope.where.not(image_file_name: '') |
| 51 | + scope = scope.where(type: :link) if options[:link] |
| 52 | + scope = scope.where('updated_at < ?', time_ago) |
| 53 | + |
| 54 | + if time_ago > 2.weeks.ago |
| 55 | + prompt.say "\n" |
| 56 | + prompt.say('The preview cards less than the past two weeks will not be re-acquired even when needed.') |
| 57 | + prompt.say "\n" |
| 58 | + |
| 59 | + unless prompt.yes?('Are you sure you want to delete the preview cards?', default: false) |
| 60 | + prompt.say "\n" |
| 61 | + prompt.warn 'Nothing execute. Bye!' |
| 62 | + prompt.say "\n" |
| 63 | + exit(1) |
| 64 | + end |
| 65 | + end |
| 66 | + |
| 67 | + if options[:background] |
| 68 | + scope.select(:id, :image_file_size).reorder(nil).find_in_batches do |preview_cards| |
| 69 | + queued += preview_cards.size |
| 70 | + size += preview_cards.reduce(0) { |sum, p| sum + (p.image_file_size || 0) } |
| 71 | + Maintenance::UncachePreviewWorker.push_bulk(preview_cards.map(&:id)) unless options[:dry_run] |
| 72 | + end |
| 73 | + |
| 74 | + else |
| 75 | + scope.select(:id, :image_file_size).reorder(nil).find_in_batches do |preview_cards| |
| 76 | + preview_cards.each do |p| |
| 77 | + size += p.image_file_size || 0 |
| 78 | + Maintenance::UncachePreviewWorker.new.perform(p.id) unless options[:dry_run] |
| 79 | + options[:verbose] ? say(p.id) : say('.', :green, false) |
| 80 | + processed += 1 |
| 81 | + end |
| 82 | + end |
| 83 | + end |
| 84 | + |
| 85 | + say |
| 86 | + |
| 87 | + if options[:background] |
| 88 | + say("Scheduled the deletion of #{queued} #{link}preview cards (approx. #{number_to_human_size(size)}) #{dry_run}", :green, true) |
| 89 | + else |
| 90 | + say("Removed #{processed} #{link}preview cards (approx. #{number_to_human_size(size)}) #{dry_run}", :green, true) |
| 91 | + end |
| 92 | + end |
| 93 | + end |
| 94 | +end |
0 commit comments