fix(fix_champs_commune_having_value_but_not_external_id_task): timeouting despite classic pattern to find all champs for a specific type. lighten request by selecting only :id and processing batches

This commit is contained in:
mfo 2024-09-10 15:38:25 +02:00
parent 3e1bcb35d6
commit 18137b176c
No known key found for this signature in database
GPG key ID: 7CE3E1F5B794A8EC
2 changed files with 8 additions and 4 deletions

View file

@ -7,10 +7,12 @@ module Maintenance
DEFAULT_INSTRUCTEUR_EMAIL = ENV.fetch('DEFAULT_INSTRUCTEUR_EMAIL') { CONTACT_EMAIL } DEFAULT_INSTRUCTEUR_EMAIL = ENV.fetch('DEFAULT_INSTRUCTEUR_EMAIL') { CONTACT_EMAIL }
def collection def collection
Champs::CommuneChamp.where.not(value: nil) Champs::CommuneChamp.select(:id, :value, :external_id)
end end
def process(champ) def process(champ)
return if !(champ.value.present? && champ.external_id.blank?)
champ.reload
return if !fixable?(champ) return if !fixable?(champ)
response = APIGeoService.commune_by_name_or_postal_code(champ.value) response = APIGeoService.commune_by_name_or_postal_code(champ.value)
@ -36,7 +38,7 @@ module Maintenance
end end
def count def count
# 2.4M champs, count is not an option # osf, count is not an option
end end
private private
@ -61,7 +63,7 @@ module Maintenance
end end
def fixable?(champ) def fixable?(champ)
champ.value.present? && [champ.dossier.en_instruction? || champ.dossier.en_construction?] champ.dossier.en_instruction? || champ.dossier.en_construction?
end end
def notify(message, champ) = Sentry.capture_message(message, extra: { champ: }) def notify(message, champ) = Sentry.capture_message(message, extra: { champ: })

View file

@ -8,7 +8,9 @@ module Maintenance
let(:procedure) { create(:procedure, types_de_champ_public: [{ type: :communes }]) } let(:procedure) { create(:procedure, types_de_champ_public: [{ type: :communes }]) }
let(:dossier) { create(:dossier, state, :with_populated_champs, procedure:) } let(:dossier) { create(:dossier, state, :with_populated_champs, procedure:) }
let(:champ) { dossier.champs.first } let(:champ) { dossier.champs.first }
subject(:process) { described_class.process(champ) } subject(:process) do
described_class.process(champ)
end
context 'when search find one result', vcr: { cassette_name: 'fix-champs-commune-with-one-results' } do context 'when search find one result', vcr: { cassette_name: 'fix-champs-commune-with-one-results' } do
let(:state) { [:en_instruction, :en_construction].sample } let(:state) { [:en_instruction, :en_construction].sample }