Import models that are already in the models folder

The worker scans MODELS_DIR (startup, every 2 min, on "Rescan") for
<org>/<name> folders that no model uses and stores them in local_folders.
The library lists them; Import opens Add model prefilled with the folder,
the commit hf recorded and the files on disk preselected. The import job
runs hf download into the existing folder: hf 2.1.1 hashes files it didn't
download itself, keeps matching ones and re-downloads the rest (verified
in Docker, including a corrupted file being replaced).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Pascal LinxweilerandClaude Opus 5.5 committed 2026-10-05 23:01:58 +02:00
1 parent 3603742637
commit 145818f149
16 files changed
+1418 -25

No files matched your search

+24 -1
View File
@@ -13,6 +13,7 @@ import { checkHubAvailability, modelsDueForHubCheck } from "@/lib/hub/availabili
import { backfillMetadata } from "@/lib/hub/refresh";
import { DownloadRunner } from "./download";
import { log } from "./log";
import { scanModelsDir } from "./scan";
import { settleModelStatus } from "./model-status";
const HEARTBEAT_MS = 5_000;
@@ -22,6 +23,8 @@ const LEASE_STALE_MS = 20_000;
const SHUTDOWN_GRACE_MS = 8_000;
/** How often to look for models whose Hub check is due (the check interval itself is configurable). */
const HUB_CHECK_SCAN_MS = 10 * 60_000;
/** How often MODELS_DIR is scanned for folders that could be imported (plus on request). */
const FOLDER_SCAN_MS = 2 * 60_000;
/** Touched on every heartbeat; the Docker healthcheck checks its age. */
const HEALTH_FILE = join(tmpdir(), "kaname-worker-alive");
@@ -103,12 +106,14 @@ async function heartbeat() {
.update(workerState)
.set(values)
.where(and(eq(workerState.id, "worker"), eq(workerState.instanceId, instanceId)))
.returning({ id: workerState.id });
.returning({ id: workerState.id, scanRequestedAt: workerState.scanRequestedAt });
if (kept.length === 0) {
log.error("lost the worker lease to another process; exiting");
await shutdown("lease lost", 1);
return;
}
const requested = kept[0].scanRequestedAt;
if (requested && requested.getTime() > lastScanStartedAt) void scanFolders();
await writeFile(HEALTH_FILE, String(Date.now())).catch(() => {});
}
@@ -229,6 +234,22 @@ function reportHubCheck(model: { repoId: string }, result: Awaited<ReturnType<ty
else if (result.state !== "available") log.warn(`${model.repoId}: ${result.state.replace("_", " ")} on the Hub (local files kept)`);
}
let scanning = false;
let lastScanStartedAt = 0;
async function scanFolders() {
if (scanning || stopping) return;
scanning = true;
lastScanStartedAt = Date.now();
try {
const n = await scanModelsDir(coreEnv().MODELS_DIR);
if (n > 0) log.info(`found ${n} folder(s) in the models dir that aren't in the library`);
} catch (err) {
log.error("models dir scan failed", err);
} finally {
scanning = false;
}
}
let checkingHub = false;
async function checkHubDue() {
const hours = coreEnv().UPDATE_CHECK_INTERVAL_HOURS;
@@ -314,6 +335,8 @@ async function main() {
timers.push(setInterval(() => heartbeat().catch((err) => log.error("heartbeat failed", err)), HEARTBEAT_MS));
timers.push(setInterval(() => void tick(), TICK_MS));
timers.push(setInterval(() => void checkHubDue(), HUB_CHECK_SCAN_MS));
timers.push(setInterval(() => void scanFolders(), FOLDER_SCAN_MS));
void scanFolders();
void tick();
setTimeout(() => void checkHubDue(), 15_000);
}