Milestone (c): worker and downloads with live progress
- Worker queue with per-model exclusivity and MAX_CONCURRENT_DOWNLOADS, spawns `hf download --revision <sha> --local-dir ... --include <exact paths>` with HF_TOKEN via env only - Progress from bytes on disk (final + .incomplete files matched by etag), per job and per file, with speed and ETA; post-download size/etag check - Job logs (sanitized), cancel, graceful interrupt on shutdown, partial-file cleanup (hf 2.1.1 does not resume partials) - Single-worker DB lease, free-space check - SSE live updates for library, model and job pages - Dockerfile + compose.yaml (web, worker, data/models volumes) - HF_HUB_DISABLE_XET default on for measurable progress Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
8f7ac60ccf
commit
49440154a2
42 files changed
+2974
-110
No files matched your search
@@ -0,0 +1,58 @@
|
||||
"use client";
|
||||
|
||||
import { useRouter } from "next/navigation";
|
||||
import { useEffect, useRef, useState } from "react";
|
||||
import type { JobFileSnapshot, JobSnapshot, LogLine } from "@/lib/jobs";
|
||||
import type { WorkerStatus } from "@/lib/worker-status";
|
||||
|
||||
export type LiveSnapshot = { worker: WorkerStatus; jobs: JobSnapshot[]; files: JobFileSnapshot[] };
|
||||
|
||||
export const TERMINAL_STATES = ["completed", "failed", "cancelled", "interrupted"];
|
||||
const MAX_LOG_LINES = 5000;
|
||||
|
||||
/**
|
||||
* Subscribe to /api/stream. Re-renders on every snapshot; refreshes server components
|
||||
* (model status, library) when a job reaches a terminal state.
|
||||
*/
|
||||
export function useLive(query: { model?: string; job?: string } = {}, withLogs = false) {
|
||||
const router = useRouter();
|
||||
const [snapshot, setSnapshot] = useState<LiveSnapshot | null>(null);
|
||||
const [logs, setLogs] = useState<LogLine[]>([]);
|
||||
const [connected, setConnected] = useState(false);
|
||||
const states = useRef(new Map<string, string>());
|
||||
|
||||
const params = new URLSearchParams();
|
||||
if (query.model) params.set("model", query.model);
|
||||
if (query.job) params.set("job", query.job);
|
||||
const url = `/api/stream${params.size ? `?${params}` : ""}`;
|
||||
|
||||
useEffect(() => {
|
||||
const es = new EventSource(url);
|
||||
es.onopen = () => setConnected(true);
|
||||
es.onerror = () => setConnected(false);
|
||||
es.addEventListener("snapshot", (e) => {
|
||||
const snap = JSON.parse((e as MessageEvent).data) as LiveSnapshot;
|
||||
let finished = false;
|
||||
for (const j of snap.jobs) {
|
||||
const prev = states.current.get(j.id);
|
||||
if (prev && prev !== j.state && TERMINAL_STATES.includes(j.state)) finished = true;
|
||||
states.current.set(j.id, j.state);
|
||||
}
|
||||
setSnapshot(snap);
|
||||
if (finished) router.refresh();
|
||||
});
|
||||
if (withLogs) {
|
||||
es.addEventListener("logs", (e) => {
|
||||
const lines = JSON.parse((e as MessageEvent).data) as LogLine[];
|
||||
setLogs((prev) => {
|
||||
const seen = prev.length ? prev[prev.length - 1].id : 0;
|
||||
const merged = prev.concat(lines.filter((l) => l.id > seen));
|
||||
return merged.length > MAX_LOG_LINES ? merged.slice(-MAX_LOG_LINES) : merged;
|
||||
});
|
||||
});
|
||||
}
|
||||
return () => es.close();
|
||||
}, [url, withLogs, router]);
|
||||
|
||||
return { snapshot, logs, connected };
|
||||
}
|
||||
Reference in new issue
Block a user