Files
cobble/src/functions/backend/db/app-settings/grab-app-settings.ts
T
tben e1faa2141e
CI / check (push) Canceled after 0s
Limit the app container to 1 CPU and 2 GB, and stop stale caches
The app container is now created with --cpus 1 --memory 2048m: the heavy
work happens in the computers' own containers, so the orchestrator does
not need more. Both are installer flags (--cpus, --memory), kept in
install.conf, and part of the idempotency check, so an existing install
picks them up on its next update.

The env and app-setting caches lived in module variables, and bunext
bundles pages and API routes separately. A save cleared only its own
copy, so a saved provider key never reached the page that lists models
until the process restarted. They now share one slot per name on
globalThis, behind a symbol from the global registry, with the pattern
extracted into a tested helper.
2026-10-22 15:16:51 +01:00

46 lines
1.3 KiB
TypeScript

import BunSQLite from "@moduletrace/bun-sqlite";
import type { BUN_SQLITE_COBBLE_APP_SETTINGS } from "@/db/types/db";
import globalCache from "@/src/utils/global-cache";
/**
* The cache lives on `globalThis`, behind a registered symbol, rather than
* in a module variable: the page and API bundles each carry their own copy
* of this module, so a module-scoped cache would let a save clear its own
* copy while another page kept serving a stale one until the next restart.
*/
const cache = globalCache<Map<string, string>>({
name: "cobble.grabAppSettings.cache",
});
/**
* Every install-wide setting, cached per process (like `grabAllEnvs`).
* `saveAppSetting` and `deleteAppSetting` clear it.
*/
export default async function grabAppSettings() {
const cached = cache.get();
if (cached) return cached;
const res = await BunSQLite.sql<BUN_SQLITE_COBBLE_APP_SETTINGS>({
sql: `SELECT key, value FROM app_settings`,
values: [],
});
const map = new Map<string, string>();
for (const row of res.payload || []) {
if (row.key) map.set(row.key, row.value || "");
}
cache.set(map);
return map;
}
/**
* Drops the cache, so the next read sees the database.
*/
export function clearAppSettingsCache() {
cache.clear();
}