Skip to content

Commit

Permalink
Create and schedule job to delete old alert runs
Browse files Browse the repository at this point in the history
Daily job to delete any Subscription Alert Run created longer than 7
days ago.

We only need the alert runs to check if the alert was already triggered
for the current date.
At the moment, the table contains 16M records for over 100k
subscriptions.
We do not need that data at all.

Reducing it to only keeping the latest week will greatly reduce the
table size without compromising the recent runs check.
  • Loading branch information
scruti committed Jan 13, 2025
1 parent 3ecbdff commit 9725f54
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
7 changes: 7 additions & 0 deletions app/jobs/delete_old_alert_runs_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class DeleteOldAlertRunsJob < ApplicationJob
queue_as :low

def perform
AlertRun.where(run_on: ...1.week.ago).destroy_all
end
end
5 changes: 5 additions & 0 deletions config/schedule.yml
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@ delete_old_applications:
class: 'DeleteOldNonDraftJobApplicationsJob'
queue: low

delete_old_alert_runs:
cron: '10 5 * * *' # "At 05:10."
class: 'DeleteOldAlertRunsJob'
queue: low

refresh_markers:
cron: '0 6 * * *' # "At 06:00."
class: 'RefreshMarkersJob'
Expand Down
21 changes: 21 additions & 0 deletions spec/jobs/delete_old_alert_runs_job_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
require "rails_helper"

RSpec.describe DeleteOldAlertRunsJob do
it "deletes alert runs older than a week" do
alert_run = create(:alert_run, run_on: 8.days.ago)
described_class.perform_now
expect(AlertRun).not_to exist(alert_run.id)
end

it "does not delete alert runs from exactly a week ago" do
alert_run = create(:alert_run, run_on: 7.days.ago)
described_class.perform_now
expect(AlertRun).to exist(alert_run.id)
end

it "does not delete alert runs newer than a week" do
alert_run = create(:alert_run, run_on: 6.days.ago)
described_class.perform_now
expect(AlertRun).to exist(alert_run.id)
end
end

0 comments on commit 9725f54

Please sign in to comment.