After maximum ammount of retries discard the job and log exception

This commit is contained in:
Paul Chavard 2021-02-04 19:27:16 +01:00
parent a591d5528e
commit 9156754cf6
9 changed files with 38 additions and 11 deletions

View file

@ -1,6 +1,6 @@
class ApiEntreprise::AssociationJob < ApiEntreprise::Job
def perform(etablissement_id, procedure_id)
etablissement = Etablissement.find(etablissement_id)
find_etablissement(etablissement_id)
etablissement_params = ApiEntreprise::RNAAdapter.new(etablissement.siret, procedure_id).to_params
etablissement.update!(etablissement_params)
end

View file

@ -1,6 +1,6 @@
class ApiEntreprise::AttestationFiscaleJob < ApiEntreprise::Job
def perform(etablissement_id, procedure_id, user_id)
etablissement = Etablissement.find(etablissement_id)
find_etablissement(etablissement_id)
etablissement_params = ApiEntreprise::AttestationFiscaleAdapter.new(etablissement.siret, procedure_id, user_id).to_params
attestation_fiscale_url = etablissement_params.delete(:entreprise_attestation_fiscale_url)
etablissement.upload_attestation_fiscale(attestation_fiscale_url) if attestation_fiscale_url.present?

View file

@ -1,6 +1,6 @@
class ApiEntreprise::AttestationSocialeJob < ApiEntreprise::Job
def perform(etablissement_id, procedure_id)
etablissement = Etablissement.find(etablissement_id)
find_etablissement(etablissement_id)
etablissement_params = ApiEntreprise::AttestationSocialeAdapter.new(etablissement.siret, procedure_id).to_params
attestation_sociale_url = etablissement_params.delete(:entreprise_attestation_sociale_url)
etablissement.upload_attestation_sociale(attestation_sociale_url) if attestation_sociale_url.present?

View file

@ -1,6 +1,6 @@
class ApiEntreprise::BilansBdfJob < ApiEntreprise::Job
def perform(etablissement_id, procedure_id)
etablissement = Etablissement.find(etablissement_id)
find_etablissement(etablissement_id)
etablissement_params = ApiEntreprise::BilansBdfAdapter.new(etablissement.siret, procedure_id).to_params
etablissement.update!(etablissement_params)
end

View file

@ -1,6 +1,6 @@
class ApiEntreprise::EffectifsAnnuelsJob < ApiEntreprise::Job
def perform(etablissement_id, procedure_id)
etablissement = Etablissement.find(etablissement_id)
find_etablissement(etablissement_id)
etablissement_params = ApiEntreprise::EffectifsAnnuelsAdapter.new(etablissement.siret, procedure_id).to_params
etablissement.update!(etablissement_params)
end

View file

@ -1,6 +1,6 @@
class ApiEntreprise::EffectifsJob < ApiEntreprise::Job
def perform(etablissement_id, procedure_id)
etablissement = Etablissement.find(etablissement_id)
find_etablissement(etablissement_id)
# may 2020 is at the moment the most actual info for effectifs endpoint
etablissement_params = ApiEntreprise::EffectifsAdapter.new(etablissement.siret, procedure_id, "2020", "05").to_params
etablissement.update!(etablissement_params)

View file

@ -1,6 +1,6 @@
class ApiEntreprise::EntrepriseJob < ApiEntreprise::Job
def perform(etablissement_id, procedure_id)
etablissement = Etablissement.find(etablissement_id)
find_etablissement(etablissement_id)
etablissement_params = ApiEntreprise::EntrepriseAdapter.new(etablissement.siret, procedure_id).to_params
etablissement.update!(etablissement_params)
end

View file

@ -3,7 +3,7 @@ class ApiEntreprise::ExercicesJob < ApiEntreprise::Job
end
def perform(etablissement_id, procedure_id)
etablissement = Etablissement.find(etablissement_id)
find_etablissement(etablissement_id)
etablissement_params = ApiEntreprise::ExercicesAdapter.new(etablissement.siret, procedure_id).to_params
etablissement.update!(etablissement_params)
end

View file

@ -8,9 +8,12 @@ class ApiEntreprise::Job < ApplicationJob
# - bdf: erreur interne
# so we retry every day for 5 days
# same logic for ServiceUnavailable
retry_on ApiEntreprise::API::Error::ServiceUnavailable,
ApiEntreprise::API::Error::BadGateway,
wait: 1.day
rescue_from(ApiEntreprise::API::Error::ServiceUnavailable) do |exception|
retry_or_discard(exception)
end
rescue_from(ApiEntreprise::API::Error::BadGateway) do |exception|
retry_or_discard(exception)
end
# We guess the backend is slow but not broken
# and the information we are looking for is available
@ -33,7 +36,31 @@ class ApiEntreprise::Job < ApplicationJob
# override ApplicationJob#error to avoid reporting to sentry
end
def log_job_exception(exception)
if etablissement.present?
if etablissement.dossier.present?
etablissement.dossier.log_api_entreprise_job_exception(exception)
elsif etablissement.champ.present?
etablissement.champ.log_fetch_external_data_exception(exception)
end
end
end
def retry_or_discard(exception)
if executions < max_attempts
retry_job wait: 1.day
else
log_job_exception(exception)
end
end
def max_attempts
ENV.fetch("MAX_ATTEMPTS_API_ENTREPRISE_JOBS", DEFAULT_MAX_ATTEMPTS_API_ENTREPRISE_JOBS).to_i
end
attr_reader :etablissement
def find_etablissement(etablissement_id)
@etablissement = Etablissement.find(etablissement_id)
end
end