summaryrefslogtreecommitdiff
path: root/app/workers/remove_unaccepted_member_invites_worker.rb
blob: 96f60b5fa12adfe75e53ccf898bff4315f7a6994 (plain)
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
# frozen_string_literal: true

class RemoveUnacceptedMemberInvitesWorker # rubocop:disable Scalability/IdempotentWorker
  include ApplicationWorker

  data_consistency :always

  include CronjobQueue # rubocop:disable Scalability/CronWorkerContext

  feature_category :system_access
  urgency :low
  idempotent!

  EXPIRATION_THRESHOLD = 90.days
  BATCH_SIZE = 10_000

  def perform
    # We need to check for user_id IS NULL because we have accepted invitations
    # in the database where we did not clear the invite_token. We do not
    # want to accidentally delete those members.
    loop do
      # rubocop: disable CodeReuse/ActiveRecord
      inner_query = Member
                      .select(:id)
                      .invite
                      .created_before(EXPIRATION_THRESHOLD.ago)
                      .where(user_id: nil)
                      .limit(BATCH_SIZE)

      records_deleted = Member.where(id: inner_query).delete_all
      # rubocop: enable CodeReuse/ActiveRecord

      break if records_deleted == 0
    end
  end
end