Skip to content

Commit

Permalink
Delete old retained emails after certain period (forem#14949)
Browse files Browse the repository at this point in the history
* Delete old retained emails after certain period

* Update app/models/email_message.rb

Co-authored-by: Jamie Gaskins <[email protected]>

* Update spec/workers/emails/remove_old_emails_worker_spec.rb

Co-authored-by: Jamie Gaskins <[email protected]>

* Update app/models/email_message.rb

Co-authored-by: Jamie Gaskins <[email protected]>

Co-authored-by: Jamie Gaskins <[email protected]>
  • Loading branch information
benhalpern and Jamie Gaskins authored Oct 12, 2021
1 parent 3a4a273 commit 549336b
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 0 deletions.
19 changes: 19 additions & 0 deletions app/models/email_message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,23 @@ def self.find_for_reports(feedback_message_ids)
select(:to, :subject, :content, :utm_campaign, :feedback_message_id)
.where(feedback_message_id: feedback_message_ids)
end

def self.fast_destroy_old_retained_email_deliveries(destroy_before_timestamp = 3.months.ago)
# We remove email delivery records periodically, except some we retain long term.
# We generally want to retain emails directly sent by human admins.
# The only email currently sent manually are those that are tied directly to a feedback message.
sql = <<~SQL
DELETE FROM ahoy_messages
WHERE ahoy_messages.id IN (
SELECT ahoy_messages.id
FROM ahoy_messages
WHERE sent_at < ? AND feedback_message_id IS NULL
LIMIT 50000
)
SQL

email_sql = EmailMessage.sanitize_sql([sql, destroy_before_timestamp])

BulkSqlDelete.delete_in_batches(email_sql)
end
end
11 changes: 11 additions & 0 deletions app/workers/emails/remove_old_emails_worker.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module Emails
class RemoveOldEmailsWorker
include Sidekiq::Worker

sidekiq_options queue: :low_priority, retry: 10

def perform
EmailMessage.fast_destroy_old_retained_email_deliveries
end
end
end
4 changes: 4 additions & 0 deletions config/schedule.yml
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,10 @@ remove_old_notifications:
description: "Deletes old notifications from the database (runs daily at 05:00 UTC)"
cron: "0 5 * * *"
class: "Notifications::RemoveOldNotificationsWorker"
remove_old_emails:
description: "Deletes old emails we don't need to retain from the database (runs daily at 06:00 UTC)"
cron: "0 6 * * *"
class: "Emails::RemoveOldEmailsWorker"
sync_credits_counter_cache:
description: "Sychronizes counter caches for credits (runs daily at 16:00 UTC)"
cron: "0 16 * * *"
Expand Down
8 changes: 8 additions & 0 deletions spec/models/email_message_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,12 @@

it { is_expected.to belong_to(:feedback_message).optional }
end

describe "#fast_destroy_old_notifications" do
it "bulk deletes emails older than given timestamp" do
allow(BulkSqlDelete).to receive(:delete_in_batches)
described_class.fast_destroy_old_retained_email_deliveries("a_time")
expect(BulkSqlDelete).to have_received(:delete_in_batches).with(a_string_including("< 'a_time'"))
end
end
end
15 changes: 15 additions & 0 deletions spec/workers/emails/remove_old_emails_worker_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
require "rails_helper"

RSpec.describe Emails::RemoveOldEmailsWorker, type: :worker do
include_examples "#enqueues_on_correct_queue", "low_priority"

describe "#perform" do
let(:worker) { subject }

it "fast destroys notifications" do
allow(EmailMessage).to receive(:fast_destroy_old_retained_email_deliveries)
worker.perform
expect(EmailMessage).to have_received(:fast_destroy_old_retained_email_deliveries)
end
end
end

0 comments on commit 549336b

Please sign in to comment.