Harden async task flows and enhance analysis tooling
这个提交包含在:
24
server/db.ts
24
server/db.ts
@@ -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;
|
||||
|
||||
在新工单中引用
屏蔽一个用户