Create rake Task to Destroy Old Notifications (#5180) [deploy]

This commit is contained in:
Molly Struve 2019-12-26 13:17:34 -06:00 committed by GitHub
parent d1cb715858
commit f86f93bc8a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 22 additions and 2 deletions

View file

@ -136,6 +136,10 @@ class Notification < ApplicationRecord
Notifications::UpdateJob.perform_later(notifiable.id, notifiable.class.name, action)
end
def fast_destroy_old_notifications(destroy_before_timestamp = 4.months.ago)
Notification.where("created_at < ?", destroy_before_timestamp).in_batches(of: 10_000).delete_all
end
private
def user_data(user)

View file

@ -37,8 +37,8 @@ task expire_old_listings: :environment do
end
end
task clear_memory_if_too_high: :environment do
Rails.cache.clear if Rails.cache.stats.flatten[1]["bytes"].to_i > 9_650_000_000
task remove_old_notifications: :environment do
Notification.fast_destroy_old_notifications
end
task save_nil_hotness_scores: :environment do

View file

@ -1,4 +1,7 @@
FactoryBot.define do
factory :notification do
association :user, factory: :user, strategy: :create
association :organization, factory: :organization, strategy: :create
notifiable { create(:article) }
end
end

View file

@ -517,4 +517,17 @@ RSpec.describe Notification, type: :model do
end
end
end
describe "#fast_destroy_old_notifications" do
it "bulk deletes notifications older than 4 months by default" do
create_list :notification, 5
described_class.last.update(created_at: 5.months.ago)
expect { described_class.fast_destroy_old_notifications }.to change(described_class, :count).by(-1)
end
it "bulk deletes notifications older than a given timestamp" do
create_list :notification, 5
expect { described_class.fast_destroy_old_notifications(Time.zone.now) }.to change(described_class, :count).by(-5)
end
end
end