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
@@ -4,6 +4,7 @@ import com.nanri.aiimage.common.api.ApiResponse;
import com.nanri.aiimage.common.api.PageDefaults;
import com.nanri.aiimage.common.security.AdminAuthSupport;
import com.nanri.aiimage.modules.appearancepatent.model.dto.AppearancePatentParseRequest;
import com.nanri.aiimage.modules.appearancepatent.model.dto.AppearancePatentParsedPayloadDto;
import com.nanri.aiimage.modules.appearancepatent.model.dto.AppearancePatentSubmitResultRequest;
import com.nanri.aiimage.modules.appearancepatent.model.dto.AppearancePatentTaskBatchRequest;
import com.nanri.aiimage.modules.appearancepatent.model.vo.AppearancePatentDashboardVo;
@@ -57,6 +58,16 @@ public class AppearancePatentController {
adminAuthSupport.currentDeviceId(httpRequest)));
}
@GetMapping("/tasks/{taskId}/queue-payload")
@Operation(summary = "获取外观专利 Python 队列载荷", description = "本地采集模式下,前端取出 groups 后推本机客户端队列;只返回 Python 当前需要的分组,避免前端搬运完整解析大数据。")
public ApiResponse<AppearancePatentParsedPayloadDto> queuePayload(
@Parameter(description = "外观专利任务 ID", required = true, example = "7004")
@PathVariable Long taskId,
@Parameter(description = "当前用户 ID", required = true, example = "1")
@RequestParam("user_id") Long userId) {
return ApiResponse.success(service.queuePayload(taskId, userId));
}
@GetMapping("/dashboard")
@Operation(summary = "查询外观专利检测总览", description = "查询当前用户的运行中、成功、失败和已结束任务数量。页面进入时请求一次即可,不需要持续轮询。")
public ApiResponse<AppearancePatentDashboardVo> dashboard(
@@ -113,6 +113,13 @@ public class AppearancePatentTaskService extends AbstractFileTaskPipeline<Appear
@org.springframework.beans.factory.annotation.Autowired(required = false)
private com.nanri.aiimage.modules.usersecret.service.UserTaskGuardService taskGuard;
/**
* 采集路由开关:字段注入的理由同 {@code taskGuard}——本类构造器被 golden 用例按参数顺序
* new 出来,加参数会连带改一批测试。null=未注入(单测)时按「非本地」处理,退回后台语义。
*/
@org.springframework.beans.factory.annotation.Autowired(required = false)
private com.nanri.aiimage.config.CollectRoutingProperties collectRoutingProperties;
/**
* 参数顺序=迁移前 {@code @RequiredArgsConstructor} 的字段声明顺序,一字未动;
* 其中五枚(fileTaskMapper/fileResultMapper/taskDistributedLockService/
@@ -204,9 +211,44 @@ public class AppearancePatentTaskService extends AbstractFileTaskPipeline<Appear
throw new com.nanri.aiimage.common.exception.BusinessException(com.nanri.aiimage.modules
.usersecret.service.UserTaskGuardService.PAUSED_MESSAGE);
}
// 2026-10-29 本地采集开关:开着就恢复「页面推本地客户端」的启动语义;
// 关着走原后台语义(记录支持里对 PENDING 明确报「已切换后台自动执行」)。
if (collectRoutingProperties != null
&& collectRoutingProperties.isLocal(com.nanri.aiimage.config.CollectRoutingProperties.MODULE_APPEARANCE_PATENT)) {
activateForLocalClient(taskId, userId);
return;
}
recordSupport().activateTask(taskId, userId);
}
/**
* 本地采集模式的激活:只允许 PENDING→RUNNING 条件更新。
*
* <p>与客户端兜底拉取 / Linux 内部认领共用同一条原子翻转,谁先翻谁执行,
* 页面推送与后台接管不会重复跑同一份任务。
*/
private void activateForLocalClient(Long taskId, Long userId) {
FileTaskEntity task = fileTaskMapper.selectById(taskId);
if (task == null || !MODULE_TYPE.equals(task.getModuleType())
|| userId == null || !java.util.Objects.equals(userId, task.getUserId())) {
throw new com.nanri.aiimage.common.exception.BusinessException("任务不存在");
}
ensureTaskOwnedByCurrentInstance(task, "activate");
int updated = fileTaskMapper.update(null, new LambdaUpdateWrapper<FileTaskEntity>()
.eq(FileTaskEntity::getId, taskId)
.eq(FileTaskEntity::getStatus, com.nanri.aiimage.common.task.TaskStatuses.FileTask.PENDING)
.set(FileTaskEntity::getStatus, STATUS_RUNNING)
.set(FileTaskEntity::getUpdatedAt, LocalDateTime.now()));
if (updated == 0) {
FileTaskEntity latest = fileTaskMapper.selectById(taskId);
if (latest != null && STATUS_RUNNING.equals(latest.getStatus())) {
throw new com.nanri.aiimage.common.exception.BusinessException("任务已在执行中(可能已由客户端自动接管),无需重复启动");
}
throw new com.nanri.aiimage.common.exception.BusinessException("任务已结束");
}
taskCacheService.touchTaskHeartbeat(taskId);
}
public AppearancePatentDashboardVo dashboard(Long userId) {
return progressSupport().dashboard(userId);
}