harden database interactions and improve error handling

This commit is contained in:
ImBenji 2026-04-28 18:58:49 +01:00
parent b9f7d1ff25
commit d67a896b32
2 changed files with 8 additions and 8 deletions

View file

@ -11,10 +11,11 @@ import {
} from "../utils/generateTTS";
const INTERVAL_MS = 5 * 60 * 1000;
const STALE_MINUTES = 15;
const STALE_PENDING_MINUTES = 15;
const STALE_GENERATING_MINUTES = 45; // generating can take a while for big lessons
function staleCutoff() {
return new Date(Date.now() - STALE_MINUTES * 60 * 1000).toISOString();
function staleCutoff(minutes: number) {
return new Date(Date.now() - minutes * 60 * 1000).toISOString();
}
async function repairCycle() {
@ -49,7 +50,6 @@ async function repairCycle() {
where: eq(topics.status, "generating"),
});
const cutoff = staleCutoff();
for (const t of stuckGenerating) {
const lesson = await db.query.lessons.findFirst({ where: eq(lessons.topicId, t.id) });
@ -93,7 +93,7 @@ async function repairCycle() {
// ── 4. stale generating branches (server died mid-branch) ────────────────
try {
const cutoff = staleCutoff();
const cutoff = staleCutoff(STALE_GENERATING_MINUTES);
const stale = await db
.update(lessons)
@ -121,7 +121,7 @@ async function repairCycle() {
// ── 5. stale pending branches (never kicked off or abandoned) ────────────
try {
const cutoff = staleCutoff();
const cutoff = staleCutoff(STALE_PENDING_MINUTES);
const stale = await db
.update(lessons)

View file

@ -1,5 +1,5 @@
import { mkdir, writeFile, access } from "fs/promises";
import { resolve } from "path";
import { resolve, dirname } from "path";
import { askAI } from "./openrouter";
import { ttsLimiter } from "./limiter";
@ -378,7 +378,7 @@ export async function generateClip(
buffer = Buffer.from(await res.arrayBuffer());
}
await mkdir(resolve(process.cwd(), "data/audio/labels"), { recursive: true });
await mkdir(dirname(outPath), { recursive: true });
await writeFile(outPath, buffer);
return { cost };
} catch (err: any) {