fix(跟价): 失败任务也产出并保留部分结果文件,不再只剩一条失败记录
会话掉线这类「跑到第 N 页才断」的场景,任务本就该是 FAILED,但已经跑出来的行 (哪些 ASIN 真实改价过)必须随任务一起交付,否则用户无从对账、也无法只重跑漏掉的。 - submitResult 的 error / success=false 分支改走 finalizeFailedShop:先按失败落库 (success=0 + 原因),若合并后仍有可用行就排队组装部分结果文件;一行可用数据都没有时 才退化成原来的纯失败(并清掉合并缓存) - markResultFilePending / assembleShopResult 增加 preserveFailure 语义:失败行只挂文件, 不把 success 洗成 1、也不清 errorMessage - updateTaskStatusFromLatestRows:失败行正在组装文件时不让任务提前终态, 否则前端一停轮询、下载按钮永不出现(失败任务的结果文件同样要能下载) PriceTrackTaskServiceTest 新增 2 条契约测试(部分结果排队 + 组装完成后仍失败且可下载), 跟价模块测试 20/20 通过。
This commit is contained in:
+97
@@ -10,6 +10,7 @@ import com.nanri.aiimage.modules.task.mapper.FileResultMapper;
|
||||
import com.nanri.aiimage.modules.task.mapper.FileTaskMapper;
|
||||
import com.nanri.aiimage.modules.task.model.entity.FileResultEntity;
|
||||
import com.nanri.aiimage.modules.task.model.entity.FileTaskEntity;
|
||||
import com.nanri.aiimage.modules.task.model.entity.TaskFileJobEntity;
|
||||
import com.nanri.aiimage.modules.task.service.TaskDistributedLockService;
|
||||
import com.nanri.aiimage.modules.task.service.TaskFileJobService;
|
||||
import com.nanri.aiimage.modules.task.service.TaskResultPayloadService;
|
||||
@@ -28,6 +29,7 @@ import java.util.Map;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyLong;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
@@ -255,6 +257,101 @@ class PriceTrackTaskServiceTest {
|
||||
verify(lock, times(2)).close();
|
||||
}
|
||||
|
||||
/**
|
||||
* 跟价 28443 场景:店铺跑到第 2 页会话掉线(955 件在售只跟了 20 件),
|
||||
* 客户端收尾回传带 error。失败归失败,但已经跑出来的行必须组装成部分结果文件,
|
||||
* 否则用户只看到「失败」,拿不到「哪些 ASIN 实际已被改价」的记录。
|
||||
*/
|
||||
@Test
|
||||
void failedShopWithPartialRowsStillQueuesResultFile() throws Exception {
|
||||
long taskId = 28443L;
|
||||
String shopName = "林芳";
|
||||
String error = "国家 英国 未跑完:会话失效:页面已跳转到登录页,商品列表为空";
|
||||
|
||||
FileTaskEntity task = runningTask(taskId);
|
||||
FileResultEntity result = pendingResult(taskId, shopName);
|
||||
|
||||
PriceTrackSubmitResultRequest.ShopResult shopResult = shopResult(
|
||||
shopName,
|
||||
Map.of("UK", List.of(asinRow("B0DWX8WJF8", "改价成功", "改价成功"))));
|
||||
shopResult.setError(error);
|
||||
PriceTrackSubmitResultRequest request = new PriceTrackSubmitResultRequest();
|
||||
request.setShops(List.of(shopResult));
|
||||
|
||||
TaskFileJobEntity pendingJob = new TaskFileJobEntity();
|
||||
pendingJob.setTaskId(taskId);
|
||||
pendingJob.setResultId(result.getId());
|
||||
pendingJob.setScopeKey(shopName);
|
||||
pendingJob.setStatus("PENDING");
|
||||
|
||||
TaskDistributedLockService.LockHandle lock = mock(TaskDistributedLockService.LockHandle.class);
|
||||
when(taskDistributedLockService.acquire("PRICE_TRACK", taskId)).thenReturn(lock);
|
||||
when(priceTrackTaskCacheService.getTaskCacheBatch(List.of(taskId))).thenReturn(Map.of(taskId, task));
|
||||
when(fileResultMapper.selectList(any())).thenReturn(List.of(result));
|
||||
when(ziniaoShopSwitchService.normalizeShopName(shopName)).thenReturn(shopName);
|
||||
when(excelAssemblyService.normalizeCountriesMap(any())).thenAnswer(invocation -> invocation.getArgument(0));
|
||||
when(excelAssemblyService.countRows(any())).thenReturn(1);
|
||||
when(taskFileJobService.findAssembleJobsByResultIds(anyString(), any()))
|
||||
.thenReturn(Map.of(result.getId(), pendingJob));
|
||||
when(objectMapper.writeValueAsString(any())).thenReturn("[]");
|
||||
|
||||
service.submitResult(taskId, request);
|
||||
|
||||
// 部分结果文件已排队,且失败态没被"洗成成功"
|
||||
verify(taskFileJobService).enqueueAssembleResult(taskId, "PRICE_TRACK", result.getId(), shopName);
|
||||
assertEquals(0, result.getSuccess());
|
||||
assertEquals(error, result.getErrorMessage());
|
||||
assertEquals(shopName + ".xlsx", result.getResultFilename());
|
||||
assertNull(result.getResultFileUrl(), "文件还没组装完,URL 由组装任务回填");
|
||||
|
||||
// 文件还没落地前任务必须留在 RUNNING:提前终态会让前端停轮询、下载按钮永不出现
|
||||
assertEquals("RUNNING", task.getStatus());
|
||||
assertNull(task.getFinishedAt());
|
||||
}
|
||||
|
||||
/**
|
||||
* 部分结果文件组装完成后:文件挂上去了,但行/任务仍是失败态,
|
||||
* 用户既能看到"没跑完",也能下载已跑出来的部分。
|
||||
*/
|
||||
@Test
|
||||
void assembledPartialResultKeepsFailedStateAndStaysDownloadable() throws Exception {
|
||||
long taskId = 28444L;
|
||||
String shopName = "林芳";
|
||||
String error = "国家 英国 未跑完:会话失效";
|
||||
String objectKey = "result/price_track/abc/林芳.xlsx";
|
||||
|
||||
FileTaskEntity task = runningTask(taskId);
|
||||
FileResultEntity result = pendingResult(taskId, shopName);
|
||||
result.setErrorMessage(error);
|
||||
result.setResultFilename(shopName + ".xlsx");
|
||||
|
||||
TaskFileJobEntity job = new TaskFileJobEntity();
|
||||
job.setTaskId(taskId);
|
||||
job.setResultId(result.getId());
|
||||
job.setScopeKey(shopName);
|
||||
job.setStatus("PENDING");
|
||||
|
||||
when(priceTrackTaskCacheService.getTaskCacheBatch(List.of(taskId))).thenReturn(Map.of(taskId, task));
|
||||
when(fileResultMapper.selectById(result.getId())).thenReturn(result);
|
||||
when(fileResultMapper.selectList(any())).thenReturn(List.of(result));
|
||||
when(taskResultPayloadService.getLatest(eq(taskId), eq("PRICE_TRACK"), eq(shopName),
|
||||
eq(PriceTrackSubmitResultRequest.ShopResult.class)))
|
||||
.thenReturn(shopResult(shopName,
|
||||
Map.of("UK", List.of(asinRow("B0DWX8WJF8", "改价成功", "改价成功")))));
|
||||
when(excelAssemblyService.normalizeCountriesMap(any())).thenAnswer(invocation -> invocation.getArgument(0));
|
||||
when(excelAssemblyService.countRows(any())).thenReturn(1);
|
||||
when(ossStorageService.uploadResultFile(any(), eq("PRICE_TRACK"))).thenReturn(objectKey);
|
||||
when(objectMapper.writeValueAsString(any())).thenReturn("[]");
|
||||
|
||||
service.processResultFileJob(job);
|
||||
|
||||
assertEquals(0, result.getSuccess(), "部分结果不改变失败态");
|
||||
assertEquals(error, result.getErrorMessage(), "失败原因不能被组装洗掉");
|
||||
assertEquals(objectKey, result.getResultFileUrl(), "文件挂上去,失败任务也能下载");
|
||||
assertEquals("FAILED", task.getStatus());
|
||||
assertTrue(task.getErrorMessage() != null && task.getErrorMessage().contains("未跑完"));
|
||||
}
|
||||
|
||||
private FileTaskEntity runningTask(long taskId) {
|
||||
FileTaskEntity task = new FileTaskEntity();
|
||||
task.setId(taskId);
|
||||
|
||||
Reference in New Issue
Block a user