闭社主体 forked from https://github.com/tootsuite/mastodon
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

90 lines
2.7 KiB

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