diff --git a/app/jobs/api_entreprise/job.rb b/app/jobs/api_entreprise/job.rb index 371d769e7..25e299b6f 100644 --- a/app/jobs/api_entreprise/job.rb +++ b/app/jobs/api_entreprise/job.rb @@ -3,6 +3,10 @@ class ApiEntreprise::Job < ApplicationJob DEFAULT_MAX_ATTEMPTS_API_ENTREPRISE_JOBS = 5 + # If by the time the job runs the Etablissement has been deleted + # (it can happen through EtablissementUpdateJob for instance), ignore the job + discard_on ActiveRecord::RecordNotFound + rescue_from(ApiEntreprise::API::ResourceNotFound) do |exception| error(self, exception) end diff --git a/spec/jobs/api_entreprise/association_job_spec.rb b/spec/jobs/api_entreprise/association_job_spec.rb index 8d8b3cb94..e77e81edf 100644 --- a/spec/jobs/api_entreprise/association_job_spec.rb +++ b/spec/jobs/api_entreprise/association_job_spec.rb @@ -18,4 +18,14 @@ RSpec.describe ApiEntreprise::AssociationJob, type: :job do subject expect(Etablissement.find(etablissement.id).association_rna).to eq('W595001988') end + + context "when the etablissement has been deleted" do + before do + allow_any_instance_of(Etablissement).to receive(:find) { raise ActiveRecord::RecordNotFound } + end + + it "ignores the error" do + expect { subject }.not_to raise_error + end + end end