Skip to content

Commit 0d80f68

Browse files
mayaehGargron
authored andcommitted
Add tootctl preview_cards remove (#11320)
* Add `tootctl preview_cards remove` * fix code style * Remove `Scheduler::PreviewCardsCleanupScheduler` file * fix code style again Add exclude case where image_file_name is blank * Added a function to output confirmation if the specified number of days is less than 2 weeks
1 parent 9349f10 commit 0d80f68

4 files changed

Lines changed: 98 additions & 25 deletions

File tree

app/workers/scheduler/preview_cards_cleanup_scheduler.rb

Lines changed: 0 additions & 22 deletions
This file was deleted.

config/sidekiq.yml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,6 @@
2424
ip_cleanup_scheduler:
2525
cron: '<%= Random.rand(0..59) %> <%= Random.rand(3..5) %> * * *'
2626
class: Scheduler::IpCleanupScheduler
27-
preview_cards_cleanup_scheduler:
28-
cron: '<%= Random.rand(0..59) %> <%= Random.rand(3..5) %> * * *'
29-
class: Scheduler::PreviewCardsCleanupScheduler
3027
email_scheduler:
3128
cron: '0 10 * * 2'
3229
class: Scheduler::EmailScheduler

lib/cli.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
require_relative 'mastodon/settings_cli'
1010
require_relative 'mastodon/statuses_cli'
1111
require_relative 'mastodon/domains_cli'
12+
require_relative 'mastodon/preview_cards_cli'
1213
require_relative 'mastodon/cache_cli'
1314
require_relative 'mastodon/version'
1415

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

46+
desc 'preview_cards SUBCOMMAND ...ARGS', 'Manage preview cards'
47+
subcommand 'preview_cards', Mastodon::PreviewCardsCLI
48+
4549
desc 'cache SUBCOMMAND ...ARGS', 'Manage cache'
4650
subcommand 'cache', Mastodon::CacheCLI
4751

lib/mastodon/preview_cards_cli.rb

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
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

Comments
 (0)