feeds_cli.rb 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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 FeedsCLI < Thor
  7. option :all, type: :boolean, default: false
  8. option :background, type: :boolean, default: false
  9. option :dry_run, type: :boolean, default: false
  10. option :verbose, type: :boolean, default: false
  11. desc 'build [USERNAME]', 'Build home and list feeds for one or all users'
  12. long_desc <<-LONG_DESC
  13. Build home and list feeds that are stored in Redis from the database.
  14. With the --all option, all active users will be processed.
  15. Otherwise, a single user specified by USERNAME.
  16. With the --background option, regeneration will be queued into Sidekiq,
  17. and the command will exit as soon as possible.
  18. With the --dry-run option, no work will be done.
  19. With the --verbose option, when accounts are processed sequentially in the
  20. foreground, the IDs of the accounts will be printed.
  21. LONG_DESC
  22. def build(username = nil)
  23. dry_run = options[:dry_run] ? '(DRY RUN)' : ''
  24. if options[:all] || username.nil?
  25. processed = 0
  26. queued = 0
  27. User.active.select(:id, :account_id).reorder(nil).find_in_batches do |users|
  28. if options[:background]
  29. RegenerationWorker.push_bulk(users.map(&:account_id)) unless options[:dry_run]
  30. queued += users.size
  31. else
  32. users.each do |user|
  33. RegenerationWorker.new.perform(user.account_id) unless options[:dry_run]
  34. options[:verbose] ? say(user.account_id) : say('.', :green, false)
  35. processed += 1
  36. end
  37. end
  38. end
  39. if options[:background]
  40. say("Scheduled feed regeneration for #{queued} accounts #{dry_run}", :green, true)
  41. else
  42. say
  43. say("Regenerated feeds for #{processed} accounts #{dry_run}", :green, true)
  44. end
  45. elsif username.present?
  46. account = Account.find_local(username)
  47. if options[:background]
  48. RegenerationWorker.perform_async(account.id) unless options[:dry_run]
  49. else
  50. RegenerationWorker.new.perform(account.id) unless options[:dry_run]
  51. end
  52. say("OK #{dry_run}", :green, true)
  53. else
  54. say('No account(s) given', :red)
  55. exit(1)
  56. end
  57. end
  58. desc 'clear', 'Remove all home and list feeds from Redis'
  59. def clear
  60. keys = Redis.current.keys('feed:*')
  61. Redis.current.pipelined do
  62. keys.each { |key| Redis.current.del(key) }
  63. end
  64. say('OK', :green)
  65. end
  66. end
  67. end