Files
blitzkit/packages/scripts/src/discoverIds/time.ts
T
Mark Greene 3aefe7d985 Fix: await the ID discovery loop so it runs to completion
The ID discovery script exited about a second after starting. Since the switch to `atMaxRate`, neither the discovery loop nor the request loop was awaited, and `atMaxRate` didn't await its callback, so the script went straight to `save()`, committed and exited while overlapping duplicate requests failed in the background.

- Awaited both `atMaxRate` loops and the callback inside `atMaxRate`
- Moved the ID range exhaustion check out of the retry loop so retries can't remove extra regions
- Treated network and JSON errors as failed API calls so a long run retries instead of crashing without saving

Fixes #306
2026-10-05 17:33:01 +13:00

48 lines
925 B
TypeScript

import { API_RATE } from "./constants";
const MAX_COMPUTE_TIME = (5 * 60 + 55) * 60 * 1000; // 5hr 55min
const t0 = performance.now();
const t1 = t0 + MAX_COMPUTE_TIME;
export function withinTimeLimit() {
return performance.now() < t1;
}
export function sleep(t: number) {
return new Promise((resolve) => setTimeout(resolve, t));
}
interface MethodContext {
_break(): void;
}
let lastT = 0;
const maxDelta = 1000 / API_RATE;
const DELTA_BUFFER = 0;
export async function atMaxRate(
method: (context: MethodContext) => void | Promise<void>,
) {
let isBroken = false;
const context: MethodContext = {
_break() {
isBroken = true;
},
};
while (!isBroken) {
if (!withinTimeLimit()) break;
const t = performance.now();
const delta = t - lastT;
if (delta < maxDelta) {
await sleep(maxDelta - delta + DELTA_BUFFER);
}
await method(context);
lastT = t;
}
}