From 1f5098a871e5cd6b81c2a52633772c44be1fb025 Mon Sep 17 00:00:00 2001 From: el Date: Sat, 24 May 2025 02:25:42 +0200 Subject: [PATCH] cron job to run every day 00h ingestion --- backend/package-lock.json | 19 +++++++++ backend/package.json | 6 ++- backend/src/index.ts | 5 ++- backend/src/utils/jobScheduler.ts | 65 +++++++++++++++++++++++++++++++ 4 files changed, 92 insertions(+), 3 deletions(-) create mode 100644 backend/src/utils/jobScheduler.ts diff --git a/backend/package-lock.json b/backend/package-lock.json index 78f4f09..11edbf4 100644 --- a/backend/package-lock.json +++ b/backend/package-lock.json @@ -8,6 +8,10 @@ "name": "backend", "version": "1.0.0", "license": "ISC", + "dependencies": { + "@types/node-cron": "^3.0.11", + "node-cron": "^4.0.7" + }, "devDependencies": { "@prisma/client": "^6.8.2", "@types/express": "^5.0.2", @@ -244,6 +248,12 @@ "undici-types": "~6.21.0" } }, + "node_modules/@types/node-cron": { + "version": "3.0.11", + "resolved": "https://registry.npmjs.org/@types/node-cron/-/node-cron-3.0.11.tgz", + "integrity": "sha512-0ikrnug3/IyneSHqCBeslAhlK2aBfYek1fGo4bP4QnZPmiqSGRK+Oy7ZMisLWkesffJvQ1cqAcBnJC+8+nxIAg==", + "license": "MIT" + }, "node_modules/@types/qs": { "version": "6.14.0", "resolved": "https://registry.npmjs.org/@types/qs/-/qs-6.14.0.tgz", @@ -1321,6 +1331,15 @@ "node": ">= 0.6" } }, + "node_modules/node-cron": { + "version": "4.0.7", + "resolved": "https://registry.npmjs.org/node-cron/-/node-cron-4.0.7.tgz", + "integrity": "sha512-A37UUDpxRT/kWanELr/oMayCWQFk9Zx9BEUoXrAKuKwKzH4XuAX+vMixMBPkgZBkADgJwXv91w5cMRTNSVP/mA==", + "license": "ISC", + "engines": { + "node": ">=6.0.0" + } + }, "node_modules/normalize-path": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/normalize-path/-/normalize-path-3.0.0.tgz", diff --git a/backend/package.json b/backend/package.json index 2f8169c..60095fd 100644 --- a/backend/package.json +++ b/backend/package.json @@ -24,5 +24,9 @@ "ts-node": "^10.9.2", "ts-node-dev": "^2.0.0", "typescript": "^5.8.3" + }, + "dependencies": { + "@types/node-cron": "^3.0.11", + "node-cron": "^4.0.7" } -} \ No newline at end of file +} diff --git a/backend/src/index.ts b/backend/src/index.ts index 5584b9c..471b5bc 100644 --- a/backend/src/index.ts +++ b/backend/src/index.ts @@ -3,6 +3,7 @@ import dotenv from 'dotenv'; import jobIngestionRoutes from './routes/jobIngestionRoutes'; import jobSearchRoutes from './routes/jobSearchRoutes'; import { ingestJobOffers } from './controllers/jobIngestionController'; +import { startJobIngestionScheduler } from './utils/jobScheduler'; // <-- AJOUTE CETTE LIGNE dotenv.config(); @@ -17,5 +18,5 @@ app.post('/api/ingest-jobs', ingestJobOffers); app.listen(PORT, () => { console.log(`Server is running on port ${PORT}`); - -}); \ No newline at end of file + startJobIngestionScheduler(); // <-- AJOUTE CETTE LIGNE +}); diff --git a/backend/src/utils/jobScheduler.ts b/backend/src/utils/jobScheduler.ts new file mode 100644 index 0000000..afde77e --- /dev/null +++ b/backend/src/utils/jobScheduler.ts @@ -0,0 +1,65 @@ +import cron from 'node-cron'; +import { ingestJobOffers } from '../controllers/jobIngestionController'; +import { Request, Response } from 'express'; // <-- AJOUTE CETTE LIGNE + +// Planifie la tâche d'ingestion. +// L'expression '0 0 * * *' signifie "tous les jours à minuit". +// Vous pouvez ajuster cette expression pour un intervalle plus fréquent si nécessaire +// pendant le développement (ex: '0 */4 * * *' pour toutes les 4 heures, ou '*/30 * * * *' toutes les 30 minutes). +// Attention aux limites de l'API France Travail ! + +export const startJobIngestionScheduler = () => { + // Pour les tests de développement, vous pouvez commencer par une fréquence plus élevée + // Par exemple, toutes les 15 minutes: '*/15 * * * *' + // Ou toutes les 6 heures : '0 */6 * * *' + // Assurez-vous de ne pas dépasser les 10 appels/seconde sur le long terme si vous augmentez la fréquence ou le volume. + const CRON_SCHEDULE = process.env.JOB_INGESTION_CRON_SCHEDULE || '0 0 * * *'; // Minuit tous les jours par défaut + + console.log(`Scheduling job ingestion with cron: ${CRON_SCHEDULE}`); + + cron.schedule(CRON_SCHEDULE, async () => { + console.log('Starting scheduled job ingestion...'); + try { + // La fonction ingestJobOffers du contrôleur Express prend req, res. + // Pour un appel planifié, nous devons la "simuler" ou la modifier. + // Le plus simple est d'extraire la logique principale de l'ingestion + // dans une fonction séparée qui ne dépend pas de req/res, et de l'appeler ici. + // Pour l'instant, nous allons créer des objets req/res factices pour qu'elle puisse être appelée. + // C'est une solution temporaire, une meilleure pratique serait de refactoriser ingestJobOffers. + + const mockReq = {} as Request; + const mockRes = { + status: (statusCode: number) => { + console.log(`Ingestion status: ${statusCode}`); + return mockRes; + }, + json: (data: any) => { + console.log('Ingestion result:', data); + }, + } as Response; + + await ingestJobOffers(mockReq, mockRes); + console.log('Scheduled job ingestion completed.'); + } catch (error) { + console.error('Error during scheduled job ingestion:', error); + } + }, { + timezone: "Europe/Paris" // ou votre fuseau horaire + }); + + // Exécute une ingestion initiale au démarrage du serveur + console.log('Performing initial job ingestion on server start...'); + const mockReqInitial = {} as Request; + const mockResInitial = { + status: (statusCode: number) => { + console.log(`Initial Ingestion status: ${statusCode}`); + return mockResInitial; + }, + json: (data: any) => { + console.log('Initial Ingestion result:', data); + }, + } as Response; + ingestJobOffers(mockReqInitial, mockResInitial).catch(error => { + console.error('Error during initial job ingestion:', error); + }); +};