harden database interactions and improve error handling
This commit is contained in:
parent
b9f7d1ff25
commit
d67a896b32
2 changed files with 8 additions and 8 deletions
|
|
@ -11,10 +11,11 @@ import {
|
||||||
} from "../utils/generateTTS";
|
} from "../utils/generateTTS";
|
||||||
|
|
||||||
const INTERVAL_MS = 5 * 60 * 1000;
|
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() {
|
function staleCutoff(minutes: number) {
|
||||||
return new Date(Date.now() - STALE_MINUTES * 60 * 1000).toISOString();
|
return new Date(Date.now() - minutes * 60 * 1000).toISOString();
|
||||||
}
|
}
|
||||||
|
|
||||||
async function repairCycle() {
|
async function repairCycle() {
|
||||||
|
|
@ -49,7 +50,6 @@ async function repairCycle() {
|
||||||
where: eq(topics.status, "generating"),
|
where: eq(topics.status, "generating"),
|
||||||
});
|
});
|
||||||
|
|
||||||
const cutoff = staleCutoff();
|
|
||||||
for (const t of stuckGenerating) {
|
for (const t of stuckGenerating) {
|
||||||
const lesson = await db.query.lessons.findFirst({ where: eq(lessons.topicId, t.id) });
|
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) ────────────────
|
// ── 4. stale generating branches (server died mid-branch) ────────────────
|
||||||
try {
|
try {
|
||||||
const cutoff = staleCutoff();
|
const cutoff = staleCutoff(STALE_GENERATING_MINUTES);
|
||||||
|
|
||||||
const stale = await db
|
const stale = await db
|
||||||
.update(lessons)
|
.update(lessons)
|
||||||
|
|
@ -121,7 +121,7 @@ async function repairCycle() {
|
||||||
|
|
||||||
// ── 5. stale pending branches (never kicked off or abandoned) ────────────
|
// ── 5. stale pending branches (never kicked off or abandoned) ────────────
|
||||||
try {
|
try {
|
||||||
const cutoff = staleCutoff();
|
const cutoff = staleCutoff(STALE_PENDING_MINUTES);
|
||||||
|
|
||||||
const stale = await db
|
const stale = await db
|
||||||
.update(lessons)
|
.update(lessons)
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
import { mkdir, writeFile, access } from "fs/promises";
|
import { mkdir, writeFile, access } from "fs/promises";
|
||||||
import { resolve } from "path";
|
import { resolve, dirname } from "path";
|
||||||
import { askAI } from "./openrouter";
|
import { askAI } from "./openrouter";
|
||||||
import { ttsLimiter } from "./limiter";
|
import { ttsLimiter } from "./limiter";
|
||||||
|
|
||||||
|
|
@ -378,7 +378,7 @@ export async function generateClip(
|
||||||
buffer = Buffer.from(await res.arrayBuffer());
|
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);
|
await writeFile(outPath, buffer);
|
||||||
return { cost };
|
return { cost };
|
||||||
} catch (err: any) {
|
} catch (err: any) {
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue