Files
Kaname/src/worker/model-status.ts
T
Pascal LinxweilerandClaude Opus 5.5 49440154a2 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>
2026-10-05 16:57:12 +02:00

37 lines
1.4 KiB
TypeScript

import { and, eq, inArray, sql } from "drizzle-orm";
import { db } from "@/lib/db/client";
import { ACTIVE_JOB_STATES, jobs, type ModelStatus, modelFiles, models } from "@/lib/db/schema";
/**
* Recompute a model's status from its jobs and files:
* - an active job → downloading
* - all selected files present → ready
* - `failure` given (last job failed) → failed
* - some files present → incomplete, none → pending
*/
export async function settleModelStatus(modelId: string, failure?: "failed"): Promise<ModelStatus> {
const d = db();
const active = await d
.select({ n: sql<number>`count(*)` })
.from(jobs)
.where(and(eq(jobs.modelId, modelId), inArray(jobs.state, ["queued", ...ACTIVE_JOB_STATES])));
let status: ModelStatus;
if (active[0].n > 0) {
status = "downloading";
} else {
const [counts] = await d
.select({
total: sql<number>`count(*)`,
present: sql<number>`sum(case when ${modelFiles.state} in ('present', 'verified') then 1 else 0 end)`,
})
.from(modelFiles)
.where(and(eq(modelFiles.modelId, modelId), eq(modelFiles.selected, true)));
const present = counts.present ?? 0;
if (counts.total > 0 && present === counts.total) status = "ready";
else if (failure) status = "failed";
else status = present > 0 ? "incomplete" : "pending";
}
await d.update(models).set({ status }).where(eq(models.id, modelId));
return status;
}