ensure a buggy procedure does not block the others from being closed

This commit is contained in:
simon lehericey 2021-01-05 10:03:35 +01:00
parent ec1f6a7bfb
commit 650a598698
2 changed files with 37 additions and 7 deletions

View file

@ -2,13 +2,25 @@ class Cron::AutoArchiveProcedureJob < Cron::CronJob
self.schedule_expression = "every 1 minute"
def perform(*args)
Procedure.publiees.where("auto_archive_on <= ?", Time.zone.today).each do |procedure|
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!
rescue StandardError => e
Raven.capture_exception(e, extra: { procedure_id: procedure.id })
end
end
end
def procedures_to_close
Procedure
.publiees
.where("auto_archive_on <= ?", Time.zone.today)
end
end

View file

@ -3,8 +3,9 @@ RSpec.describe Cron::AutoArchiveProcedureJob, type: :job do
let!(:procedure_hier) { create(:procedure, :published, :with_instructeur, auto_archive_on: 1.day.ago.to_date) }
let!(:procedure_aujourdhui) { create(:procedure, :published, :with_instructeur, auto_archive_on: Time.zone.today) }
let!(:procedure_demain) { create(:procedure, :published, :with_instructeur, auto_archive_on: 1.day.from_now.to_date) }
let!(:job) { Cron::AutoArchiveProcedureJob.new }
subject { Cron::AutoArchiveProcedureJob.new.perform }
subject { job.perform }
context "when procedures have no auto_archive_on" do
before do
@ -63,4 +64,21 @@ RSpec.describe Cron::AutoArchiveProcedureJob, type: :job do
it { expect(procedure_demain.close?).to eq false }
end
context 'when an error occurs' do
let!(:buggy_procedure) { create(:procedure, :published, :with_instructeur, auto_archive_on: 1.day.ago.to_date) }
before do
error = StandardError.new('nop')
expect(buggy_procedure).to receive(:close!).and_raise(error)
expect(job).to receive(:procedures_to_close).and_return([buggy_procedure, procedure_hier])
expect(Raven).to receive(:capture_exception).with(error, extra: { procedure_id: buggy_procedure.id })
subject
end
it "should close all the procedure" do
expect(procedure_hier.reload.close?).to eq true
end
end
end