Lighten library cache reports and heartbeat cleanup

This commit is contained in:
2026-10-05 15:51:55 -04:00
parent 847399a4ff
commit c8c53e87d6
5 changed files with 127 additions and 3 deletions
+12 -1
View File
@@ -332,9 +332,17 @@ function transferProgress(value: unknown, status: LibraryCacheState): LibraryTra
bytesPerSecond: progress.bytesPerSecond as number | null ?? null, etaSeconds: progress.etaSeconds as number | null ?? null };
}
type LibraryCacheMetadata = Pick<GameLibraryVersion, 'state' | 'sizeBytes' | 'manifestSha256'>;
const publishedCacheMetadata = new WeakMap<ReturnType<typeof database>, Map<string, LibraryCacheMetadata>>();
export function updateHostLibraryCaches(hostId: string, input: unknown, options: { snapshot?: boolean } = {}): void {
if (!Array.isArray(input) || input.length > 2000) throw new Error('Invalid host library report.');
const handle = database();
// Published metadata is immutable. Keep caches local to the connection and
// avoid retaining a publication from an outer transaction that may roll back.
const versions = handle.inTransaction ? new Map<string, LibraryCacheMetadata>()
: publishedCacheMetadata.get(handle) ?? new Map<string, LibraryCacheMetadata>();
if (!handle.inTransaction) publishedCacheMetadata.set(handle, versions);
const now = new Date().toISOString();
handle.transaction(() => {
const reported = new Set<string>();
@@ -344,7 +352,10 @@ export function updateHostLibraryCaches(hostId: string, input: unknown, options:
if (typeof id !== 'string') throw new Error('Invalid library version ID.');
if (reported.has(id)) throw new Error('Duplicate version in host cache report.');
reported.add(id);
const version = getLibraryVersion(id); if (!version || version.state !== 'ready') continue;
const version = versions.get(id) ?? handle.prepare(`SELECT state, size_bytes AS sizeBytes, manifest_sha256 AS manifestSha256
FROM game_file_versions WHERE id=?`).get(id) as LibraryCacheMetadata | undefined;
if (!version || version.state !== 'ready') continue;
versions.set(id, version);
let status = raw.status ?? raw.state;
if (!['missing', 'downloading', 'verifying', 'ready', 'failed', 'outdated'].includes(status)) throw new Error('Invalid library cache state.');
const checksum = typeof raw.manifestSha256 === 'string' ? raw.manifestSha256 : null;
+1 -2
View File
@@ -2,7 +2,7 @@ import { json } from '@sveltejs/kit';
import { heartbeat, pendingCreationCancellations } from '$lib/server/hosts/index.js';
import { authenticateAgent, hostApiError, objectBody } from '$lib/server/hosts/http.js';
import { reconcileHostPreparation } from '$lib/server/hosts/preparation.js';
import { reconcileHostLibraryCleanup, cleanupControllerLibraries } from '$lib/server/game-files/index.js';
import { reconcileHostLibraryCleanup } from '$lib/server/game-files/index.js';
import { fleetSettings } from '$lib/server/settings.js';
export async function POST({ request }: { request: Request }) {
const authenticated = authenticateAgent(request);
@@ -16,7 +16,6 @@ export async function POST({ request }: { request: Request }) {
}
reconcileHostPreparation(host.id);
reconcileHostLibraryCleanup(host.id);
await cleanupControllerLibraries();
return json({ host, cancelledCreations: pendingCreationCancellations(host.id), heartbeatSeconds: 15,
observationKeepDays: fleetSettings().observationKeepDays, serverTime: new Date().toISOString() });
} catch (cause) { return hostApiError(cause); }