闭社主体 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.

60 lines
1.9 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 CacheCLI < Thor
  7. include CLIHelper
  8. def self.exit_on_failure?
  9. true
  10. end
  11. desc 'clear', 'Clear out the cache storage'
  12. def clear
  13. Rails.cache.clear
  14. say('OK', :green)
  15. end
  16. option :concurrency, type: :numeric, default: 5, aliases: [:c]
  17. option :verbose, type: :boolean, aliases: [:v]
  18. desc 'recount TYPE', 'Update hard-cached counters'
  19. long_desc <<~LONG_DESC
  20. Update hard-cached counters of TYPE by counting referenced
  21. records from scratch. TYPE can be "accounts" or "statuses".
  22. It may take a very long time to finish, depending on the
  23. size of the database.
  24. LONG_DESC
  25. def recount(type)
  26. case type
  27. when 'accounts'
  28. processed, = parallelize_with_progress(Account.local.includes(:account_stat)) do |account|
  29. account_stat = account.account_stat
  30. account_stat.following_count = account.active_relationships.count
  31. account_stat.followers_count = account.passive_relationships.count
  32. account_stat.statuses_count = account.statuses.where.not(visibility: :direct).count
  33. account_stat.save if account_stat.changed?
  34. end
  35. when 'statuses'
  36. processed, = parallelize_with_progress(Status.includes(:status_stat)) do |status|
  37. status_stat = status.status_stat
  38. status_stat.replies_count = status.replies.where.not(visibility: :direct).count
  39. status_stat.reblogs_count = status.reblogs.count
  40. status_stat.favourites_count = status.favourites.count
  41. status_stat.save if status_stat.changed?
  42. end
  43. else
  44. say("Unknown type: #{type}", :red)
  45. exit(1)
  46. end
  47. say
  48. say("OK, recounted #{processed} records", :green)
  49. end
  50. end
  51. end