From 3aefe7d985d690bfe748669633429c66f9657445 Mon Sep 17 00:00:00 2001 From: Mark Greene Date: Mon, 5 Oct 2026 17:33:01 +1300 Subject: [PATCH] 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 --- packages/scripts/src/discoverIds/index.ts | 23 ++++++++++++----------- packages/scripts/src/discoverIds/time.ts | 6 ++++-- 2 files changed, 16 insertions(+), 13 deletions(-) diff --git a/packages/scripts/src/discoverIds/index.ts b/packages/scripts/src/discoverIds/index.ts index 1d18a25d7..fb8c307cd 100644 --- a/packages/scripts/src/discoverIds/index.ts +++ b/packages/scripts/src/discoverIds/index.ts @@ -135,7 +135,7 @@ let regionI = 0; console.log("Starting discovery loop..."); -atMaxRate(async ({ _break: break1 }) => { +await atMaxRate(async ({ _break: break1 }) => { if (regions.length === 0) { console.log("All regions exhausted, exiting discovery loop..."); return break1(); @@ -166,18 +166,19 @@ atMaxRate(async ({ _break: break1 }) => { fields }&account_id=${accountIds}`; - atMaxRate(async ({ _break: break2 }) => { - if (idRange.length < MAX_IDS_PER_CALL) { - console.log( - `Removing region ${region.domain} removed due to ID range exhaustion...`, - ); - regions.splice(regionI, 1); - } + if (idRange.length < MAX_IDS_PER_CALL) { + console.log( + `Removing region ${region.domain} removed due to ID range exhaustion...`, + ); + regions.splice(regionI, 1); + } - const response = await fetch(url); - const data = (await response.json()) as QuickInfo; + await atMaxRate(async ({ _break: break2 }) => { + const data = await fetch(url) + .then((response) => response.json() as Promise) + .catch(() => null); - if (data.status !== "ok") { + if (data === null || data.status !== "ok") { console.warn("Failed API call; trying again..."); return; } diff --git a/packages/scripts/src/discoverIds/time.ts b/packages/scripts/src/discoverIds/time.ts index d8750ae24..09c476e18 100644 --- a/packages/scripts/src/discoverIds/time.ts +++ b/packages/scripts/src/discoverIds/time.ts @@ -20,7 +20,9 @@ interface MethodContext { let lastT = 0; const maxDelta = 1000 / API_RATE; const DELTA_BUFFER = 0; -export async function atMaxRate(method: (context: MethodContext) => void) { +export async function atMaxRate( + method: (context: MethodContext) => void | Promise, +) { let isBroken = false; const context: MethodContext = { @@ -39,7 +41,7 @@ export async function atMaxRate(method: (context: MethodContext) => void) { await sleep(maxDelta - delta + DELTA_BUFFER); } - method(context); + await method(context); lastT = t; } }