[#5675] for all api entreprise job, retry on 502, 503
This commit is contained in:
parent
02e7de0003
commit
9eab310565
8 changed files with 59 additions and 22 deletions
|
@ -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
|
||||
rescue ApiEntreprise::API::RequestFailed, ApiEntreprise::API::BadGateway
|
||||
return render_siret_error(t('errors.messages.siret_network_error'))
|
||||
end
|
||||
if etablissement.nil?
|
||||
|
|
|
@ -1,6 +1,4 @@
|
|||
class ApiEntreprise::AttestationSocialeJob < ApiEntreprise::Job
|
||||
retry_on ApiEntreprise::API::ServiceUnavailable, wait: 1.day
|
||||
|
||||
def perform(etablissement_id, procedure_id)
|
||||
etablissement = Etablissement.find(etablissement_id)
|
||||
etablissement_params = ApiEntreprise::AttestationSocialeAdapter.new(etablissement.siret, procedure_id).to_params
|
||||
|
|
|
@ -1,6 +1,10 @@
|
|||
class ApiEntreprise::Job < ApplicationJob
|
||||
queue_as :api_entreprise
|
||||
|
||||
retry_on ApiEntreprise::API::ServiceUnavailable,
|
||||
ApiEntreprise::API::BadGateway,
|
||||
wait: 1.day
|
||||
|
||||
DEFAULT_MAX_ATTEMPTS_API_ENTREPRISE_JOBS = 5
|
||||
|
||||
# If by the time the job runs the Etablissement has been deleted
|
||||
|
|
|
@ -21,6 +21,9 @@ class ApiEntreprise::API
|
|||
class BadFormatRequest < StandardError
|
||||
end
|
||||
|
||||
class BadGateway < StandardError
|
||||
end
|
||||
|
||||
class ServiceUnavailable < StandardError
|
||||
end
|
||||
|
||||
|
@ -97,6 +100,8 @@ class ApiEntreprise::API
|
|||
raise ResourceNotFound, "url: #{url}"
|
||||
elsif response.code == 400
|
||||
raise BadFormatRequest, "url: #{url}"
|
||||
elsif response.code == 502
|
||||
raise BadGateway, "url: #{url}"
|
||||
elsif response.code == 503
|
||||
raise ServiceUnavailable, "url: #{url}"
|
||||
else
|
||||
|
|
|
@ -23,20 +23,4 @@ RSpec.describe ApiEntreprise::AttestationSocialeJob, type: :job do
|
|||
subject
|
||||
expect(Etablissement.find(etablissement.id).entreprise_attestation_sociale).to be_attached
|
||||
end
|
||||
|
||||
context 'when ApiEntreprise::API::ServiceUnavailable is raised' do
|
||||
# https://api.rubyonrails.org/classes/ActiveJob/Exceptions/ClassMethods.html#method-i-retry_on
|
||||
# retry on will try 5 times and then bubble up the error
|
||||
it 'makes 5 attempts' do
|
||||
assert_performed_jobs 5 do
|
||||
ServiceUnavailableJob.perform_later rescue ApiEntreprise::API::ServiceUnavailable
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
class ServiceUnavailableJob < ApiEntreprise::AttestationSocialeJob
|
||||
def perform
|
||||
raise ApiEntreprise::API::ServiceUnavailable
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
46
spec/jobs/api_entreprise/job_spec.rb
Normal file
46
spec/jobs/api_entreprise/job_spec.rb
Normal file
|
@ -0,0 +1,46 @@
|
|||
include ActiveJob::TestHelper
|
||||
|
||||
RSpec.describe ApiEntreprise::Job, type: :job do
|
||||
# https://api.rubyonrails.org/classes/ActiveJob/Exceptions/ClassMethods.html#method-i-retry_on
|
||||
context 'when an exception is raised' do
|
||||
subject do
|
||||
assert_performed_jobs(try) do
|
||||
ExceptionJob.perform_later(error) rescue StandardError
|
||||
end
|
||||
end
|
||||
|
||||
context 'when it is a service_unavaible' do
|
||||
let(:error) { :standard_error }
|
||||
let(:try) { 1 }
|
||||
|
||||
it { subject }
|
||||
end
|
||||
|
||||
context 'when it is a service_unavaible' do
|
||||
let(:error) { :service_unavaible }
|
||||
let(:try) { 5 }
|
||||
|
||||
it { subject }
|
||||
end
|
||||
|
||||
context 'when it is a bad gateway' do
|
||||
let(:error) { :bad_gateway }
|
||||
let(:try) { 5 }
|
||||
|
||||
it { subject }
|
||||
end
|
||||
end
|
||||
|
||||
class ExceptionJob < ApiEntreprise::Job
|
||||
def perform(exception)
|
||||
case exception
|
||||
when :service_unavaible
|
||||
raise ApiEntreprise::API::ServiceUnavailable
|
||||
when :bad_gateway
|
||||
raise ApiEntreprise::API::BadGateway
|
||||
else
|
||||
raise StandardError
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -12,13 +12,13 @@ describe ApiEntreprise::API do
|
|||
allow_any_instance_of(ApiEntrepriseToken).to receive(:expired?).and_return(false)
|
||||
end
|
||||
|
||||
context 'when the service is unavailable' do
|
||||
context 'when the service throws a bad gateaway exception' do
|
||||
let(:siren) { '111111111' }
|
||||
let(:status) { 502 }
|
||||
let(:body) { File.read('spec/fixtures/files/api_entreprise/entreprises_unavailable.json') }
|
||||
|
||||
it 'raises ApiEntreprise::API::RequestFailed' do
|
||||
expect { subject }.to raise_error(ApiEntreprise::API::RequestFailed)
|
||||
expect { subject }.to raise_error(ApiEntreprise::API::BadGateway)
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -81,7 +81,7 @@ describe ApiEntreprise::EntrepriseAdapter do
|
|||
|
||||
context "when the service is unavailable" do
|
||||
let(:body) { File.read('spec/fixtures/files/api_entreprise/entreprises_unavailable.json') }
|
||||
let(:status) { 502 }
|
||||
let(:status) { 500 }
|
||||
|
||||
it 'raises an exception' do
|
||||
expect { subject }.to raise_error(ApiEntreprise::API::RequestFailed)
|
||||
|
|
Loading…
Reference in a new issue