Merge pull request #5704 from betagouv/retry_503_in_social_attestation

cherche a télécharger l'attestation sociale tous les jours pendant 5 jours
This commit is contained in:
LeSim 2020-10-28 17:05:36 +01:00 committed by GitHub
commit ac1c53f8f5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 27 additions and 2 deletions

View file

@ -16,7 +16,7 @@ class Champs::SiretController < ApplicationController
begin begin
etablissement = find_etablissement_with_siret etablissement = find_etablissement_with_siret
rescue ApiEntreprise::API::RequestFailed rescue ApiEntreprise::API::RequestFailed, ApiEntreprise::API::ServiceUnavailable
return siret_error(:network_error) return siret_error(:network_error)
end end
if etablissement.nil? if etablissement.nil?

View file

@ -1,4 +1,6 @@
class ApiEntreprise::AttestationSocialeJob < ApiEntreprise::Job class ApiEntreprise::AttestationSocialeJob < ApiEntreprise::Job
retry_on ApiEntreprise::API::ServiceUnavailable, wait: 1.day
def perform(etablissement_id, procedure_id) def perform(etablissement_id, procedure_id)
etablissement = Etablissement.find(etablissement_id) etablissement = Etablissement.find(etablissement_id)
etablissement_params = ApiEntreprise::AttestationSocialeAdapter.new(etablissement.siret, procedure_id).to_params etablissement_params = ApiEntreprise::AttestationSocialeAdapter.new(etablissement.siret, procedure_id).to_params

View file

@ -20,6 +20,9 @@ class ApiEntreprise::API
class BadFormatRequest < StandardError class BadFormatRequest < StandardError
end end
class ServiceUnavailable < StandardError
end
def self.entreprise(siren, procedure_id) def self.entreprise(siren, procedure_id)
call(ENTREPRISE_RESOURCE_NAME, siren, procedure_id) call(ENTREPRISE_RESOURCE_NAME, siren, procedure_id)
end end
@ -77,6 +80,8 @@ class ApiEntreprise::API
raise ResourceNotFound, "url: #{url}" raise ResourceNotFound, "url: #{url}"
elsif response.code == 400 elsif response.code == 400
raise BadFormatRequest, "url: #{url}" raise BadFormatRequest, "url: #{url}"
elsif response.code == 503
raise ServiceUnavailable, "url: #{url}"
else else
raise RequestFailed, "HTTP Error Code: #{response.code} for #{url}\nheaders: #{response.headers}\nbody: #{response.body}" raise RequestFailed, "HTTP Error Code: #{response.code} for #{url}\nheaders: #{response.headers}\nbody: #{response.body}"
end end

View file

@ -1,3 +1,5 @@
include ActiveJob::TestHelper
RSpec.describe ApiEntreprise::AttestationSocialeJob, type: :job do RSpec.describe ApiEntreprise::AttestationSocialeJob, type: :job do
let(:etablissement) { create(:etablissement, siret: siret) } let(:etablissement) { create(:etablissement, siret: siret) }
let(:siret) { '41816609600069' } let(:siret) { '41816609600069' }
@ -21,4 +23,20 @@ RSpec.describe ApiEntreprise::AttestationSocialeJob, type: :job do
subject subject
expect(Etablissement.find(etablissement.id).entreprise_attestation_sociale).to be_attached expect(Etablissement.find(etablissement.id).entreprise_attestation_sociale).to be_attached
end 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 end