Merge pull request #10300 from tchak/chore-stable_id-task

fix(stable_id): recursive job to fill stable_ids
This commit is contained in:
Paul Chavard 2024-04-11 09:53:56 +00:00 committed by GitHub
commit 4cdaef3a08
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 12 additions and 16 deletions

View file

@ -1,13 +1,18 @@
class Migrations::BackfillStableIdJob < ApplicationJob
queue_as :low_priority
def perform(dossier_id)
BATCH = 100_000
def perform(iteration)
sql = "UPDATE champs SET stable_id = t.stable_id, stream = 'main'
FROM types_de_champ t
WHERE champs.type_de_champ_id = t.id
AND champs.dossier_id = ?
AND champs.stable_id IS NULL;"
query = ActiveRecord::Base.sanitize_sql_array([sql, dossier_id])
AND champs.id IN (SELECT id FROM champs WHERE champs.stable_id IS NULL LIMIT ?);"
query = ActiveRecord::Base.sanitize_sql_array([sql, BATCH])
ActiveRecord::Base.connection.execute(query)
if Champ.exists?(stable_id: nil)
Migrations::BackfillStableIdJob.set(wait: 2.seconds).perform_later(iteration + 1)
end
end
end

View file

@ -2,19 +2,10 @@
module Maintenance
class BackfillChampsStableIdTask < MaintenanceTasks::Task
def collection
Dossier.select(:id)
end
no_collection
def process(dossier)
if Champ.exists?(dossier_id: dossier.id, stable_id: nil)
day = 24 * 60 * 60 # 24 hours in seconds
wait = rand(0...(day * 4)).seconds # every second over 4 days
Migrations::BackfillStableIdJob
.set(wait:)
.perform_later(dossier.id)
end
def process
Migrations::BackfillStableIdJob.perform_later(0)
end
end
end