web: harden the cache purge scheduler

This commit is contained in:
ponas committed 2026-10-14 00:36:54 +02:00
1 parent ef5dd54f2e
commit a4bfdb5657
6 files changed
+159 -13

No files matched your search

+16 -4
View File
@@ -12,8 +12,8 @@ import {
// How often the clock is checked. Timers alone can't be trusted to fire at
// midnight: they stand still while a host or VM is paused
const CHECK_INTERVAL_MS = 30_000;
// Until the first purge has gone through; the server isn't listening yet
// when register() runs
// Until the first purge has gone through. The server is already listening
// when register() runs, so this is only for a purge that fails
const STARTUP_RETRY_MS = 1_000;
function keepCacheCurrent() {
@@ -24,6 +24,18 @@ function keepCacheCurrent() {
let failures = 0;
async function check() {
try {
await purgeWhatIsDue();
} catch (error) {
// nothing in there is expected to throw, but if something does the
// loop must survive it: without it the cache never rolls over
console.error("Checking the page cache failed", error);
} finally {
schedule();
}
}
async function purgeWhatIsDue() {
const day = osloDay();
let error: string | null = null;
@@ -50,14 +62,14 @@ function keepCacheCurrent() {
if (error) {
failures += 1;
// at startup, connection errors are expected for the first moments
// report the first failure once running, and then now and again; at
// startup only if it goes on for a minute
if (failures === 1 ? purgedDay !== undefined : failures % 60 === 0) {
console.error(`Purging the page cache keeps failing: ${error}`);
}
} else {
failures = 0;
}
schedule();
}
function schedule() {