fix(data): avoid destroying 2M dossiers in one day due to error in Cron::ExpiredDossiersTermineDeletionJob

This commit is contained in:
Martin 2024-03-04 10:52:49 +01:00
parent 9280ccc23d
commit eb6b23347b
2 changed files with 44 additions and 0 deletions

View file

@ -0,0 +1,24 @@
# frozen_string_literal: true
module Maintenance
class SpreadDossierDeletionTask < MaintenanceTasks::Task
ERROR_OCCURED_AT = Date.new(2024, 2, 14)
SPREAD_DURATION_IN_DAYS = 150
def collection
Dossier.where(termine_close_to_expiration_notice_sent_at: ERROR_OCCURED_AT)
.in_batches
end
def process(element)
element.update_all(termine_close_to_expiration_notice_sent_at: ERROR_OCCURED_AT + random_date_spread.days)
end
# since we do not keep track of current batch idx,
# delay termine_close_to_expiration_notice_sent_at using random approach
# should be good enough
def random_date_spread
rand(1..SPREAD_DURATION_IN_DAYS)
end
end
end

View file

@ -0,0 +1,20 @@
# frozen_string_literal: true
require "rails_helper"
module Maintenance
RSpec.describe SpreadDossierDeletionTask do
describe "#process" do
let(:dossiers) { Dossier.all }
let(:dossier_1) { create(:dossier, termine_close_to_expiration_notice_sent_at: Date.new(2024, 2, 14)) }
let(:dossier_2) { create(:dossier, termine_close_to_expiration_notice_sent_at: Date.new(2024, 2, 14)) }
let(:dossier_3) { create(:dossier, termine_close_to_expiration_notice_sent_at: Date.new(2024, 2, 14)) }
let(:dossier_4) { create(:dossier, termine_close_to_expiration_notice_sent_at: Date.new(2024, 2, 14)) }
subject(:process) { described_class.process(dossiers) }
it "works" do
expect { subject }.to change { dossier_1.reload.termine_close_to_expiration_notice_sent_at }
end
end
end
end