feat(collect-routing): 专利/货源本地采集开关与 Linux 兜底窗口

外观专利与相似ASIN默认「本地采集」,由页面推本机客户端;本地推了没人领时
PENDING 超过 linux-fallback-minutes(默认 30 分钟)才允许 Linux 内部认领接管,
避免本地机全关时任务无限排队(PENDING 已不再按时间判死)。

- 新增 aiimage.collect-routing.local-enabled 与 GET /api/collect-routing
- 客户端兜底拉取只认本地模块;内部认领对本地模块按兜底窗口二次过滤
- 外观专利 activate 在本地模式下恢复 PENDING→RUNNING 条件更新(与认领互斥),
  关闭时保留后台拒绝语义;相似ASIN 关闭时明确拒绝「点启动」
- 恢复 GET /api/appearance-patent/tasks/{id}/queue-payload(本地推送取载荷用)
- /api/collect-routing 进用户态守卫名单,两处前缀计数基线 15→16 / 23→24
This commit is contained in:
2026-10-29 20:27:53 +08:00
parent 8b7d5d4a40
commit 39c01ada44
17 changed files with 578 additions and 14 deletions
@@ -0,0 +1,147 @@
package com.nanri.aiimage.modules.appearancepatent.service;
import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.nanri.aiimage.common.exception.BusinessException;
import com.nanri.aiimage.common.service.DistributedJobLockService;
import com.nanri.aiimage.common.service.ResultDownloadResolver;
import com.nanri.aiimage.config.AppearancePatentProperties;
import com.nanri.aiimage.config.CollectRoutingProperties;
import com.nanri.aiimage.config.InstanceMetadata;
import com.nanri.aiimage.config.StorageProperties;
import com.nanri.aiimage.modules.appearancepatent.client.AppearancePatentLlmClient;
import com.nanri.aiimage.modules.task.mapper.FileResultMapper;
import com.nanri.aiimage.modules.task.mapper.FileTaskMapper;
import com.nanri.aiimage.modules.task.mapper.TaskChunkMapper;
import com.nanri.aiimage.modules.task.mapper.TaskScopeStateMapper;
import com.nanri.aiimage.modules.task.model.entity.FileTaskEntity;
import com.nanri.aiimage.modules.task.service.TaskDistributedLockService;
import com.nanri.aiimage.modules.task.service.TaskFileJobService;
import com.nanri.aiimage.modules.task.service.TaskHeartbeatPositionService;
import com.nanri.aiimage.modules.task.service.TaskProgressLightAssembler;
import com.nanri.aiimage.modules.task.service.TaskProgressSnapshotService;
import com.nanri.aiimage.modules.task.service.TransientPayloadStorageService;
import com.nanri.aiimage.modules.usersecret.service.UserApiSecretService;
import org.apache.ibatis.builder.MapperBuilderAssistant;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;
import org.springframework.test.util.ReflectionTestUtils;
import org.springframework.transaction.PlatformTransactionManager;
import java.util.Map;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.isNull;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
/**
* 外观专利 activate 的采集路由(2026-10-29):
* 本地采集开着 ⇒ 恢复「PENDING→RUNNING 条件更新 + 刷心跳」,页面推本地客户端前必须能翻状态;
* 关着 ⇒ 保持后台语义,明确拒绝,避免旧页面缓存继续推队列造成双跑。
*/
class AppearancePatentActivateRoutingTest {
private static final long TASK_ID = 7004L;
private static final long USER_ID = 1L;
private final FileTaskMapper fileTaskMapper = mock(FileTaskMapper.class);
private final AppearancePatentTaskCacheService taskCacheService =
mock(AppearancePatentTaskCacheService.class);
@BeforeEach
void initTableInfo() {
TableInfoHelper.initTableInfo(new MapperBuilderAssistant(new MybatisConfiguration(), ""), FileTaskEntity.class);
}
@Test
void 本地采集开启时_activate_走条件更新并刷心跳() {
FileTaskEntity task = pendingTask();
when(fileTaskMapper.selectById(TASK_ID)).thenReturn(task);
when(fileTaskMapper.update(isNull(), any())).thenReturn(1);
AppearancePatentTaskService service = service(routing(true));
service.activateTask(TASK_ID, USER_ID);
@SuppressWarnings("unchecked")
ArgumentCaptor<Wrapper<FileTaskEntity>> captor = ArgumentCaptor.forClass(Wrapper.class);
verify(fileTaskMapper).update(isNull(), captor.capture());
Map<String, Object> params = paramsOf(captor.getValue());
assertTrue(params.containsValue("PENDING"), "激活必须限定 PENDING,才能与后台认领互斥");
assertTrue(params.containsValue("RUNNING"));
verify(taskCacheService).touchTaskHeartbeat(TASK_ID);
}
@Test
void 本地采集关闭时_activate_保持后台语义并明确拒绝() {
when(fileTaskMapper.selectById(TASK_ID)).thenReturn(pendingTask());
AppearancePatentTaskService service = service(routing(false));
BusinessException error = assertThrows(BusinessException.class,
() -> service.activateTask(TASK_ID, USER_ID));
assertTrue(String.valueOf(error.getMessage()).contains("后台自动执行"), error.getMessage());
verify(fileTaskMapper, never()).update(isNull(), any());
}
private static CollectRoutingProperties routing(boolean local) {
CollectRoutingProperties properties = new CollectRoutingProperties();
properties.setLocalEnabled(Map.of(CollectRoutingProperties.MODULE_APPEARANCE_PATENT, local));
return properties;
}
private FileTaskEntity pendingTask() {
FileTaskEntity task = new FileTaskEntity();
task.setId(TASK_ID);
task.setModuleType(AppearancePatentTaskService.MODULE_TYPE);
task.setUserId(USER_ID);
task.setStatus("PENDING");
return task;
}
private AppearancePatentTaskService service(CollectRoutingProperties routing) {
AppearancePatentTaskService service = new AppearancePatentTaskService(
mock(com.nanri.aiimage.modules.file.service.LocalFileStorageService.class),
null,
mock(ResultDownloadResolver.class),
mock(StorageProperties.class),
fileTaskMapper,
mock(FileResultMapper.class),
mock(TaskScopeStateMapper.class),
mock(TaskChunkMapper.class),
new ObjectMapper(),
mock(AppearancePatentLlmClient.class),
taskCacheService,
mock(AppearancePatentProperties.class),
mock(TaskFileJobService.class),
mock(TaskProgressSnapshotService.class),
mock(TaskHeartbeatPositionService.class),
mock(TransientPayloadStorageService.class),
mock(PlatformTransactionManager.class),
mock(DistributedJobLockService.class),
mock(TaskDistributedLockService.class),
mock(InstanceMetadata.class),
mock(TaskProgressLightAssembler.class),
mock(UserApiSecretService.class));
ReflectionTestUtils.setField(service, "collectRoutingProperties", routing);
return service;
}
@SuppressWarnings("unchecked")
private static Map<String, Object> paramsOf(Wrapper<FileTaskEntity> wrapper) {
wrapper.getSqlSegment();
((LambdaUpdateWrapper<FileTaskEntity>) wrapper).getSqlSet();
Map<String, Object> params = (Map<String, Object>) ReflectionTestUtils.getField(wrapper, "paramNameValuePairs");
assertTrue(params != null && !params.isEmpty());
return params;
}
}
@@ -0,0 +1,48 @@
package com.nanri.aiimage.modules.task.controller;
import com.nanri.aiimage.config.CollectRoutingProperties;
import org.junit.jupiter.api.Test;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import java.util.Map;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
/**
* 采集路由读取口:前端两个工具页据这两个布尔决定「启动」是推本地客户端还是提交后台。
* 开关的唯一真源在服务端配置,这里只钉「配置怎么配,接口就怎么答」。
*/
class CollectRoutingControllerTest {
@Test
void 开关默认开启时两个模块都答本地() throws Exception {
CollectRoutingProperties properties = new CollectRoutingProperties();
properties.setLocalEnabled(Map.of("APPEARANCE_PATENT", true, "SIMILAR_ASIN", true));
MockMvc mockMvc = MockMvcBuilders
.standaloneSetup(new CollectRoutingController(properties))
.build();
mockMvc.perform(get("/api/collect-routing"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.success").value(true))
.andExpect(jsonPath("$.data.appearancePatentLocal").value(true))
.andExpect(jsonPath("$.data.similarAsinLocal").value(true));
}
@Test
void 关闭本地采集时接口如实回答且未登记模块不影响这两项() throws Exception {
CollectRoutingProperties properties = new CollectRoutingProperties();
properties.setLocalEnabled(Map.of("APPEARANCE_PATENT", false, "SIMILAR_ASIN", true));
MockMvc mockMvc = MockMvcBuilders
.standaloneSetup(new CollectRoutingController(properties))
.build();
mockMvc.perform(get("/api/collect-routing"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.data.appearancePatentLocal").value(false))
.andExpect(jsonPath("$.data.similarAsinLocal").value(true));
}
}
@@ -190,13 +190,96 @@ class TaskClientPullServiceTest {
}
}
// ------------------------------------------------------------ 本地采集路由(2026-10-29)
@Test
void 客户端兜底拉取只认本地模块() {
com.nanri.aiimage.config.CollectRoutingProperties routing =
new com.nanri.aiimage.config.CollectRoutingProperties();
routing.setLocalEnabled(Map.of("SIMILAR_ASIN", false, "COLLECT_DATA", false));
List<TaskClientPullVo> result = serviceWithRouting(routing,
new StubHandler("SIMILAR_ASIN", payload("similar-asin-run"))).pullPendingTasks(USER_ID);
assertTrue(result.isEmpty(), "模块切到 Linux 后台后客户端不应再拉取");
verifyNoInteractions(fileTaskMapper);
}
@Test
void 内部认领对本地模块让位到兜底窗口() {
com.nanri.aiimage.config.CollectRoutingProperties routing =
new com.nanri.aiimage.config.CollectRoutingProperties();
routing.setLocalEnabled(Map.of("SIMILAR_ASIN", true));
routing.setLinuxFallbackMinutes(30L);
when(fileTaskMapper.selectList(any())).thenReturn(List.of(task(11L, "SIMILAR_ASIN", 10L)));
List<TaskClientPullVo> result = serviceWithRouting(routing,
new StubHandler("SIMILAR_ASIN", payload("similar-asin-run")))
.pullPendingTasksInternal(List.of("SIMILAR_ASIN"));
assertTrue(result.isEmpty(), "本地采集模块在兜底窗口内必须让位给本地客户端");
verify(fileTaskMapper, never()).update(isNull(), any());
}
@Test
void 内部认领超过兜底窗口后接管本地模块() {
com.nanri.aiimage.config.CollectRoutingProperties routing =
new com.nanri.aiimage.config.CollectRoutingProperties();
routing.setLocalEnabled(Map.of("SIMILAR_ASIN", true));
routing.setLinuxFallbackMinutes(30L);
FileTaskEntity candidate = task(11L, "SIMILAR_ASIN", 40L);
when(fileTaskMapper.selectList(any())).thenReturn(List.of(candidate));
when(fileTaskMapper.update(isNull(), any())).thenReturn(1);
when(fileTaskMapper.selectById(11L)).thenReturn(candidate);
List<TaskClientPullVo> result = serviceWithRouting(routing,
new StubHandler("SIMILAR_ASIN", payload("similar-asin-run")))
.pullPendingTasksInternal(List.of("SIMILAR_ASIN"));
assertEquals(1, result.size(), "本地长时间没人领,Linux 必须兜底接管");
}
@Test
void 内部认领对非本地模块保持原等待窗口() {
com.nanri.aiimage.config.CollectRoutingProperties routing =
new com.nanri.aiimage.config.CollectRoutingProperties();
routing.setLocalEnabled(Map.of("SIMILAR_ASIN", true));
routing.setLinuxFallbackMinutes(30L);
FileTaskEntity candidate = task(21L, "COLLECT_DATA", 10L);
when(fileTaskMapper.selectList(any())).thenReturn(List.of(candidate));
when(fileTaskMapper.update(isNull(), any())).thenReturn(1);
when(fileTaskMapper.selectById(21L)).thenReturn(candidate);
List<TaskClientPullVo> result = serviceWithRouting(routing,
new StubHandler("COLLECT_DATA", payload("collect-data-run")))
.pullPendingTasksInternal(List.of("COLLECT_DATA"));
assertEquals(1, result.size(), "未登记本地开关的模块不受兜底窗口影响");
}
private TaskClientPullService service(ClientTaskPullSpi... handlers) {
return serviceWithRouting(defaultRouting(), handlers);
}
/** 默认路由:两个模块都按本地采集(现有用例的既有语义)。 */
private static com.nanri.aiimage.config.CollectRoutingProperties defaultRouting() {
com.nanri.aiimage.config.CollectRoutingProperties routing =
new com.nanri.aiimage.config.CollectRoutingProperties();
routing.setLocalEnabled(Map.of("SIMILAR_ASIN", true, "COLLECT_DATA", true));
return routing;
}
private TaskClientPullService serviceWithRouting(
com.nanri.aiimage.config.CollectRoutingProperties routing,
ClientTaskPullSpi... handlers) {
TaskClientPullService service = new TaskClientPullService(
fileTaskMapper, new InstanceMetadata("server-test"), List.of(handlers));
fileTaskMapper, new InstanceMetadata("server-test"), routing, List.of(handlers));
ReflectionTestUtils.setField(service, "enabled", true);
ReflectionTestUtils.setField(service, "moduleTypes", "SIMILAR_ASIN,COLLECT_DATA");
ReflectionTestUtils.setField(service, "minPendingMinutes", 5L);
ReflectionTestUtils.setField(service, "limit", 5);
ReflectionTestUtils.setField(service, "internalEnabled", true);
ReflectionTestUtils.setField(service, "internalMinPendingMinutes", 2L);
return service;
}
@@ -243,14 +326,18 @@ class TaskClientPullServiceTest {
}
private static FileTaskEntity task(Long id, String moduleType) {
return task(id, moduleType, 10L);
}
private static FileTaskEntity task(Long id, String moduleType, long createdAtMinutesAgo) {
FileTaskEntity task = new FileTaskEntity();
task.setId(id);
task.setModuleType(moduleType);
task.setTaskNo(moduleType + "-" + id);
task.setUserId(USER_ID);
task.setStatus("PENDING");
task.setCreatedAt(LocalDateTime.now().minusMinutes(10));
task.setUpdatedAt(LocalDateTime.now().minusMinutes(10));
task.setCreatedAt(LocalDateTime.now().minusMinutes(createdAtMinutesAgo));
task.setUpdatedAt(LocalDateTime.now().minusMinutes(createdAtMinutesAgo));
return task;
}