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:
Pascal LinxweilerandClaude Opus 5.5 committed 2026-10-05 16:57:12 +02:00
1 parent 8f7ac60ccf
commit 49440154a2
42 files changed
+2974 -110

No files matched your search

+111
View File
@@ -0,0 +1,111 @@
// What `hf download --local-dir <dir>` writes to disk (verified against huggingface_hub 2.1.1):
//
// <dir>/<path> final file (moved in place when complete)
// <dir>/.cache/huggingface/download/<path>.metadata "<commit sha>\n<etag>\n<timestamp>\n"
// <dir>/.cache/huggingface/download/<path>.lock lock file, left behind
// <dir>/.cache/huggingface/download/**/<b64>.<etag>.<random>.incomplete partial download
// <dir>/.cache/huggingface/trees/<sha>.json cached file tree
//
// The etag is the LFS sha256 for LFS/Xet files and the git blob SHA-1 for small files.
// Partial files are NOT resumed: every run writes a new <random> name, and a killed run leaves
// its .incomplete file behind. We delete those before each run.
import { readdir, readFile, rm, stat } from "node:fs/promises";
import { join, resolve, sep } from "node:path";
export const HF_CACHE_DIR = join(".cache", "huggingface");
const DOWNLOAD_DIR = join(HF_CACHE_DIR, "download");
/** Absolute local dir for a model; refuses paths that would escape MODELS_DIR. */
export function modelDir(modelsDir: string, localPath: string): string {
const root = resolve(modelsDir);
const dir = resolve(root, localPath);
if (dir === root || !dir.startsWith(root + sep)) {
throw new Error(`Refusing model path outside MODELS_DIR: ${localPath}`);
}
return dir;
}
export type FileMetadata = { commit: string; etag: string };
export async function readMetadata(dir: string, path: string): Promise<FileMetadata | null> {
try {
const text = await readFile(join(dir, DOWNLOAD_DIR, `${path}.metadata`), "utf8");
const [commit, etag] = text.split("\n");
return commit && etag ? { commit: commit.trim(), etag: etag.trim() } : null;
} catch {
return null;
}
}
/** Size of a file, or null if it doesn't exist. */
export async function fileSize(path: string): Promise<number | null> {
try {
const s = await stat(path);
return s.isFile() ? s.size : null;
} catch {
return null;
}
}
/** Extract the etag (40 or 64 hex chars) from an .incomplete file name. */
export function etagFromIncompleteName(name: string): string | null {
if (!name.endsWith(".incomplete")) return null;
for (const part of name.split(".")) {
if (/^(?:[0-9a-f]{40}|[0-9a-f]{64})$/.test(part)) return part;
}
return null;
}
export type IncompleteFile = { path: string; etag: string | null; size: number };
/** All partial files under the local dir's download cache. */
export async function listIncomplete(dir: string): Promise<IncompleteFile[]> {
const root = join(dir, DOWNLOAD_DIR);
let entries;
try {
entries = await readdir(root, { recursive: true, withFileTypes: true });
} catch {
return [];
}
const out: IncompleteFile[] = [];
for (const e of entries) {
if (!e.isFile() || !e.name.endsWith(".incomplete")) continue;
const full = join(e.parentPath, e.name);
const size = await fileSize(full);
if (size == null) continue; // finished and moved between readdir and stat
out.push({ path: full, etag: etagFromIncompleteName(e.name), size });
}
return out;
}
/** Delete leftover partial files from earlier runs. Only call while no hf process uses the dir. */
export async function removeStaleIncomplete(dir: string): Promise<{ count: number; bytes: number }> {
const files = await listIncomplete(dir);
for (const f of files) await rm(f.path, { force: true });
return { count: files.length, bytes: files.reduce((s, f) => s + f.size, 0) };
}
export type ExpectedFile = { path: string; size: number; etags: string[] };
/**
* Bytes present for one expected file:
* - final file with the expected size and matching (or missing) metadata etag → complete
* - otherwise the size of its in-progress .incomplete file (matched by etag), else 0
*/
export async function bytesOnDisk(
dir: string,
file: ExpectedFile,
incomplete: IncompleteFile[],
): Promise<{ done: number; complete: boolean }> {
const size = await fileSize(join(dir, file.path));
if (size === file.size) {
const meta = await readMetadata(dir, file.path);
if (!meta || file.etags.includes(meta.etag)) return { done: file.size, complete: true };
}
let partial = 0;
for (const inc of incomplete) {
if (inc.etag && file.etags.includes(inc.etag)) partial = Math.max(partial, inc.size);
}
return { done: Math.min(partial, file.size), complete: false };
}