Scheduler: stagger resource checks with spacing and jitter to avoid thundering herd

This commit is contained in:
nmemmert committed 2025-12-16 12:31:03 -05:00
1 parent b4860241dd
commit 803c5adbc2
1 file changed
+11 -5
+11 -5
View File
@@ -81,7 +81,13 @@ class Scheduler {
SELECT * FROM resources WHERE enabled = 1 SELECT * FROM resources WHERE enabled = 1
`).all(); `).all();
for (const resource of resources) { // Stagger checks to avoid thundering herd
const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
const jitter = (maxMs = 1500) => Math.floor(Math.random() * maxMs);
const baseSpacingMs = 250; // spread starts in 250ms increments
const tasks = resources.map((resource, index) => (async () => {
await sleep(index * baseSpacingMs + jitter());
try { try {
const result = await monitorService.checkResource(resource); const result = await monitorService.checkResource(resource);
monitorService.saveCheck(result); monitorService.saveCheck(result);
@@ -100,9 +106,7 @@ class Scheduler {
LIMIT 12 LIMIT 12
`).all(resource.id); `).all(resource.id);
stats.recentChecks = recentChecks.reverse(); stats.recentChecks = recentChecks.reverse();
if (resource.maintenance_mode) { if (!resource.maintenance_mode) {
console.log(`Resource ${resource.name} in maintenance; skipping alert`);
} else {
await notificationService.sendAlert(resource, incident, stats); await notificationService.sendAlert(resource, incident, stats);
} }
} }
@@ -111,7 +115,9 @@ class Scheduler {
} catch (error) { } catch (error) {
console.error(`Error checking ${resource.name}:`, error.message); console.error(`Error checking ${resource.name}:`, error.message);
} }
} })());
await Promise.allSettled(tasks);
// Broadcast updated dashboard to all connected WebSocket clients // Broadcast updated dashboard to all connected WebSocket clients
if (global.broadcastDashboardUpdate) { if (global.broadcastDashboardUpdate) {