Skip to content

Commit

Permalink
fix: mise à jour du scipt de mise a jour des computed effectif
Browse files Browse the repository at this point in the history
  • Loading branch information
Pomarom committed Dec 16, 2024
1 parent bd2aecd commit cfe9b6a
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 20 deletions.
1 change: 1 addition & 0 deletions server/src/common/actions/effectifs.statut.actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ export function createComputedStatutObject(
function createUpdateObject(effectif: IEffectif, evaluationDate: Date): UpdateFilter<IEffectif> {
return {
$set: {
updated_at: new Date(),
"_computed.statut": createComputedStatutObject(effectif, evaluationDate),
},
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { captureException } from "@sentry/node";
import { IEffectif } from "shared/models";
import { substractDaysUTC } from "shared/utils";

import { updateEffectifStatut } from "@/common/actions/effectifs.statut.actions";
import logger from "@/common/logger";
Expand All @@ -11,26 +13,54 @@ import { effectifsDb } from "@/common/model/collections";
* query (Requête MongoDB pour filtrer les effectifs) et
* evaluationDate (La date pour évaluer le statut des effectifs).
*/
export async function hydrateEffectifsComputedTypes({ query = {}, evaluationDate = new Date() } = {}) {
export async function hydrateEffectifsComputedTypes(
{ query = {}, evaluationDate = new Date() } = {},
signal: AbortSignal
) {
let nbEffectifsMisAJour = 0;
let nbEffectifsNonMisAJour = 0;

const BULK_SIZE = 100;
let bulkEffectifs: IEffectif[] = [];

const computedQuery = {
...query,
updated_at: { $lt: substractDaysUTC(evaluationDate, 7) },
};

const processEffectif = async (eff: IEffectif) => {
if (eff) {
const isSuccess = await updateEffectifStatut(eff, evaluationDate);
if (isSuccess) {
nbEffectifsMisAJour++;
} else {
nbEffectifsNonMisAJour++;
}
}
};

try {
const cursor = effectifsDb().find(query);
const cursor = effectifsDb().find(computedQuery);

while (await cursor.hasNext()) {
const effectif = await cursor.next();

const effectif: IEffectif | null = await cursor.next();
if (effectif) {
const isSuccess = await updateEffectifStatut(effectif, evaluationDate);
if (isSuccess) {
nbEffectifsMisAJour++;
} else {
nbEffectifsNonMisAJour++;
bulkEffectifs.push(effectif);
}

if (bulkEffectifs.length > BULK_SIZE) {
await Promise.allSettled(bulkEffectifs.map(processEffectif));
if (signal && signal.aborted) {
return;
}
bulkEffectifs = [];
}
}

if (bulkEffectifs.length > 0) {
await Promise.allSettled(bulkEffectifs.map(processEffectif));
}

logger.info(`${nbEffectifsMisAJour} effectifs mis à jour, ${nbEffectifsNonMisAJour} effectifs non mis à jour.`);
} catch (err) {
logger.error(`Échec de la mise à jour des effectifs: ${err}`);
Expand Down
26 changes: 15 additions & 11 deletions server/src/jobs/jobs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,10 @@ export async function setupJobProcessor() {
},
},

"Mettre à jour les statuts d'effectifs le 1er de chaque mois à 00h45": {
cron_string: "45 0 1 * *",
"Mettre à jour les statuts d'effectifs tous les samedis matin à 5h": {
cron_string: "0 5 * * 6",
handler: async () => {
await addJob({ name: "hydrate:effectifs:update_computed_statut_month", queued: true });
await addJob({ name: "hydrate:effectifs:update_computed_statut", queued: true });
return 0;
},
},
Expand Down Expand Up @@ -183,16 +183,20 @@ export async function setupJobProcessor() {
return hydrateDecaRaw();
},
},
"hydrate:effectifs:update_computed_statut_month": {
handler: async (job?) => {
const organismeId = (job?.payload?.id as string) ? new ObjectId(job?.payload?.id as string) : null;
return hydrateEffectifsComputedTypes({
query: {
annee_scolaire: { $in: getAnneesScolaireListFromDate(new Date()) },
...(organismeId ? { organisme_id: organismeId } : {}),
"hydrate:effectifs:update_computed_statut": {
handler: async (job, signal) => {
const organismeId = (job.payload?.id as string) ? new ObjectId(job.payload?.id as string) : null;
return hydrateEffectifsComputedTypes(
{
query: {
annee_scolaire: { $in: getAnneesScolaireListFromDate(new Date()) },
...(organismeId ? { organisme_id: organismeId } : {}),
},
},
});
signal
);
},
resumable: true,
},
"hydrate:effectifs-formation-niveaux": {
handler: async () => {
Expand Down
4 changes: 4 additions & 0 deletions shared/utils/date.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ export function addDaysUTC(date: Date, days: number) {
return result;
}

export function substractDaysUTC(date: Date, days: number) {
return addDaysUTC(date, -days);
}

export const getYearFromDate = (date?: Date | null): number | undefined => {
return date ? new Date(date).getFullYear() : undefined;
};

0 comments on commit cfe9b6a

Please sign in to comment.