Milestone (a): project skeleton and OIDC login
- Next.js 16 + Tailwind + shadcn/ui skeleton - Auth.js v5 generic OIDC (Pocket ID, PKCE) with optional group gate - proxy.ts gates all pages and API routes - Drizzle schema (models, files, jobs, logs, worker state) on SQLite/libsql - Worker skeleton: migrations, interrupted-job recovery, heartbeat Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
commit
891e496157
47 files changed
+14945
No files matched your search
@@ -0,0 +1,83 @@
|
||||
import { execFile } from "node:child_process";
|
||||
import { promisify } from "node:util";
|
||||
import { inArray } from "drizzle-orm";
|
||||
import { db } from "@/lib/db/client";
|
||||
import { runMigrations } from "@/lib/db/migrate";
|
||||
import { ACTIVE_JOB_STATES, jobLogs, jobs, workerState } from "@/lib/db/schema";
|
||||
import { coreEnv } from "@/lib/env";
|
||||
import { log } from "./log";
|
||||
|
||||
const HEARTBEAT_MS = 5_000;
|
||||
|
||||
async function detectHfVersion(): Promise<string | null> {
|
||||
try {
|
||||
const { stdout } = await promisify(execFile)("hf", ["version"], { timeout: 30_000 });
|
||||
return stdout.trim() || null;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/** Jobs that were running when the worker last stopped can't still be running: mark them interrupted. */
|
||||
async function recoverInterruptedJobs() {
|
||||
const stale = await db()
|
||||
.update(jobs)
|
||||
.set({ state: "interrupted", finishedAt: new Date(), speedBps: null, etaSeconds: null })
|
||||
.where(inArray(jobs.state, [...ACTIVE_JOB_STATES]))
|
||||
.returning({ id: jobs.id });
|
||||
if (stale.length === 0) return;
|
||||
await db()
|
||||
.insert(jobLogs)
|
||||
.values(
|
||||
stale.map((j) => ({
|
||||
jobId: j.id,
|
||||
stream: "system" as const,
|
||||
line: "Worker restarted while this job was running. Marked as interrupted; resume to continue.",
|
||||
})),
|
||||
);
|
||||
log.warn(`marked ${stale.length} job(s) as interrupted`);
|
||||
}
|
||||
|
||||
async function heartbeat(startedAt: Date, hfVersion: string | null) {
|
||||
const now = new Date();
|
||||
await db()
|
||||
.insert(workerState)
|
||||
.values({ id: "worker", pid: process.pid, hfVersion, startedAt, heartbeatAt: now })
|
||||
.onConflictDoUpdate({
|
||||
target: workerState.id,
|
||||
set: { pid: process.pid, hfVersion, startedAt, heartbeatAt: now },
|
||||
});
|
||||
}
|
||||
|
||||
async function main() {
|
||||
const env = coreEnv();
|
||||
log.info(`starting (pid ${process.pid}), models dir: ${env.MODELS_DIR}`);
|
||||
|
||||
await runMigrations();
|
||||
log.info("database migrated");
|
||||
|
||||
await recoverInterruptedJobs();
|
||||
|
||||
const hfVersion = await detectHfVersion();
|
||||
if (hfVersion) log.info(`hf CLI: ${hfVersion}`);
|
||||
else log.warn("hf CLI not found on PATH; downloads will fail until it is installed");
|
||||
|
||||
const startedAt = new Date();
|
||||
await heartbeat(startedAt, hfVersion);
|
||||
const timer = setInterval(() => {
|
||||
heartbeat(startedAt, hfVersion).catch((err) => log.error("heartbeat failed", err));
|
||||
}, HEARTBEAT_MS);
|
||||
|
||||
const shutdown = (signal: string) => {
|
||||
log.info(`received ${signal}, shutting down`);
|
||||
clearInterval(timer);
|
||||
process.exit(0);
|
||||
};
|
||||
process.on("SIGINT", () => shutdown("SIGINT"));
|
||||
process.on("SIGTERM", () => shutdown("SIGTERM"));
|
||||
}
|
||||
|
||||
main().catch((err) => {
|
||||
log.error("fatal", err);
|
||||
process.exit(1);
|
||||
});
|
||||
@@ -0,0 +1,12 @@
|
||||
// Console logger for the worker process (job output goes to the job_logs table instead).
|
||||
|
||||
function fmt(level: string, msg: string) {
|
||||
return `${new Date().toISOString()} [worker] ${level} ${msg}`;
|
||||
}
|
||||
|
||||
export const log = {
|
||||
info: (msg: string) => console.log(fmt("INFO ", msg)),
|
||||
warn: (msg: string) => console.warn(fmt("WARN ", msg)),
|
||||
error: (msg: string, err?: unknown) =>
|
||||
console.error(fmt("ERROR", msg), err instanceof Error ? err.stack ?? err.message : (err ?? "")),
|
||||
};
|
||||
Reference in new issue
Block a user