feed_cleanup_scheduler.rb 1.4 KB

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