diff --git a/app/controllers/users/dossiers_controller.rb b/app/controllers/users/dossiers_controller.rb index 61f111fc3..19d30a5e6 100644 --- a/app/controllers/users/dossiers_controller.rb +++ b/app/controllers/users/dossiers_controller.rb @@ -105,7 +105,7 @@ module Users sanitized_siret = siret_model.siret begin etablissement = ApiEntrepriseService.create_etablissement(@dossier, sanitized_siret, current_user.id) - rescue ApiEntreprise::API::RequestFailed, ApiEntreprise::API::BadGateway + rescue ApiEntreprise::API::RequestFailed, ApiEntreprise::API::BadGateway, ApiEntreprise::API::TimedOut return render_siret_error(t('errors.messages.siret_network_error')) end if etablissement.nil? diff --git a/app/jobs/api_entreprise/job.rb b/app/jobs/api_entreprise/job.rb index c3ae9a73e..e6114dd69 100644 --- a/app/jobs/api_entreprise/job.rb +++ b/app/jobs/api_entreprise/job.rb @@ -5,6 +5,8 @@ class ApiEntreprise::Job < ApplicationJob ApiEntreprise::API::BadGateway, wait: 1.day + retry_on ApiEntreprise::API::TimedOut, wait: :exponentially_longer + DEFAULT_MAX_ATTEMPTS_API_ENTREPRISE_JOBS = 5 # If by the time the job runs the Etablissement has been deleted diff --git a/app/lib/api_entreprise/api.rb b/app/lib/api_entreprise/api.rb index 56ecf2ea7..01d786287 100644 --- a/app/lib/api_entreprise/api.rb +++ b/app/lib/api_entreprise/api.rb @@ -27,6 +27,9 @@ class ApiEntreprise::API class ServiceUnavailable < StandardError end + class TimedOut < StandardError + end + def self.entreprise(siren, procedure_id) call_with_siret(ENTREPRISE_RESOURCE_NAME, siren, procedure_id) end @@ -104,6 +107,8 @@ class ApiEntreprise::API raise BadGateway, "url: #{url}" elsif response.code == 503 raise ServiceUnavailable, "url: #{url}" + elsif response.timed_out? + raise TimedOut, "url: #{url}" else raise RequestFailed, <<~TEXT @@ -111,7 +116,6 @@ class ApiEntreprise::API headers: #{response.headers} body: #{response.body} curl message: #{response.return_message} - timeout: #{response.timed_out?} TEXT end end diff --git a/spec/jobs/api_entreprise/job_spec.rb b/spec/jobs/api_entreprise/job_spec.rb index 63e9a5edc..59af2288f 100644 --- a/spec/jobs/api_entreprise/job_spec.rb +++ b/spec/jobs/api_entreprise/job_spec.rb @@ -29,6 +29,13 @@ RSpec.describe ApiEntreprise::Job, type: :job do it { subject } end + + context 'when it is timed out' do + let(:error) { :timed_out } + let(:try) { 5 } + + it { subject } + end end class ExceptionJob < ApiEntreprise::Job @@ -38,6 +45,8 @@ RSpec.describe ApiEntreprise::Job, type: :job do raise ApiEntreprise::API::ServiceUnavailable when :bad_gateway raise ApiEntreprise::API::BadGateway + when :timed_out + raise ApiEntreprise::API::TimedOut else raise StandardError end