about summary refs log tree commit diff
path: root/app/workers/scheduler/feed_cleanup_scheduler.rb
blob: bf5e20757007d893913d077021256eb03ceba062 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# frozen_string_literal: true

class Scheduler::FeedCleanupScheduler
  include Sidekiq::Worker
  include Redisable

  sidekiq_options unique: :until_executed, retry: 0

  def perform
    clean_home_feeds!
    clean_list_feeds!
  end

  private

  def clean_home_feeds!
    clean_feeds!(inactive_account_ids, :home)
  end

  def clean_list_feeds!
    clean_feeds!(inactive_list_ids, :list)
  end

  def clean_feeds!(ids, type)
    reblogged_id_sets = {}

    redis.pipelined do
      ids.each do |feed_id|
        redis.del(feed_manager.key(type, feed_id))
        reblog_key = feed_manager.key(type, feed_id, 'reblogs')
        # We collect a future for this: we don't block while getting
        # it, but we can iterate over it later.
        reblogged_id_sets[feed_id] = redis.zrange(reblog_key, 0, -1)
        redis.del(reblog_key)
      end
    end

    # Remove all of the reblog tracking keys we just removed the
    # references to.
    redis.pipelined do
      reblogged_id_sets.each do |feed_id, future|
        future.value.each do |reblogged_id|
          reblog_set_key = feed_manager.key(type, feed_id, "reblogs:#{reblogged_id}")
          redis.del(reblog_set_key)
        end
      end
    end
  end

  def inactive_account_ids
    @inactive_account_ids ||= User.confirmed.inactive.pluck(:account_id)
  end

  def inactive_list_ids
    List.where(account_id: inactive_account_ids).pluck(:id)
  end

  def feed_manager
    FeedManager.instance
  end
end