feat(worker): refund AI scan quota on definitive job failure + sanitize DLQ jobId

This commit is contained in:
Sven (AAMOS AI)
2026-08-16 21:43:19 +07:00
parent 305b6d1886
commit baa5f6447a
2 changed files with 45 additions and 1 deletions
+6 -1
View File
@@ -4,6 +4,7 @@ import IORedis from "ioredis";
import { createServer } from "node:http";
import { createContext } from "./context.js";
import { processScanJob } from "./processors/scans.js";
import { refundAiScanForFailedJob } from "./processors/quota-refund.js";
import { processModerateRecipe } from "./processors/moderation.js";
import { processTranslateRecipe } from "./processors/translation.js";
import { processGenerateWeekPlan } from "./processors/weekplan.js";
@@ -186,6 +187,10 @@ worker.on("failed", async (job, err) => {
console.error(`${jobType} (${job?.id}): ${err.message} (attempt ${attemptsMade}/${attempts})`);
if (job && attemptsMade >= attempts) {
const _sjid = (job.data as { scanJobId?: unknown })?.scanJobId;
if (_sjid) {
try { await refundAiScanForFailedJob(ctx, String(_sjid)); } catch (e) { console.error("Kvot-refund misslyckades:", e); }
}
try {
await deadLetterQueue.add(
job.name,
@@ -197,7 +202,7 @@ worker.on("failed", async (job, err) => {
_failedAt: new Date().toISOString(),
},
{
jobId: `${instanceId}:${job.id}`,
jobId: `${instanceId}__${job.id}`.replaceAll(":", "_"),
removeOnComplete: { age: 30 * 24 * 60 * 60 }, // 30 dagar
removeOnFail: { age: 90 * 24 * 60 * 60 }, // 90 dagar
},