cron job to run every day 00h ingestion

This commit is contained in:
el 2025-05-24 02:25:42 +02:00
parent 72f3fd82c8
commit 1f5098a871
4 changed files with 92 additions and 3 deletions

View file

@ -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",

View file

@ -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"
}
}
}

View file

@ -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}`);
});
startJobIngestionScheduler(); // <-- AJOUTE CETTE LIGNE
});

View file

@ -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);
});
};