Harden async task flows and enhance analysis tooling

这个提交包含在:
cryptocommuniums-afk
2026-03-15 08:05:37 +08:00
父节点 585fd5773d
当前提交 cb643ac154
修改 14 个文件,包含 566 行新增33 行删除

查看文件

@@ -1631,7 +1631,11 @@ export async function claimNextBackgroundTask(workerId: string) {
const now = new Date();
const [nextTask] = await db.select().from(backgroundTasks)
.where(and(eq(backgroundTasks.status, "queued"), lte(backgroundTasks.runAfter, now)))
.where(and(
eq(backgroundTasks.status, "queued"),
lte(backgroundTasks.runAfter, now),
sql`${backgroundTasks.attempts} < ${backgroundTasks.maxAttempts}`,
))
.orderBy(asc(backgroundTasks.runAfter), asc(backgroundTasks.createdAt))
.limit(1);
@@ -1733,6 +1737,24 @@ export async function retryBackgroundTask(userId: number, taskId: string) {
return getBackgroundTaskById(taskId);
}
export async function failExhaustedBackgroundTasks(now: Date = new Date()) {
const db = await getDb();
if (!db) return;
await db.update(backgroundTasks).set({
status: "failed",
progress: 100,
message: "任务达到最大重试次数,已停止自动重试",
error: sql`coalesce(${backgroundTasks.error}, '任务达到最大重试次数')`,
workerId: null,
lockedAt: null,
completedAt: now,
}).where(and(
eq(backgroundTasks.status, "queued"),
lte(backgroundTasks.runAfter, now),
sql`${backgroundTasks.attempts} >= ${backgroundTasks.maxAttempts}`,
));
}
export async function requeueStaleBackgroundTasks(staleBefore: Date) {
const db = await getDb();
if (!db) return;