Files
Kaname/src/components/job-actions.ts
T
Claude 76d851896e Live updates: one poller per tab instead of SSE streams
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
2026-11-01 20:46:54 +00:00

38 lines
1.2 KiB
TypeScript

"use client";
import { toast } from "sonner";
import { pokeLive } from "@/lib/live-store";
async function post(url: string): Promise<boolean> {
const res = await fetch(url, { method: "POST" });
if (res.ok) {
pokeLive(); // show the new job state now instead of on the next poll
return true;
}
const body = await res.json().catch(() => ({}));
toast.error(body.error?.message ?? `Request failed (${res.status})`);
return false;
}
export async function startDownload(modelId: string) {
if (await post(`/api/models/${modelId}/download`)) toast.success("Download queued");
}
export async function cancelJob(jobId: string) {
if (await post(`/api/jobs/${jobId}/cancel`)) toast.info("Cancelling…");
}
export async function startVerify(modelId: string, deep: boolean) {
if (await post(`/api/models/${modelId}/verify${deep ? "?deep=1" : ""}`)) toast.success(deep ? "Deep verify queued" : "Verify queued");
}
export async function startRepair(modelId: string) {
if (await post(`/api/models/${modelId}/repair`)) toast.success("Repair queued");
}
export async function startUpdate(modelId: string): Promise<boolean> {
const ok = await post(`/api/models/${modelId}/update`);
if (ok) toast.success("Update queued");
return ok;
}