2020-08-05 18:40:47 +02:00
|
|
|
class APIEntreprise::Job < ApplicationJob
|
2020-12-03 11:36:26 +01:00
|
|
|
DEFAULT_MAX_ATTEMPTS_API_ENTREPRISE_JOBS = 5
|
|
|
|
|
2020-09-22 17:14:31 +02:00
|
|
|
queue_as :api_entreprise
|
|
|
|
|
2020-12-03 11:36:26 +01:00
|
|
|
# BadGateway could mean
|
|
|
|
# - acoss: réessayer ultérieurement
|
|
|
|
# - bdf: erreur interne
|
|
|
|
# so we retry every day for 5 days
|
|
|
|
# same logic for ServiceUnavailable
|
2020-08-05 18:40:47 +02:00
|
|
|
rescue_from(APIEntreprise::API::Error::ServiceUnavailable) do |exception|
|
2021-02-04 19:27:16 +01:00
|
|
|
retry_or_discard(exception)
|
|
|
|
end
|
2020-08-05 18:40:47 +02:00
|
|
|
rescue_from(APIEntreprise::API::Error::BadGateway) do |exception|
|
2021-02-04 19:27:16 +01:00
|
|
|
retry_or_discard(exception)
|
|
|
|
end
|
2020-12-02 17:17:00 +01:00
|
|
|
|
2020-12-03 11:36:26 +01:00
|
|
|
# We guess the backend is slow but not broken
|
|
|
|
# and the information we are looking for is available
|
|
|
|
# so we retry few seconds later (exponentially to avoid overload)
|
2020-08-05 18:40:47 +02:00
|
|
|
retry_on APIEntreprise::API::Error::TimedOut, wait: :exponentially_longer
|
2020-12-03 10:39:51 +01:00
|
|
|
|
2020-09-28 14:37:34 +02:00
|
|
|
# If by the time the job runs the Etablissement has been deleted
|
|
|
|
# (it can happen through EtablissementUpdateJob for instance), ignore the job
|
|
|
|
discard_on ActiveRecord::RecordNotFound
|
|
|
|
|
2020-08-05 18:40:47 +02:00
|
|
|
rescue_from(APIEntreprise::API::Error::ResourceNotFound) do |exception|
|
2020-06-11 23:09:50 +02:00
|
|
|
error(self, exception)
|
|
|
|
end
|
|
|
|
|
2020-08-05 18:40:47 +02:00
|
|
|
rescue_from(APIEntreprise::API::Error::BadFormatRequest) do |exception|
|
2020-06-11 23:09:50 +02:00
|
|
|
error(self, exception)
|
|
|
|
end
|
|
|
|
|
2020-07-07 16:56:13 +02:00
|
|
|
def error(job, exception)
|
|
|
|
# override ApplicationJob#error to avoid reporting to sentry
|
|
|
|
end
|
|
|
|
|
2021-02-04 19:27:16 +01:00
|
|
|
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
|
|
|
|
|
2020-06-02 20:13:38 +02:00
|
|
|
def max_attempts
|
2020-06-16 15:47:24 +02:00
|
|
|
ENV.fetch("MAX_ATTEMPTS_API_ENTREPRISE_JOBS", DEFAULT_MAX_ATTEMPTS_API_ENTREPRISE_JOBS).to_i
|
2020-06-02 20:13:38 +02:00
|
|
|
end
|
2021-02-04 19:27:16 +01:00
|
|
|
|
|
|
|
attr_reader :etablissement
|
|
|
|
|
|
|
|
def find_etablissement(etablissement_id)
|
|
|
|
@etablissement = Etablissement.find(etablissement_id)
|
|
|
|
end
|
2020-06-02 20:13:38 +02:00
|
|
|
end
|