media_cli.rb 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. # frozen_string_literal: true
  2. require_relative '../../config/boot'
  3. require_relative '../../config/environment'
  4. require_relative 'cli_helper'
  5. module Mastodon
  6. class MediaCLI < Thor
  7. option :days, type: :numeric, default: 7
  8. option :background, type: :boolean, default: false
  9. option :verbose, type: :boolean, default: false
  10. option :dry_run, type: :boolean, default: false
  11. desc 'remove', 'Remove remote media files'
  12. long_desc <<-DESC
  13. Removes locally cached copies of media attachments from other servers.
  14. The --days option specifies how old media attachments have to be before
  15. they are removed. It defaults to 7 days.
  16. With the --background option, instead of deleting the files sequentially,
  17. they will be queued into Sidekiq and the command will exit as soon as
  18. possible. In Sidekiq they will be processed with higher concurrency, but
  19. it may impact other operations of the Mastodon server, and it may overload
  20. the underlying file storage.
  21. With the --dry-run option, no work will be done.
  22. With the --verbose option, when media attachments are processed sequentially in the
  23. foreground, the IDs of the media attachments will be printed.
  24. DESC
  25. def remove
  26. time_ago = options[:days].days.ago
  27. queued = 0
  28. processed = 0
  29. dry_run = options[:dry_run] ? '(DRY RUN)' : ''
  30. if options[:background]
  31. MediaAttachment.where.not(remote_url: '').where.not(file_file_name: nil).where('created_at < ?', time_ago).select(:id).reorder(nil).find_in_batches do |media_attachments|
  32. queued += media_attachments.size
  33. Maintenance::UncacheMediaWorker.push_bulk(media_attachments.map(&:id)) unless options[:dry_run]
  34. end
  35. else
  36. MediaAttachment.where.not(remote_url: '').where.not(file_file_name: nil).where('created_at < ?', time_ago).reorder(nil).find_in_batches do |media_attachments|
  37. media_attachments.each do |m|
  38. Maintenance::UncacheMediaWorker.new.perform(m) unless options[:dry_run]
  39. options[:verbose] ? say(m.id) : say('.', :green, false)
  40. processed += 1
  41. end
  42. end
  43. end
  44. say
  45. if options[:background]
  46. say("Scheduled the deletion of #{queued} media attachments #{dry_run}", :green, true)
  47. else
  48. say("Removed #{processed} media attachments #{dry_run}", :green, true)
  49. end
  50. end
  51. end
  52. end