Class: RailsInformant::PurgeJob

Inherits:
ApplicationJob show all
Defined in:
app/jobs/rails_informant/purge_job.rb

Instance Method Summary collapse

Instance Method Details

#performObject



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'app/jobs/rails_informant/purge_job.rb', line 6

def perform
  return unless RailsInformant.retention_days

  cutoff = RailsInformant.retention_days.days.ago

  # IDs referenced as duplicate targets must be kept (subquery avoids TOCTOU)
  duplicate_target_ids = ErrorGroup.where(status: "duplicate")
    .where.not(duplicate_of_id: nil)
    .distinct
    .select(:duplicate_of_id)

  # Split into separate queries so each can hit its composite index cleanly
  resolved_scope = ErrorGroup.where(status: "resolved").where(resolved_at: ...cutoff).where.not(id: duplicate_target_ids)
  ignored_scope = ErrorGroup.where(status: "ignored").where(updated_at: ...cutoff).where.not(id: duplicate_target_ids)

  [ ignored_scope, resolved_scope ].each do |purgeable|
    purgeable.in_batches(of: 500) do |batch|
      Occurrence.where(error_group_id: batch.select(:id)).delete_all
      batch.delete_all
    end
  end
end