Refactor environment variable defaults and improve error handling in various scripts.

This commit is contained in:
scamiv
2026-02-23 13:50:33 +01:00
parent dd7938a7fc
commit 3bb92600cd
5 changed files with 18 additions and 21 deletions
+18 -11
View File
@@ -1,10 +1,11 @@
import WebSocket from "ws";
const BASE_URL = process.env.TARGET_BASE_URL || "https://openfront.io";
const ARCHIVE_API_BASE = process.env.ARCHIVE_API_BASE || "https://api.openfront.io";
const NUM_WORKERS = Number(process.env.NUM_WORKERS || "20");
const WS_WAIT_MS = Number(process.env.WS_WAIT_MS || "6000");
const CONNECT_TIMEOUT_MS = Number(process.env.CONNECT_TIMEOUT_MS || "5000");
const BASE_URL = process.env.TARGET_BASE_URL ?? "https://openfront.io";
const ARCHIVE_API_BASE =
process.env.ARCHIVE_API_BASE ?? "https://api.openfront.io";
const NUM_WORKERS = Number(process.env.NUM_WORKERS ?? "20");
const WS_WAIT_MS = Number(process.env.WS_WAIT_MS ?? "6000");
const CONNECT_TIMEOUT_MS = Number(process.env.CONNECT_TIMEOUT_MS ?? "5000");
const trim = (v) => v.replace(/\/+$/, "");
const base = trim(BASE_URL);
@@ -20,8 +21,6 @@ const workerIndexForGame = (gameID, workers) => {
return Math.abs(hash) % Math.max(1, workers);
};
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
const fetchJson = async (url, timeoutMs = 6000) => {
const controller = new AbortController();
const timeout = setTimeout(() => controller.abort(), timeoutMs);
@@ -30,7 +29,7 @@ const fetchJson = async (url, timeoutMs = 6000) => {
signal: controller.signal,
headers: { Accept: "application/json" },
});
const contentType = response.headers.get("content-type") || "";
const contentType = response.headers.get("content-type") ?? "";
const text = await response.text();
let json = null;
if (contentType.includes("application/json")) {
@@ -93,7 +92,9 @@ const wsProbe = async (url, waitMs = WS_WAIT_MS) => {
result.error = "connect-timeout";
try {
ws?.terminate();
} catch {}
} catch (error) {
result.error = result.error ?? String(error);
}
finish();
}, CONNECT_TIMEOUT_MS);
@@ -113,7 +114,9 @@ const wsProbe = async (url, waitMs = WS_WAIT_MS) => {
setTimeout(() => {
try {
ws.close(1000, "probe-done");
} catch {}
} catch (error) {
result.error = result.error ?? String(error);
}
}, waitMs);
});
@@ -207,7 +210,11 @@ async function main() {
}
printHeader("Sample game follow-up");
const parsed = JSON.parse(withLobbies.firstMessageRaw || withLobbies.firstMessageSample);
const firstPayload =
withLobbies.firstMessageRaw === ""
? withLobbies.firstMessageSample
: withLobbies.firstMessageRaw;
const parsed = JSON.parse(firstPayload);
const game = parsed.games?.[0] ?? parsed.data?.lobbies?.[0];
if (!game?.gameID) {
console.log("No sample game in first games payload.");
-4
View File
@@ -172,7 +172,6 @@ export class LobbyIngestService {
this.store.systemNote(
`Invalid /lobbies JSON parse error: ${String(error)} | payload=${payload}`,
);
// eslint-disable-next-line no-console
console.error("[lobbystatistics] invalid websocket payload", {
error,
payload,
@@ -183,7 +182,6 @@ export class LobbyIngestService {
if (json && typeof json === "object" && (json as { type?: string }).type === "error") {
const payload = compactPayload(text);
this.store.systemNote(`WebSocket error reply received: payload=${payload}`);
// eslint-disable-next-line no-console
console.error("[lobbystatistics] websocket error reply", payload);
return;
}
@@ -195,7 +193,6 @@ export class LobbyIngestService {
`Invalid /lobbies schema: ${normalized.error
.slice(0, 240)} | payload=${payload}`,
);
// eslint-disable-next-line no-console
console.error("[lobbystatistics] websocket schema mismatch", {
error: normalized.error,
payload,
@@ -525,7 +522,6 @@ export class LobbyIngestService {
await fn();
} catch (error) {
this.store.systemNote(`Task ${label} failed: ${String(error)}`);
// eslint-disable-next-line no-console
console.error(`[lobbystatistics] task ${label} failed`, error);
}
}
-4
View File
@@ -95,17 +95,14 @@ async function main() {
});
const server = app.listen(config.port, () => {
// eslint-disable-next-line no-console
console.log(`[lobbystatistics] ingest server listening on :${config.port}`);
});
process.on("unhandledRejection", (reason) => {
// eslint-disable-next-line no-console
console.error("[lobbystatistics] unhandledRejection", reason);
});
process.on("uncaughtException", (error) => {
// eslint-disable-next-line no-console
console.error("[lobbystatistics] uncaughtException", error);
});
@@ -122,7 +119,6 @@ async function main() {
}
void main().catch((error) => {
// eslint-disable-next-line no-console
console.error("[lobbystatistics] fatal startup error", error);
process.exit(1);
});
-1
View File
@@ -375,7 +375,6 @@ export class JsonStore {
this.flushTimer = null;
void this.flush().catch((error) => {
this.dirty = true;
// eslint-disable-next-line no-console
console.error("[lobbystatistics] db flush failed", error);
});
}, 500);
-1
View File
@@ -8,7 +8,6 @@ import {
import * as d3 from "d3";
import "./styles.css";
const DEFAULT_BUCKET_MODE: BucketMode = "game_mode_team";
const DEFAULT_LOOKBACK_HOURS = 24;
const DEFAULT_ORDER_COUNT = 40;