-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create and schedule job to delete old alert runs
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
Showing
3 changed files
with
33 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |