From 18137b176cdf8da681bf02df639582284662c510 Mon Sep 17 00:00:00 2001 From: mfo Date: Tue, 10 Sep 2024 15:38:25 +0200 Subject: [PATCH] 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 --- ...hamps_commune_having_value_but_not_external_id_task.rb | 8 +++++--- ..._commune_having_value_but_not_external_id_task_spec.rb | 4 +++- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/app/tasks/maintenance/fix_champs_commune_having_value_but_not_external_id_task.rb b/app/tasks/maintenance/fix_champs_commune_having_value_but_not_external_id_task.rb index da6fe8dd4..7ccf5ce32 100644 --- a/app/tasks/maintenance/fix_champs_commune_having_value_but_not_external_id_task.rb +++ b/app/tasks/maintenance/fix_champs_commune_having_value_but_not_external_id_task.rb @@ -7,10 +7,12 @@ module Maintenance DEFAULT_INSTRUCTEUR_EMAIL = ENV.fetch('DEFAULT_INSTRUCTEUR_EMAIL') { CONTACT_EMAIL } def collection - Champs::CommuneChamp.where.not(value: nil) + Champs::CommuneChamp.select(:id, :value, :external_id) end def process(champ) + return if !(champ.value.present? && champ.external_id.blank?) + champ.reload return if !fixable?(champ) response = APIGeoService.commune_by_name_or_postal_code(champ.value) @@ -36,7 +38,7 @@ module Maintenance end def count - # 2.4M champs, count is not an option + # osf, count is not an option end private @@ -61,7 +63,7 @@ module Maintenance end def fixable?(champ) - champ.value.present? && [champ.dossier.en_instruction? || champ.dossier.en_construction?] + champ.dossier.en_instruction? || champ.dossier.en_construction? end def notify(message, champ) = Sentry.capture_message(message, extra: { champ: }) diff --git a/spec/tasks/maintenance/fix_champs_commune_having_value_but_not_external_id_task_spec.rb b/spec/tasks/maintenance/fix_champs_commune_having_value_but_not_external_id_task_spec.rb index 8317d9ffe..c93e1aa3e 100644 --- a/spec/tasks/maintenance/fix_champs_commune_having_value_but_not_external_id_task_spec.rb +++ b/spec/tasks/maintenance/fix_champs_commune_having_value_but_not_external_id_task_spec.rb @@ -8,7 +8,9 @@ module Maintenance let(:procedure) { create(:procedure, types_de_champ_public: [{ type: :communes }]) } let(:dossier) { create(:dossier, state, :with_populated_champs, procedure:) } 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 let(:state) { [:en_instruction, :en_construction].sample }