fix(jd-publish): 重试复位后带回直推载荷+克隆缺源身份文案分档——35147 重试实录三件
This commit is contained in:
+7
@@ -48,4 +48,11 @@ public class JdPublishTaskPullSpiImpl implements ClientTaskPullSpi {
|
||||
log.info("[jd-publish] 兜底载荷已组装 taskId={}", task.getId());
|
||||
return payload;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, Object> buildQueuePushPayload(FileTaskEntity task) {
|
||||
// 页面直推语义:与建单响应同源、不带 claimed——重试复位后的任务仍是 PENDING,
|
||||
// 客户端必须走 activate 认领执行代(带 claimed 会跳过激活,服务端永远停在 PENDING)。
|
||||
return publishTaskService.buildQueuePayload(task);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -63,4 +63,11 @@ public class JdTaskPullSpiImpl implements ClientTaskPullSpi {
|
||||
create.getShopCategoryNames() == null ? 0 : create.getShopCategoryNames().size());
|
||||
return Map.of("type", QUEUE_TYPE, "data", data);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, Object> buildQueuePushPayload(FileTaskEntity task) {
|
||||
// 本实现的兜底载荷本就不带 claimed(采集客户端按登录身份自校验,无 activate 环节),
|
||||
// 重试直推与兜底拉取共用同一份组装,避免两处键集漂移。
|
||||
return buildQueuePayload(task);
|
||||
}
|
||||
}
|
||||
|
||||
+16
-3
@@ -2,6 +2,7 @@ package com.nanri.aiimage.modules.task.controller;
|
||||
|
||||
import com.nanri.aiimage.common.api.ApiResponse;
|
||||
import com.nanri.aiimage.common.security.AdminAuthSupport;
|
||||
import com.nanri.aiimage.modules.task.service.TaskClientPullService;
|
||||
import com.nanri.aiimage.modules.task.service.TaskInPlaceRetryService;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.Parameter;
|
||||
@@ -15,12 +16,19 @@ import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 失败任务重试(模块无关,2026-11-01 用户拍板)。
|
||||
*
|
||||
* <p>口径:失败任务不再自动克隆新任务(同一份输入会多出好几行结果),改由用户在
|
||||
* 「删除」旁点「重试」——在原任务上复位续跑,保留已收分片、解析载荷与已检测标记。
|
||||
* 外观专利另有模块专属端点 {@code /api/appearance-patent/tasks/{id}/retry},行为一致。
|
||||
*
|
||||
* <p>2026-11-21 起响应体带回「页面直推」queuePayload(实现了 {@code ClientTaskPullSpi}
|
||||
* 的本机执行类模块,如京东采集/上架):复位是 PENDING,前端把载荷推回 pywebview 队列
|
||||
* 客户端才会立刻认领——兜底拉取开关默认关闭,只复位不推送的任务会永远停在 PENDING
|
||||
* (35147 重试实录)。非本机执行模块 data 为 null,前端维持旧口径提示。
|
||||
*/
|
||||
@RestController
|
||||
@RequiredArgsConstructor
|
||||
@@ -30,18 +38,23 @@ import org.springframework.web.bind.annotation.RestController;
|
||||
public class TaskRetryController {
|
||||
|
||||
private final TaskInPlaceRetryService taskRetryService;
|
||||
private final TaskClientPullService clientPullService;
|
||||
private final AdminAuthSupport adminAuthSupport;
|
||||
|
||||
@PostMapping("/{taskId}/retry")
|
||||
@Operation(summary = "重试失败任务",
|
||||
description = "在原任务上复位(FAILED→PENDING),保留分片/解析载荷/已检测标记,不创建新任务。")
|
||||
public ApiResponse<Void> retry(
|
||||
description = "在原任务上复位(FAILED→PENDING),保留分片/解析载荷/已检测标记,不创建新任务。"
|
||||
+ "本机执行类模块同时带回页面直推 queuePayload,前端推回 pywebview 队列。")
|
||||
public ApiResponse<Map<String, Object>> retry(
|
||||
@Parameter(description = "任务 ID。", required = true) @PathVariable Long taskId,
|
||||
@Parameter(description = "当前用户 ID,必须与任务归属一致。", required = true)
|
||||
@RequestParam("user_id") Long userId,
|
||||
HttpServletRequest httpRequest) {
|
||||
userId = adminAuthSupport.resolveOwnershipUserId(httpRequest, userId);
|
||||
taskRetryService.retryFailedTask(taskId, userId);
|
||||
return ApiResponse.success(null);
|
||||
// 复位是 PENDING:把「页面直推」载荷(无 claimed,客户端照常 activate)一并带回,
|
||||
// 前端经桥推送客户端才会立刻认领;拿不到载荷时按旧口径提示,重试复位不受影响。
|
||||
Map<String, Object> queuePayload = clientPullService.buildQueuePushPayloadAfterRetry(taskId);
|
||||
return ApiResponse.success(queuePayload == null ? null : Map.of("queuePayload", queuePayload));
|
||||
}
|
||||
}
|
||||
|
||||
+28
@@ -209,6 +209,34 @@ public class TaskClientPullService {
|
||||
return claimed;
|
||||
}
|
||||
|
||||
/**
|
||||
* 重试复位后的「页面直推」载荷(无 claimed,客户端照常 activate):通用重试端点把它带回
|
||||
* 给前端,前端经 pywebview 桥推回本机队列——只复位不推的话任务停在 PENDING(兜底拉取
|
||||
* 开关默认关闭,PENDING 不再按时间判死,重试就是石沉大海;2026-11-21 任务 35147 实录)。
|
||||
* 模块无 SPI / 任务不存在 / 载荷组装失败一律返回 null,前端按旧口径提示「等客户端领取」,
|
||||
* 重试复位本身不受影响。
|
||||
*/
|
||||
public Map<String, Object> buildQueuePushPayloadAfterRetry(Long taskId) {
|
||||
if (taskId == null || taskId <= 0) {
|
||||
return null;
|
||||
}
|
||||
FileTaskEntity task = fileTaskMapper.selectById(taskId);
|
||||
if (task == null) {
|
||||
return null;
|
||||
}
|
||||
ClientTaskPullSpi handler = pullHandlers.get(task.getModuleType());
|
||||
if (handler == null) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
return handler.buildQueuePushPayload(task);
|
||||
} catch (Exception ex) {
|
||||
log.warn("[client-task-pull] 重试后组装直推载荷失败 taskId={} moduleType={} err={}",
|
||||
task.getId(), task.getModuleType(), ex.getMessage());
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/** 逐条原子认领候选任务并组装载荷;认领失败/载荷缺失的任务原样留给下一轮或标失败。 */
|
||||
private List<TaskClientPullVo> claimCandidates(List<FileTaskEntity> candidates, String scope) {
|
||||
List<TaskClientPullVo> claimed = new ArrayList<>();
|
||||
|
||||
@@ -27,6 +27,18 @@ public interface ClientTaskPullSpi {
|
||||
*/
|
||||
Map<String, Object> buildQueuePayload(FileTaskEntity task);
|
||||
|
||||
/**
|
||||
* 组装「页面直推」语义的载荷:失败任务在原任务上重试复位(FAILED→PENDING)后,前端经
|
||||
* pywebview 桥把它推回本机队列。与建单响应的载荷同形——<b>不带 claimed 标记</b>,
|
||||
* 客户端照常 activate 认领执行代;{@link #buildQueuePayload} 的 claimed 是兜底拉取
|
||||
* 专用(那条通道已把任务原子认领成 RUNNING,客户端凭它免激活),带进直推载荷会让
|
||||
* 客户端跳过 activate、服务端永远停在 PENDING(2026-11-21 任务 35147 重试实录)。
|
||||
* 默认 null=该模块不由本机客户端执行(重试后走原执行通道,前端不必推送)。
|
||||
*/
|
||||
default Map<String, Object> buildQueuePushPayload(FileTaskEntity task) {
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 认领成功后的模块侧动作:对齐各自的 activate(多数模块实现为
|
||||
* {@code cacheService.touchTaskHeartbeat(taskId)},让页面立刻看到 RUNNING)。
|
||||
|
||||
+51
-1
@@ -27,6 +27,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.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
@@ -291,6 +292,40 @@ class TaskClientPullServiceTest {
|
||||
"30 分钟的让位窗口必须比 2 分钟的基础窗口更早,否则 Linux 会抢在本地客户端前面双跑");
|
||||
}
|
||||
|
||||
/**
|
||||
* 重试复位后的「页面直推」载荷(2026-11-21 任务 35147 实录):重试只复位(FAILED→PENDING)
|
||||
* 不推送时任务停在 PENDING——兜底拉取开关默认关闭,PENDING 不再按时间判死,重试就是石沉大海。
|
||||
* 通用重试端点经本方法把载荷带回给前端推桥;直推载荷不带 claimed(客户端照常 activate)。
|
||||
*/
|
||||
@Test
|
||||
void 重试复位后按SPI带回直推载荷() {
|
||||
FileTaskEntity row = task(33L, "JD_PUBLISH");
|
||||
when(fileTaskMapper.selectById(33L)).thenReturn(row);
|
||||
Map<String, Object> push = Map.of("type", "jd-product-publish-run", "data", Map.of("taskId", 33));
|
||||
TaskClientPullService service = service(
|
||||
new StubHandler("JD_PUBLISH", payload("jd-product-publish-run"), push, null));
|
||||
|
||||
Map<String, Object> result = service.buildQueuePushPayloadAfterRetry(33L);
|
||||
|
||||
assertEquals(push, result, "直推载荷让前端推回队列,客户端认领执行代;claimed 是兜底拉取专用,不在直推语义里");
|
||||
}
|
||||
|
||||
@Test
|
||||
void 无SPI或组装失败的任务重试后不带直推载荷且不影响复位() {
|
||||
when(fileTaskMapper.selectById(77L)).thenReturn(task(77L, "APPEARANCE_PATENT"));
|
||||
when(fileTaskMapper.selectById(78L)).thenReturn(task(78L, "SIMILAR_ASIN"));
|
||||
when(fileTaskMapper.selectById(79L)).thenReturn(task(79L, "SIMILAR_ASIN"));
|
||||
StubHandler broken = new StubHandler("SIMILAR_ASIN", payload("similar-asin-run"), null,
|
||||
new IllegalStateException("boom"));
|
||||
TaskClientPullService service = service(broken);
|
||||
|
||||
assertNull(service.buildQueuePushPayloadAfterRetry(null));
|
||||
assertNull(service.buildQueuePushPayloadAfterRetry(0L));
|
||||
assertNull(service.buildQueuePushPayloadAfterRetry(999L), "任务不存在时返回 null,不抛");
|
||||
assertNull(service.buildQueuePushPayloadAfterRetry(77L), "无 SPI 的模块(Linux 执行)不走直推");
|
||||
assertNull(service.buildQueuePushPayloadAfterRetry(78L), "载荷组装失败只降级为不推送,不给重试添堵");
|
||||
}
|
||||
|
||||
private void stubPullCandidates(FileTaskEntity... rows) {
|
||||
when(fileTaskMapper.selectLocalChannelPendingTasks(any(), any(), any(), any(), any(), anyInt(),
|
||||
org.mockito.ArgumentMatchers.anyBoolean()))
|
||||
@@ -387,17 +422,24 @@ class TaskClientPullServiceTest {
|
||||
|
||||
private final String moduleType;
|
||||
private final Map<String, Object> queuePayload;
|
||||
private final Map<String, Object> pushPayload;
|
||||
private final RuntimeException failure;
|
||||
private int buildCalls;
|
||||
private int claimedCalls;
|
||||
|
||||
StubHandler(String moduleType, Map<String, Object> queuePayload) {
|
||||
this(moduleType, queuePayload, null);
|
||||
this(moduleType, queuePayload, null, null);
|
||||
}
|
||||
|
||||
StubHandler(String moduleType, Map<String, Object> queuePayload, RuntimeException failure) {
|
||||
this(moduleType, queuePayload, null, failure);
|
||||
}
|
||||
|
||||
StubHandler(String moduleType, Map<String, Object> queuePayload,
|
||||
Map<String, Object> pushPayload, RuntimeException failure) {
|
||||
this.moduleType = moduleType;
|
||||
this.queuePayload = queuePayload;
|
||||
this.pushPayload = pushPayload;
|
||||
this.failure = failure;
|
||||
}
|
||||
|
||||
@@ -415,6 +457,14 @@ class TaskClientPullServiceTest {
|
||||
return queuePayload;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, Object> buildQueuePushPayload(FileTaskEntity task) {
|
||||
if (failure != null) {
|
||||
throw failure;
|
||||
}
|
||||
return pushPayload;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onClaimed(FileTaskEntity task) {
|
||||
claimedCalls++;
|
||||
|
||||
Reference in New Issue
Block a user