When hourly sync encounters servers that fail (e.g., mid-restart): - Logs the failure count - Schedules automatic retry in 10 minutes - Retry only targets previously failed servers - Clears error state on successful retry Fixes issue where servers in daily restart would stay in error state until manual intervention. Chronicler #69
46 lines
1.5 KiB
JavaScript
46 lines
1.5 KiB
JavaScript
const cron = require('node-cron');
|
|
const { triggerImmediateSync } = require('./immediate');
|
|
const { processExpiredGracePeriods } = require('./graceExpiration');
|
|
|
|
let retryTimeout = null;
|
|
|
|
function initCron() {
|
|
// Hourly whitelist reconciliation
|
|
cron.schedule('0 * * * *', async () => {
|
|
console.log("⏰ Starting hourly sync jobs...");
|
|
|
|
// 1. Process expired grace periods
|
|
await processExpiredGracePeriods();
|
|
|
|
// 2. Whitelist reconciliation
|
|
console.log("Starting whitelist reconciliation...");
|
|
const { failCount } = await triggerImmediateSync();
|
|
|
|
// 3. Schedule retry if there were failures
|
|
if (failCount > 0) {
|
|
console.log(`⏳ ${failCount} servers failed. Scheduling retry in 10 minutes...`);
|
|
|
|
// Clear any existing retry timeout
|
|
if (retryTimeout) {
|
|
clearTimeout(retryTimeout);
|
|
}
|
|
|
|
// Retry failed servers after 10 minutes
|
|
retryTimeout = setTimeout(async () => {
|
|
console.log("🔄 Running retry sync for failed servers...");
|
|
const { failCount: retryFailCount } = await triggerImmediateSync(true);
|
|
|
|
if (retryFailCount > 0) {
|
|
console.log(`⚠️ ${retryFailCount} servers still failing after retry.`);
|
|
} else {
|
|
console.log("✅ All previously failed servers now synced successfully.");
|
|
}
|
|
}, 10 * 60 * 1000); // 10 minutes
|
|
}
|
|
|
|
console.log("✅ Hourly sync jobs complete");
|
|
});
|
|
}
|
|
|
|
module.exports = { initCron };
|