fix: stop retrying expired image archives forever

This commit is contained in:
134 2026-08-20 19:34:56 +08:00
parent 88dec71a41
commit 3d12c508d3

View File

@ -1,12 +1,13 @@
import "server-only"; import "server-only";
import { and, asc, eq, inArray, lte, sql } from "drizzle-orm"; import { and, asc, eq, inArray, lt, lte, sql } from "drizzle-orm";
import { db } from "@/lib/db"; import { db } from "@/lib/db";
import { imageArchiveTask, type ImageArchiveTask } from "@/lib/db/schema"; import { imageArchiveTask, type ImageArchiveTask } from "@/lib/db/schema";
export type ImageArchiveTaskRecord = ImageArchiveTask; export type ImageArchiveTaskRecord = ImageArchiveTask;
const DEFAULT_DISPLAY_HOLD_HOURS = 1; const DEFAULT_DISPLAY_HOLD_HOURS = 1;
const MAX_IMAGE_ARCHIVE_ATTEMPTS = 12;
function getDisplayHoldHours() { function getDisplayHoldHours() {
const raw = Number( const raw = Number(
@ -95,7 +96,8 @@ export async function getImageArchiveTasksToStore(params: { limit: number }) {
.where( .where(
and( and(
inArray(imageArchiveTask.status, ["pending", "failed"]), inArray(imageArchiveTask.status, ["pending", "failed"]),
lte(imageArchiveTask.archiveAfterAt, now) lte(imageArchiveTask.archiveAfterAt, now),
lt(imageArchiveTask.attemptCount, MAX_IMAGE_ARCHIVE_ATTEMPTS)
) )
) )
.orderBy(asc(imageArchiveTask.createdAt), asc(imageArchiveTask.id)) .orderBy(asc(imageArchiveTask.createdAt), asc(imageArchiveTask.id))
@ -139,11 +141,18 @@ export async function markImageArchiveTaskFailed(params: {
const message = String( const message = String(
params.error instanceof Error ? params.error.message : params.error || "Unknown error" params.error instanceof Error ? params.error.message : params.error || "Unknown error"
).slice(0, 2000); ).slice(0, 2000);
const isPermanentSourceError = /\bHTTP (?:404|410)\b/i.test(message);
await db await db
.update(imageArchiveTask) .update(imageArchiveTask)
.set({ .set({
status: "failed", status: isPermanentSourceError
? "abandoned"
: sql`case
when ${imageArchiveTask.attemptCount} >= ${MAX_IMAGE_ARCHIVE_ATTEMPTS}
then 'abandoned'
else 'failed'
end`,
lastError: message, lastError: message,
archiveAfterAt: new Date(Date.now() + 10 * 60 * 1000), archiveAfterAt: new Date(Date.now() + 10 * 60 * 1000),
updatedAt: new Date(), updatedAt: new Date(),