amelioration(EmailEvent): purge les email_events vieux de plus de un mois

Update app/models/email_event.rb

Co-authored-by: Colin Darie <colin@darie.eu>

Update app/jobs/cron/purge_old_email_event_job.rb

Co-authored-by: Colin Darie <colin@darie.eu>
This commit is contained in:
Martin 2023-02-03 17:09:58 +01:00
parent 54fa4ddc3d
commit f4cc8078b4
3 changed files with 25 additions and 0 deletions

View file

@ -0,0 +1,7 @@
class Cron::PurgeOldEmailEventJob < Cron::CronJob
self.schedule_expression = "every week at 3:00"
def perform
EmailEvent.outdated.destroy_all
end
end

View file

@ -13,14 +13,18 @@
# message_id :string
#
class EmailEvent < ApplicationRecord
RETENTION_DURATION = 1.month
enum status: {
dispatched: 'dispatched',
dispatch_error: 'dispatch_error'
}
scope :dolist, -> { dolist_smtp.or(dolist_api) }
scope :dolist_smtp, -> { where(method: 'dolist_smtp') }
scope :dolist_api, -> { where(method: 'dolist_api') }
scope :sendinblue, -> { where(method: 'sendinblue') }
scope :outdated, -> { where("created_at < ?", RETENTION_DURATION.ago) }
class << self
def create_from_message!(message, status:)

View file

@ -0,0 +1,14 @@
RSpec.describe Cron::PurgeOldEmailEventJob, type: :job do
describe 'perform' do
subject { Cron::PurgeOldEmailEventJob.perform_now }
let(:older_than_retention_duration) { create(:email_event, :dolist, created_at: Time.zone.now.utc - (EmailEvent::RETENTION_DURATION + 1.day)) }
let(:more_recent_than_retention_duraiton) { create(:email_event, :dolist, created_at: Time.zone.now.utc - (EmailEvent::RETENTION_DURATION - 1.day)) }
before do
older_than_retention_duration
more_recent_than_retention_duraiton
end
it { expect { subject }.to change { EmailEvent.count }.by(-1) }
it { expect { subject }.to change { EmailEvent.exists?(id: older_than_retention_duration.id) }.from(true).to(false) }
end
end