Skip to content

Commit

Permalink
fix: mise à jour du scipt de mise a jour des computed effectif (#3941)
Browse files Browse the repository at this point in the history
Co-authored-by: Paul Gaucher <[email protected]>
  • Loading branch information
Pomarom and Pomarom authored Dec 16, 2024
1 parent bd2aecd commit 42f8e63
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 21 deletions.
2 changes: 1 addition & 1 deletion server/src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ program
.description("Manually trigger the update of the computed")
.option("--id <string>", "Id de l'organisme", (organismeId) => organismeId, null)
.option("-q, --queued", "Run job asynchronously", false)
.action(createJobAction("hydrate:effectifs:update_computed_statut_month"));
.action(createJobAction("hydrate:effectifs:update_computed_statut"));

program
.command("job:run")
Expand Down
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,5 @@
import { captureException } from "@sentry/node";
import { IEffectif } from "shared/models";

import { updateEffectifStatut } from "@/common/actions/effectifs.statut.actions";
import logger from "@/common/logger";
Expand All @@ -11,26 +12,49 @@ 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 processEffectif = async (eff: IEffectif) => {
if (eff) {
const isSuccess = await updateEffectifStatut(eff, evaluationDate);
if (isSuccess) {
nbEffectifsMisAJour++;
} else {
nbEffectifsNonMisAJour++;
}
}
};

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

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
30 changes: 18 additions & 12 deletions server/src/jobs/jobs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { addJob, initJobProcessor } from "job-processor";
import { MongoError, ObjectId, WithId } from "mongodb";
import { MOTIF_SUPPRESSION } from "shared/constants";
import type { IEffectif } from "shared/models";
import { getAnneesScolaireListFromDate } from "shared/utils";
import { getAnneesScolaireListFromDate, substractDaysUTC } from "shared/utils";

import { softDeleteEffectif } from "@/common/actions/effectifs.actions";
import logger from "@/common/logger";
Expand Down 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,22 @@ 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;
const evaluationDate = new Date();
return hydrateEffectifsComputedTypes(
{
query: {
annee_scolaire: { $in: getAnneesScolaireListFromDate(evaluationDate) },
updated_at: { $lt: substractDaysUTC(evaluationDate, 7) },
...(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 42f8e63

Please sign in to comment.