retry exponentially on api entreprise timeout

This commit is contained in:
simon lehericey 2020-12-03 10:39:51 +01:00
parent 78a40b5176
commit f56235c7ee
4 changed files with 17 additions and 2 deletions

View file

@ -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?

View file

@ -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

View file

@ -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

View file

@ -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