import type { DatabaseSync } from "node:sqlite"; import type { IngestionWorker } from "./ingestion.ts"; import type { LeaderboardApi } from "./leaderboards-api.ts"; import type { OperationalApi } from "./operational-api.ts"; import type { PlayerApi } from "./players-api.ts"; import type { ScenarioApi } from "./scenarios-api.ts"; export interface ApplicationRuntime { readonly host: string; readonly port: number; readonly database: DatabaseSync; readonly worker: IngestionWorker; readonly leaderboards: LeaderboardApi; readonly players: PlayerApi; readonly scenarios: ScenarioApi; readonly operations: OperationalApi; } interface RuntimeSlot { current: ApplicationRuntime | null; starting: Promise | null; stopping: Promise | null; } const RUNTIME_KEY = Symbol.for("rad-cohesion.application-runtime"); function runtimeSlot(): RuntimeSlot { // Vite's config runner, its SSR bundle, and the production bootstrap can each // evaluate this module separately. A global symbol keeps one runtime across them. // SAFETY: this private symbol is only assigned a RuntimeSlot by this accessor. const symbolTable = globalThis as typeof globalThis & { [RUNTIME_KEY]?: RuntimeSlot }; const existing = symbolTable[RUNTIME_KEY]; if (existing !== undefined) return existing; const created: RuntimeSlot = { current: null, starting: null, stopping: null }; symbolTable[RUNTIME_KEY] = created; return created; } export function currentApplicationRuntime(): ApplicationRuntime { const runtime = runtimeSlot().current; if (!runtime) throw new Error("The application server runtime has not started."); return runtime; } export async function startApplicationRuntime(): Promise { const slot = runtimeSlot(); if (slot.stopping) throw new Error("The application server runtime is shutting down."); if (slot.current) return slot.current; if (slot.starting) return slot.starting; const starting = createApplicationRuntime().then((runtime) => { slot.current = runtime; return runtime; }).finally(() => { slot.starting = null; }); slot.starting = starting; return starting; } export async function stopApplicationRuntime(): Promise { const slot = runtimeSlot(); if (slot.stopping) return slot.stopping; if (!slot.current && !slot.starting) return; const stopping = (async () => { try { let runtime = slot.current; if (!runtime && slot.starting) { try { runtime = await slot.starting; } catch { slot.starting = null; } } if (runtime) { try { await runtime.worker.stop(); } finally { runtime.database.close(); if (slot.current === runtime) slot.current = null; slot.starting = null; } } } finally { slot.starting = null; slot.stopping = null; } })(); slot.stopping = stopping; return stopping; } async function createApplicationRuntime(): Promise { const [{ loadApplicationConfig }, { openDatabase }, { migrate }, { startIngestion }, { createLeaderboardApi }, { createPlayerApi }, { createScenarioApi }, { createOperationalApi }] = await Promise.all([ import("./config.ts"), import("./database.ts"), import("./migrations.ts"), import("./ingestion.ts"), import("./leaderboards-api.ts"), import("./players-api.ts"), import("./scenarios-api.ts"), import("./operational-api.ts"), ]); const config = loadApplicationConfig(); const database = openDatabase(config.databasePath); try { await migrate(database); const leaderboards = createLeaderboardApi(database, { publicOrigin: config.publicOrigin, trustedProxyIps: config.trustedProxyIps, }); const scenarios = createScenarioApi(database); const players = createPlayerApi(database); const worker = startIngestion(database, config.ingestion); try { return { host: config.host, port: config.port, database, worker, leaderboards, players, scenarios, operations: createOperationalApi(database, worker), }; } catch (error) { await worker.stop(); throw error; } } catch (error) { database.close(); throw error; } }