mirror of
https://github.com/margrop/wrong-notebook.git
synced 2026-12-03 23:11:36 +08:00
fix: 重复的并发重试返回当前任务,结果不依赖请求到达先后
- 另一个重试已抢占并在执行时,后到的重试请求返回当前任务(200),与同时到达但抢占失败的请求一致, 不再因先后不同返回 409;不重复识别、不重复计额度 - 从未失败、正在首次识别的任务仍拒绝重试
This commit is contained in:
@@ -420,6 +420,19 @@ describe('识别导入任务', () => {
|
||||
await waitForImportTaskForTest(id);
|
||||
expect(await database.aiUsage.count({ where: { userId: 'user-1', kind: 'analyze' } })).toBe(2);
|
||||
expect(mocks.analyzeImage).toHaveBeenCalledTimes(2);
|
||||
|
||||
// 先后到达的重复重试(第一个已抢占并在识别中)同样返回当前任务,不再识别、不计额度
|
||||
await database.importTask.update({ where: { id }, data: { status: 'failed', result: null, error: 'AI_TIMEOUT_ERROR' } });
|
||||
let releaseAnalysis: () => void = () => undefined;
|
||||
mocks.analyzeImage.mockImplementationOnce(() => new Promise((resolve) => { releaseAnalysis = () => resolve(parsed()); }));
|
||||
expect((await RETRY(json({}), params(id))).status).toBe(202);
|
||||
const late = await RETRY(json({}), params(id));
|
||||
expect(late.status).toBe(200);
|
||||
expect((await late.json()).task.status).toBe('analyzing');
|
||||
releaseAnalysis();
|
||||
await waitForImportTaskForTest(id);
|
||||
expect(await database.aiUsage.count({ where: { userId: 'user-1', kind: 'analyze' } })).toBe(3);
|
||||
expect(mocks.analyzeImage).toHaveBeenCalledTimes(3);
|
||||
});
|
||||
|
||||
it('拒绝非法状态迁移,确认时不接受前端替换图片', async () => {
|
||||
|
||||
@@ -594,6 +594,10 @@ export async function retryImportTask(user: User, id: string): Promise<NextRespo
|
||||
const task = await findOwnTask(user, id);
|
||||
if (!task) return notFound('IMPORT_TASK_NOT_FOUND');
|
||||
if (task.status !== 'failed') {
|
||||
// 重复的重试请求(另一个重试已抢占、正在执行):与抢占失败的并发请求一样返回当前任务,结果不依赖请求先后
|
||||
if ((task.status === 'analyzing' || task.status === 'saving') && task.attempts > 1) {
|
||||
return NextResponse.json({ task: toDetail(task) });
|
||||
}
|
||||
return createErrorResponse('IMPORT_TASK_NOT_RETRYABLE', 409, ErrorCode.CONFLICT, { status: task.status });
|
||||
}
|
||||
const hasResult = parseResult(task.result) !== null;
|
||||
|
||||
Reference in New Issue
Block a user