Restart job if the cron is stalled

This commit is contained in:
Denis Donici 2025-03-06 16:56:19 +02:00
parent b14a55de71
commit 23d015bdb4

View File

@ -1,11 +1,42 @@
import { CronJob } from 'cron';
import { fetchAllCitiesTemperatures } from './meteoDataHandler';
const MAX_EXECUTION_TIME = 30 * 60 * 1000; // 30 minutes in milliseconds
let jobInProgress = false;
let jobTimeout: NodeJS.Timer;
const startFetchJob = async (date: string) => {
if (jobInProgress) {
console.log('Previous job still running, clearing timeout and restarting');
clearTimeout(jobTimeout);
}
jobInProgress = true;
// Set timeout to restart job if it stalls
jobTimeout = setTimeout(() => {
console.log('Job appears stalled, restarting...');
jobInProgress = false;
startFetchJob(date);
}, MAX_EXECUTION_TIME);
try {
await fetchAllCitiesTemperatures(date);
} catch (error) {
console.error('Error in temperature fetch job:', error);
} finally {
clearTimeout(jobTimeout);
jobInProgress = false;
}
};
const today = new Date();
const shortDate = today.toISOString().split('T')[0];
const temperatureCronJob = new CronJob('0 0 * * *', () => {
console.log('Running daily temperature fetch for: ', shortDate);
fetchAllCitiesTemperatures(shortDate);
startFetchJob(shortDate);
}, () => {
console.log('Temperature cron job completed for: ', shortDate);
}, true, 'UTC');
@ -17,6 +48,6 @@ export function startTemperatureCronJob() {
// Run immediately on startup if no data exists for today
if (today.getUTCHours() > 0) { // Only run if it's past midnight UTC
fetchAllCitiesTemperatures(shortDate);
startFetchJob(shortDate);
}
}