The code powering m.abunchtell.com https://m.abunchtell.com
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 

95 行
3.4 KiB

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