mirror of
https://github.com/gevera/hot-cold-cities.git
synced 2025-12-06 07:08:20 +00:00
Restart job if the cron is stalled
This commit is contained in:
parent
b14a55de71
commit
23d015bdb4
@ -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);
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user