Comprehensive admin + user dashboards (production-ready)
This commit is contained in:
+18
-1
@@ -1,8 +1,11 @@
|
||||
import "dotenv/config";
|
||||
import { getBoss } from "@/lib/queue/pgboss";
|
||||
import { QUEUES, type GenerateEpisodePayload, type EchoPayload } from "@/lib/queue/jobs";
|
||||
import { runEpisodeGeneration } from "@/lib/ai/pipeline/generate-episode";
|
||||
import { runEpisodeGeneration, refundEpisodeUsage } from "@/lib/ai/pipeline/generate-episode";
|
||||
import { setEpisodeStatus } from "@/lib/episodes/status";
|
||||
import { recordHeartbeat } from "@/lib/queue/health";
|
||||
|
||||
const HEARTBEAT_NAME = "generation-worker";
|
||||
|
||||
const CONCURRENCY = Math.max(1, Number(process.env.WORKER_CONCURRENCY ?? "2"));
|
||||
|
||||
@@ -10,6 +13,12 @@ async function main() {
|
||||
const boss = await getBoss({ supervise: true });
|
||||
console.log(`[worker] started (concurrency=${CONCURRENCY})`);
|
||||
|
||||
// Liveness heartbeat so the admin health page can tell the worker is running.
|
||||
await recordHeartbeat(HEARTBEAT_NAME).catch(() => {});
|
||||
setInterval(() => {
|
||||
recordHeartbeat(HEARTBEAT_NAME).catch((e) => console.error("[worker] heartbeat failed", e));
|
||||
}, 15_000);
|
||||
|
||||
// Proof-of-loop queue used by health checks / verification.
|
||||
await boss.work<EchoPayload>(QUEUES.echo, { batchSize: 1 }, async (jobs) => {
|
||||
for (const job of jobs) console.log("[echo]", job.data);
|
||||
@@ -52,6 +61,14 @@ async function handleGenerate(job: {
|
||||
`[generate] ${episodeId} failed (attempt ${retryCount}/${retryLimit}): ${message}`
|
||||
);
|
||||
if (exhausted) {
|
||||
// Terminal failure: refund the usage the enqueuing caller reserved up
|
||||
// front, so a failed generation doesn't permanently consume quota.
|
||||
// (The worker never increments; see lib/usage/meter.ts invariant.)
|
||||
try {
|
||||
await refundEpisodeUsage(episodeId, type ?? "full");
|
||||
} catch (refundErr) {
|
||||
console.error(`[generate] ${episodeId} usage refund failed`, refundErr);
|
||||
}
|
||||
await setEpisodeStatus(episodeId, "FAILED", { errorMessage: message });
|
||||
} else {
|
||||
throw err; // let pg-boss retry with backoff
|
||||
|
||||
Reference in New Issue
Block a user