feed_cleanup_scheduler.rb 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. # frozen_string_literal: true
  2. require 'sidekiq-scheduler'
  3. class Scheduler::FeedCleanupScheduler
  4. include Sidekiq::Worker
  5. def perform
  6. clean_home_feeds!
  7. clean_list_feeds!
  8. end
  9. private
  10. def clean_home_feeds!
  11. clean_feeds!(inactive_account_ids, :home)
  12. end
  13. def clean_list_feeds!
  14. clean_feeds!(inactive_list_ids, :list)
  15. end
  16. def clean_feeds!(ids, type)
  17. reblogged_id_sets = {}
  18. redis.pipelined do
  19. ids.each do |feed_id|
  20. redis.del(feed_manager.key(type, feed_id))
  21. reblog_key = feed_manager.key(type, feed_id, 'reblogs')
  22. # We collect a future for this: we don't block while getting
  23. # it, but we can iterate over it later.
  24. reblogged_id_sets[feed_id] = redis.zrange(reblog_key, 0, -1)
  25. redis.del(reblog_key)
  26. end
  27. end
  28. # Remove all of the reblog tracking keys we just removed the
  29. # references to.
  30. redis.pipelined do
  31. reblogged_id_sets.each do |feed_id, future|
  32. future.value.each do |reblogged_id|
  33. reblog_set_key = feed_manager.key(type, feed_id, "reblogs:#{reblogged_id}")
  34. redis.del(reblog_set_key)
  35. end
  36. end
  37. end
  38. end
  39. def inactive_account_ids
  40. @inactive_account_ids ||= User.confirmed.inactive.pluck(:account_id)
  41. end
  42. def inactive_list_ids
  43. List.where(account_id: inactive_account_ids).pluck(:id)
  44. end
  45. def feed_manager
  46. FeedManager.instance
  47. end
  48. def redis
  49. Redis.current
  50. end
  51. end