Every useLive() call opened its own EventSource, and the library page and a model's Jobs tab call it twice. Over plain HTTP a browser allows 6 connections per host, so with three library tabs open the third took ~10 s to load and its requests never got an answer; a fourth tab didn't load. - /api/live answers one short request with the job snapshot (left out when the client already has that version) and, for job pages, new log lines. - src/lib/live-store.ts runs one poller per query per tab, shared by every component showing it, paused while the tab is hidden, with backoff on errors. One router.refresh() per finished job instead of one per component. - The worker dot reads the status from that poll; it only asks /api/status (every 10 s) when nothing else is polled. - The worker status no longer carries the heartbeat time (it changed every 5 s, so every idle stream re-sent its snapshot) and rounds free space to MiB. - Removes /api/stream: its sleep() added an abort listener per second to the request signal and never removed it. - Job actions poll right away after starting or cancelling a job; drops approveJob/rejectJob, whose routes went away in 0.15. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EooZ9biu7kFNB4eDJrwQc5
46 lines
1.5 KiB
TypeScript
46 lines
1.5 KiB
TypeScript
import "server-only";
|
|
import { eq } from "drizzle-orm";
|
|
import { db } from "@/lib/db/client";
|
|
import { workerState } from "@/lib/db/schema";
|
|
|
|
/** The worker writes a heartbeat every 5s; after this long without one we call it offline. */
|
|
const OFFLINE_AFTER_MS = 20_000;
|
|
const MIB = 1024 * 1024;
|
|
|
|
/**
|
|
* What the UI shows about the worker. No heartbeat time on purpose: it changes every 5 s, and the
|
|
* live snapshot (see /api/live) would then change every 5 s even when nothing else did.
|
|
*/
|
|
export type WorkerStatus = {
|
|
online: boolean;
|
|
hfVersion: string | null;
|
|
/** Free/total bytes of the models folder, rounded to whole MiB for the same reason. */
|
|
modelsDirFreeBytes: number | null;
|
|
modelsDirTotalBytes: number | null;
|
|
};
|
|
|
|
const OFFLINE: WorkerStatus = {
|
|
online: false,
|
|
hfVersion: null,
|
|
modelsDirFreeBytes: null,
|
|
modelsDirTotalBytes: null,
|
|
};
|
|
|
|
const toMiB = (bytes: number | null) => (bytes == null ? null : Math.round(bytes / MIB) * MIB);
|
|
|
|
export async function getWorkerStatus(): Promise<WorkerStatus> {
|
|
try {
|
|
const row = await db().query.workerState.findFirst({ where: eq(workerState.id, "worker") });
|
|
if (!row) return OFFLINE;
|
|
return {
|
|
online: Date.now() - row.heartbeatAt.getTime() < OFFLINE_AFTER_MS,
|
|
hfVersion: row.hfVersion,
|
|
modelsDirFreeBytes: toMiB(row.modelsDirFreeBytes),
|
|
modelsDirTotalBytes: toMiB(row.modelsDirTotalBytes),
|
|
};
|
|
} catch {
|
|
// Table missing (worker never ran migrations yet) or DB unavailable.
|
|
return OFFLINE;
|
|
}
|
|
}
|