Merge pull request #7455 from betagouv/US/fix-cron-DeclarativeProceduresJob-fail-all-on-one-error

bug(DeclarativeProceduresJob.perform_now): fail all next procedure on one dossier failure
This commit is contained in:
mfo 2022-06-10 15:03:37 +02:00 committed by GitHub
commit abb0f8b650
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 1 deletions

View file

@ -2,6 +2,12 @@ class Cron::DeclarativeProceduresJob < Cron::CronJob
self.schedule_expression = "every 1 minute"
def perform(*args)
Procedure.declarative.find_each(&:process_dossiers!)
Procedure.declarative.find_each do |procedure|
begin
procedure.process_dossiers!
rescue => e
Sentry.capture_exception(e)
end
end
end
end

View file

@ -81,4 +81,20 @@ RSpec.describe Cron::DeclarativeProceduresJob, type: :job do
end
end
end
describe 'safer perform' do
let(:state) { Dossier.states.fetch(:en_instruction) }
it 'works no matter if one raise' do
procedure_1 = instance_double("Procedure")
expect(procedure_1).to receive(:process_dossiers!)
procedure_2 = instance_double("Procedure")
expect(procedure_2).to receive(:process_dossiers!).and_raise("boom")
procedure_3 = double(process_dossiers!: true)
expect(procedure_3).to receive(:process_dossiers!)
expect(Procedure).to receive_message_chain(:declarative, :find_each).and_yield(procedure_1).and_yield(procedure_2).and_yield(procedure_3)
Cron::DeclarativeProceduresJob.perform_now
end
end
end