fix(auto_archive_procedure_job): AutoArchiveProcedureJob may take longer than its cron delay [everyminutes], when it takes more than one minute, we re-enqueue the same mails

This commit is contained in:
mfo 2024-11-18 12:35:33 +01:00
parent 4b740f8f29
commit babdf9536f
No known key found for this signature in database
GPG key ID: 7CE3E1F5B794A8EC
4 changed files with 62 additions and 19 deletions

View file

@ -0,0 +1,16 @@
# frozen_string_literal: true
class AutoArchiveProcedureDossiersJob < ApplicationJob
def perform(procedure)
procedure
.dossiers
.state_en_construction
.find_each do |d|
begin
d.passer_automatiquement_en_instruction!
rescue StandardError => e
Sentry.capture_exception(e, extra: { procedure_id: procedure.id })
end
end
end
end

View file

@ -2,18 +2,15 @@
class Cron::AutoArchiveProcedureJob < Cron::CronJob
self.schedule_expression = "every 1 minute"
queue_as :critical
def perform(*args)
procedures_to_close.each do |procedure|
# A buggy procedure should NEVER prevent the closing of another procedure
# we therefore exceptionally add a `begin resue` block.
begin
procedure
.dossiers
.state_en_construction
.find_each(&:passer_automatiquement_en_instruction!)
procedure.close!
AutoArchiveProcedureDossiersJob.perform_later(procedure)
rescue StandardError => e
Sentry.capture_exception(e, extra: { procedure_id: procedure.id })
end

View file

@ -0,0 +1,44 @@
# frozen_string_literal: true
RSpec.describe AutoArchiveProcedureDossiersJob, type: :job do
let!(:procedure) { create(:procedure, :published, :with_instructeur) }
let!(:job) { AutoArchiveProcedureDossiersJob.new }
before do
procedure.auto_archive_on = 1.day.ago.to_date
procedure.save(validate: false)
end
subject { job.perform(procedure) }
context "when procedures have auto_archive_on set on yesterday or today" do
let!(:dossier1) { create(:dossier, procedure: procedure) }
let!(:dossier2) { create(:dossier, :en_construction, procedure: procedure) }
let!(:dossier3) { create(:dossier, :en_construction, procedure: procedure) }
let!(:dossier4) { create(:dossier, :en_construction, procedure: procedure) }
let!(:dossier5) { create(:dossier, :en_instruction, procedure: procedure) }
let!(:dossier6) { create(:dossier, :accepte, procedure: procedure) }
let!(:dossier7) { create(:dossier, :refuse, procedure: procedure) }
let!(:dossier8) { create(:dossier, :sans_suite, procedure: procedure) }
let(:last_operation) { dossier2.dossier_operation_logs.last }
before do
subject
[dossier1, dossier2, dossier3, dossier4, dossier5, dossier6, dossier7, dossier8].each(&:reload)
procedure.reload
end
it {
expect(dossier1.state).to eq Dossier.states.fetch(:brouillon)
expect(dossier2.state).to eq Dossier.states.fetch(:en_instruction)
expect(last_operation.operation).to eq('passer_en_instruction')
expect(last_operation.automatic_operation?).to be_truthy
expect(dossier3.state).to eq Dossier.states.fetch(:en_instruction)
expect(dossier4.state).to eq Dossier.states.fetch(:en_instruction)
expect(dossier5.state).to eq Dossier.states.fetch(:en_instruction)
expect(dossier6.state).to eq Dossier.states.fetch(:accepte)
expect(dossier7.state).to eq Dossier.states.fetch(:refuse)
expect(dossier8.state).to eq Dossier.states.fetch(:sans_suite)
}
end
end

View file

@ -46,20 +46,6 @@ RSpec.describe Cron::AutoArchiveProcedureJob, type: :job do
procedure_aujourdhui.reload
end
it {
expect(dossier1.state).to eq Dossier.states.fetch(:brouillon)
expect(dossier2.state).to eq Dossier.states.fetch(:en_instruction)
expect(last_operation.operation).to eq('passer_en_instruction')
expect(last_operation.automatic_operation?).to be_truthy
expect(dossier3.state).to eq Dossier.states.fetch(:en_instruction)
expect(dossier4.state).to eq Dossier.states.fetch(:en_instruction)
expect(dossier5.state).to eq Dossier.states.fetch(:en_instruction)
expect(dossier6.state).to eq Dossier.states.fetch(:accepte)
expect(dossier7.state).to eq Dossier.states.fetch(:refuse)
expect(dossier8.state).to eq Dossier.states.fetch(:sans_suite)
expect(dossier9.state).to eq Dossier.states.fetch(:en_instruction)
}
it {
expect(procedure_hier.close?).to eq true
expect(procedure_aujourdhui.close?).to eq true