进度(light透传): 把心跳已存的 current/total 交给前端,为客户端任务进度条打通服务端一段

客户端心跳一直在发 current/total,Java 也已存进 Redis 位置 Hash,但除判死豁免外没人读它,
所以客户端界面上解决风控/跟价这些任务画不出进度条。本次只在 light 出口上透传,不新建表、不写迁移。

- TaskProgressLightVo + progressCurrent/progressTotal/progressLabel(三个同生同灭)
- 装配器只对非终态任务读位置;读不到或分母非正数一律留 null,由前端决定不画(0/0 会被读成"进度 0%",与"没有进度"是两件事)
- 位置服务加 readDrawablePositions:单次轮询封顶 20 次 Redis 往返,按尝试次数计(老客户端一个都不发时命中数为 0 但往返数仍是全量)
- 标签按「,」整段截到 60 字,定长硬切会截出半截店铺名
- 相似ASIN 那份私有 LightVo 同步(它是历史遗留的第二套实现,不同步会让该模块进度随轮询闪烁)
- 顺带修该私有实现的投影缺陷:select 少 module_type 一列,真实查询下所有任务被判 missing,
  与共享装配器 2026-09-14 那次线上事故同形态(那边有守卫测试、这份没有),现补守卫
This commit is contained in:
2026-10-10 20:04:52 +08:00
parent 07757b9f6d
commit deb3d7e161
11 changed files with 437 additions and 4 deletions
@@ -20,4 +20,11 @@ public class SimilarAsinTaskLightVo {
private Boolean fileReady;
@Schema(description = "最后更新时间,ISO 本地时间字符串。", example = "2026-04-26T10:05:00")
private String updatedAt;
/** 客户端心跳上报的执行进度,三者同生同灭;无进度信息时全为 null(前端不画进度轨)。 */
@Schema(description = "已处理数(客户端心跳上报;无进度信息时为 null)。", example = "12")
private Integer progressCurrent;
@Schema(description = "总处理单位数(相似ASIN 为待检测 ASIN 行数)。", example = "50")
private Integer progressTotal;
@Schema(description = "位置标签(中文,按「,」整段截短;无则为 null)。", example = "ASIN B0ABCD1234(12/50)")
private String progressLabel;
}
@@ -11,6 +11,7 @@ import com.fasterxml.jackson.databind.node.ObjectNode;
import com.nanri.aiimage.common.exception.BusinessException;
import com.nanri.aiimage.common.exception.TaskOwnerMismatchException;
import com.nanri.aiimage.common.security.AdminAuthSupport;
import com.nanri.aiimage.common.task.TaskStatuses;
import com.nanri.aiimage.common.util.LockSkipWitness;
import com.nanri.aiimage.common.service.DistributedJobLockService;
import com.nanri.aiimage.common.util.FailedStatusRowFilter;
@@ -628,6 +629,7 @@ public class SimilarAsinTaskService implements SimilarAsinPipelineHost {
Map<Long, FileTaskEntity> taskMap = new LinkedHashMap<>();
for (FileTaskEntity task : fileTaskMapper.selectList(new LambdaQueryWrapper<FileTaskEntity>()
.select(FileTaskEntity::getId,
FileTaskEntity::getModuleType,
FileTaskEntity::getStatus,
FileTaskEntity::getUpdatedAt)
.in(FileTaskEntity::getId, normalizedIds))) {
@@ -636,6 +638,15 @@ public class SimilarAsinTaskService implements SimilarAsinPipelineHost {
}
}
Map<Long, TaskFileJobEntity> jobMap = taskFileJobService.findAssembleJobsByTaskIds(MODULE_TYPE, new ArrayList<>(taskMap.keySet()));
List<Long> activeTaskIds = new ArrayList<>();
for (Long taskId : normalizedIds) {
FileTaskEntity active = taskMap.get(taskId);
if (active != null && !TaskStatuses.FileTask.TERMINAL.contains(active.getStatus())) {
activeTaskIds.add(taskId);
}
}
Map<Long, TaskHeartbeatPositionService.Position> positions =
taskHeartbeatPositionService.readDrawablePositions(activeTaskIds);
for (Long taskId : normalizedIds) {
FileTaskEntity task = taskMap.get(taskId);
if (task == null) {
@@ -653,11 +664,28 @@ public class SimilarAsinTaskService implements SimilarAsinPipelineHost {
item.setFileStatus(job == null ? null : job.getStatus());
item.setFileError(job == null ? null : job.getErrorMessage());
item.setUpdatedAt(fmt(task.getUpdatedAt()));
fillProgressFromHeartbeat(item, positions.get(taskId));
vo.getItems().add(item);
}
return vo;
}
/**
* 透传客户端心跳上报的处理位置,让相似ASIN 的 light 轮询也能画进度条。
*
* <p>口径与共享 {@code TaskProgressLightAssembler} 一致:终态任务不读、读不到或分母非正数时
* 三个字段一起留 null,由前端决定「不画」。(相似ASIN 的私有 LightVo 是历史遗留的第二份实现,
* 两边行为必须同步,否则这一模块的进度条会跟着轮询闪烁。)
*/
private void fillProgressFromHeartbeat(SimilarAsinTaskLightVo item, TaskHeartbeatPositionService.Position drawable) {
if (drawable == null) {
return;
}
item.setProgressCurrent(drawable.current());
item.setProgressTotal(drawable.total());
item.setProgressLabel(drawable.label());
}
public SimilarAsinTaskBatchVo progressBatch(List<Long> taskIds) {
SimilarAsinTaskBatchVo vo = new SimilarAsinTaskBatchVo();
List<Long> requestedIds = taskIds == null ? List.of() : taskIds.stream()
@@ -31,4 +31,14 @@ public class TaskProgressLightVo {
*/
@Schema(description = "结果行版本(毫秒),用于行数据按需拉取。", example = "1757788800000")
private String rowsVersion;
/**
* 客户端心跳上报的执行进度。三者同生同灭:读不到位置就全为 null(前端据此不画进度轨),
* 不会出现 progressTotal=0 配上 progressCurrent=0 那种「0% 空轨」。
*/
@Schema(description = "已处理数(客户端心跳上报;无进度信息时为 null)。", example = "12")
private Integer progressCurrent;
@Schema(description = "总处理单位数(粒度按模块而定:ASIN 行/店铺×国家/累计页)。", example = "50")
private Integer progressTotal;
@Schema(description = "位置标签(中文,按「,」整段截短;无则为 null)。", example = "国家 US(2/5)")
private String progressLabel;
}
@@ -8,7 +8,9 @@ import org.springframework.stereotype.Service;
import java.time.Duration;
import java.time.Instant;
import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
/**
@@ -27,6 +29,9 @@ public class TaskHeartbeatPositionService {
private static final String KEY_PREFIX = "task:heartbeat:position:";
private static final String STALL_KEY_PREFIX = "task:progress:stall:";
private static final Duration POSITION_TTL = Duration.ofHours(24);
private static final int POSITION_LABEL_MAX_LEN = 60;
/** 单次 light 轮询最多读多少个任务的心跳位置(Redis 往返上限,见 {@link #readDrawablePositions})。 */
public static final int MAX_POSITION_LOOKUPS_PER_CALL = 20;
private final StringRedisTemplate stringRedisTemplate;
@@ -44,6 +49,28 @@ public class TaskHeartbeatPositionService {
UNKNOWN
}
/**
* 结构化位置读数。{@code current}/{@code total} 可能为 null——客户端只发了文本、
* 没发计数(老版本按四级 key 归一化时 total 全为 0 就不发)。
*/
public record Position(Integer current, Integer total, String label) {
/**
* 归一化成「能画」的读数:total 非正数即视为没有进度(返回 null),
* current 缺失/为负按 0、超过 total 按 total 钳住(客户端计数与分母不同源,会超)。
*
* <p>共享 {@code TaskProgressLightVo} 与 similarasin 自己那份私有 LightVo 都走这里,
* 两个出口不会有第二种钳位口径。
*/
public Position drawable() {
if (total == null || total <= 0) {
return null;
}
int safeCurrent = current == null || current < 0 ? 0 : Math.min(current, total);
return new Position(safeCurrent, total, label);
}
}
public void record(Long taskId, TaskHeartbeatRequest request) {
if (taskId == null || taskId <= 0 || request == null) {
return;
@@ -106,6 +133,103 @@ public class TaskHeartbeatPositionService {
return sb.length() == 0 ? null : sb.toString();
}
/**
* 读取结构化进度读数,供 {@code progress/light} 轮询透传给前端画进度条。
*
* <p>与 {@link #describe} 的分工:那里给文案,这里给可参与除法的分子/分母。数据同源,
* 都是客户端心跳写进 {@code task:heartbeat:position:{taskId}} 的那一份,<b>不另建存储</b>。
*
* <p>读不到时返回 {@code null},调用方必须按「这里没有进度可看」处理——<b>不得</b>把 null
* 折成 {@code 0/0}:0/0 在前端会画成 0% 空轨,而空轨读起来是"进度 0%",与"没有进度"是两件事。
*/
public Position readPosition(Long taskId) {
if (taskId == null || taskId <= 0) {
return null;
}
Map<Object, Object> entries;
try {
entries = stringRedisTemplate.opsForHash().entries(KEY_PREFIX + taskId);
} catch (Exception ex) {
log.warn("[heartbeat-position] 读取结构化位置失败 taskId={} msg={}", taskId, ex.getMessage());
return null;
}
if (entries == null || entries.isEmpty()) {
return null;
}
Integer current = parseIntOrNull(value(entries, "current"));
Integer total = parseIntOrNull(value(entries, "total"));
if (current == null && total == null) {
return null;
}
return new Position(current, total, trimLabel(value(entries, "progressText")));
}
/**
* 批量取「能画」的位置读数,供 {@code progress/light} 轮询使用。
*
* <p>封顶 {@link #MAX_POSITION_LOOKUPS_PER_CALL} 次读取:light 是每 10 秒一次的轮询面、
* 单次最多带 200 个任务 id,逐个 HGETALL 会把 Redis 读成这条链路的新瓶颈。调用方已筛掉终态,
* 剩下的「真在跑」通常只有个位数;超出上限就宁可少画几条进度,也不给轮询加无界往返。
*
* <p>计数按<b>尝试次数</b>而非命中数:老客户端一个都不发位置时,命中数为 0 但往返数照样是 200。
*
* @return taskId → 可画读数;读不到位置或分母非正数的任务不出现在结果里
*/
public Map<Long, Position> readDrawablePositions(Collection<Long> taskIds) {
if (taskIds == null || taskIds.isEmpty()) {
return Map.of();
}
Map<Long, Position> result = new LinkedHashMap<>();
int attempts = 0;
for (Long taskId : taskIds) {
if (attempts >= MAX_POSITION_LOOKUPS_PER_CALL) {
break;
}
attempts++;
Position position = readPosition(taskId);
Position drawable = position == null ? null : position.drawable();
if (drawable != null) {
result.put(taskId, drawable);
}
}
return result;
}
private static Integer parseIntOrNull(String raw) {
if (raw == null || raw.isEmpty()) {
return null;
}
try {
return Integer.valueOf(raw);
} catch (NumberFormatException ex) {
return null;
}
}
/**
* 位置文案按「,」整段截短(light 每 10s 轮询、单次最多 200 个任务,不能把心跳全文搬过去)。
* 按分隔符收口而不是定长切,避免截出「国家 U…」这种半截标签。
*/
private static String trimLabel(String text) {
if (text == null || text.isEmpty()) {
return null;
}
StringBuilder kept = new StringBuilder();
for (String segment : text.split(",")) {
if (kept.length() > 0 && kept.length() + segment.length() + 1 > POSITION_LABEL_MAX_LEN) {
break;
}
if (kept.length() == 0 && segment.length() > POSITION_LABEL_MAX_LEN) {
return segment.substring(0, POSITION_LABEL_MAX_LEN);
}
if (kept.length() > 0) {
kept.append(',');
}
kept.append(segment);
}
return kept.length() == 0 ? null : kept.toString();
}
/**
* 判定客户端是否「仍在推进」,供封口/判死前的豁免使用。
*
@@ -1,6 +1,7 @@
package com.nanri.aiimage.modules.task.service;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.nanri.aiimage.common.task.TaskStatuses;
import com.nanri.aiimage.modules.task.mapper.FileResultMapper;
import com.nanri.aiimage.modules.task.mapper.FileTaskMapper;
import com.nanri.aiimage.modules.task.model.dto.TaskRowsVersionDto;
@@ -33,6 +34,7 @@ public class TaskProgressLightAssembler {
private final FileTaskMapper fileTaskMapper;
private final FileResultMapper fileResultMapper;
private final TaskFileJobService taskFileJobService;
private final TaskHeartbeatPositionService taskHeartbeatPositionService;
public TaskProgressLightBatchVo assemble(String moduleType, Long userId, List<Long> taskIds) {
TaskProgressLightBatchVo vo = new TaskProgressLightBatchVo();
@@ -61,6 +63,16 @@ public class TaskProgressLightAssembler {
}
}
Map<Long, TaskFileJobEntity> jobMap = taskFileJobService.findAssembleJobsByTaskIds(moduleType, new ArrayList<>(taskMap.keySet()));
// 心跳位置只在非终态任务上取(终态的位置 24h 会过期,且跑完的任务该按状态显示而不是中途百分比)
List<Long> activeTaskIds = new ArrayList<>();
for (Long taskId : normalizedIds) {
FileTaskEntity task = taskMap.get(taskId);
if (task != null && !TaskStatuses.FileTask.TERMINAL.contains(task.getStatus())) {
activeTaskIds.add(taskId);
}
}
Map<Long, TaskHeartbeatPositionService.Position> positions =
taskHeartbeatPositionService.readDrawablePositions(activeTaskIds);
// 行数据版本:一次 GROUP BY 拿回每个任务结果行的(最后变更时间, 行数),作为行数据按需拉取的信号
Map<Long, String> rowsVersions = new LinkedHashMap<>();
if (!taskMap.isEmpty()) {
@@ -90,8 +102,27 @@ public class TaskProgressLightAssembler {
item.setFileError(job == null ? null : job.getErrorMessage());
item.setUpdatedAt(task.getUpdatedAt() == null ? null : task.getUpdatedAt().toString());
item.setRowsVersion(rowsVersions.get(taskId));
applyPosition(item, positions.get(taskId));
vo.getItems().add(item);
}
return vo;
}
/**
* 把客户端心跳上报的处理位置透给前端画进度条。
*
* <p>入参已由 {@link #assemble} 筛过:只有非终态任务才会去读位置,且读不到/分母非正数时
* {@code positions} 里根本没有这个任务。三种情况都落到「三个字段留 null」——前端
* {@code TaskItemCard} 见 {@code progress=null} 就不渲染进度轨。这是有意的:
* <b>没有进度</b>和<b>进度 0%</b>是两件事,把前者画成后者会让人以为任务卡住了
* (2026-10-04 用户就「0/1 空轨」明确否过一次)。
*/
private void applyPosition(TaskProgressLightVo item, TaskHeartbeatPositionService.Position drawable) {
if (drawable == null) {
return;
}
item.setProgressCurrent(drawable.current());
item.setProgressTotal(drawable.total());
item.setProgressLabel(drawable.label());
}
}
@@ -88,6 +88,7 @@ class SimilarAsinTaskServiceLightTest {
@Mock private TaskFileJobService taskFileJobService;
@Mock private TaskDistributedLockService taskDistributedLockService;
@Mock private TaskProgressSnapshotService taskProgressSnapshotService;
@Mock private com.nanri.aiimage.modules.task.service.TaskHeartbeatPositionService taskHeartbeatPositionService;
@Mock private TransientPayloadStorageService transientPayloadStorageService;
@Mock private org.springframework.transaction.PlatformTransactionManager transactionManager;
@Mock private DistributedJobLockService distributedJobLockService;
@@ -191,6 +192,45 @@ class SimilarAsinTaskServiceLightTest {
jobDb.add(job(12L, 3L, "FAILED", null));
}
/**
* 守卫:light 的任务查询必须选中 module_type。
*
* <p>下面 634 行用 {@code MODULE_TYPE.equals(task.getModuleType())} 做模块过滤,投影里少这一列
* 真实查询就取不到它 → 所有任务被判 missing,这一模块的轮询整面失明。共享装配器 2026-09-14
* 就栽在同一个形态上(那边有守卫,这份私有实现没有)。mock 返回完整实体,所以只有断言 SQL
* 投影才能抓到它。
*/
@Test
void lightTaskQueryMustSelectModuleType() {
seed();
service.progressLight(List.of(1L));
org.mockito.ArgumentCaptor<Wrapper> captor = org.mockito.ArgumentCaptor.forClass(Wrapper.class);
verify(fileTaskMapper).selectList(captor.capture());
String select = ((LambdaQueryWrapper<?>) captor.getValue()).getSqlSelect();
assertTrue(select != null && select.contains("module_type"),
"light 投影缺 module_type 会把所有任务判成 missing,实际=" + select);
}
/** 心跳位置透传:非终态任务带进度,终态条目不带。 */
@Test
void lightCarriesHeartbeatProgressOnlyForActiveTask() {
seed();
lenient().when(taskHeartbeatPositionService.readDrawablePositions(anyList())).thenAnswer(invocation -> {
List<Long> ids = invocation.getArgument(0);
Map<Long, com.nanri.aiimage.modules.task.service.TaskHeartbeatPositionService.Position> map = new HashMap<>();
for (Long id : ids) {
map.put(id, new com.nanri.aiimage.modules.task.service.TaskHeartbeatPositionService.Position(3, 4, "asin 3/4"));
}
return map;
});
SimilarAsinTaskLightBatchVo vo = service.progressLight(List.of(1L, 2L));
assertEquals(4, vo.getItems().get(0).getProgressTotal(), "RUNNING 任务带心跳进度");
assertEquals(3, vo.getItems().get(0).getProgressCurrent());
assertNull(vo.getItems().get(1).getProgressTotal(), "SUCCESS 任务不得带中途百分比");
}
@Test
void lightReturnsLightFields() {
seed();
@@ -119,6 +119,7 @@ class SimilarAsinTaskServicePerf500Test {
@Mock private TaskFileJobService taskFileJobService;
@Mock private TaskDistributedLockService taskDistributedLockService;
@Mock private TaskProgressSnapshotService taskProgressSnapshotService;
@Mock private com.nanri.aiimage.modules.task.service.TaskHeartbeatPositionService taskHeartbeatPositionService;
@Mock private TransientPayloadStorageService transientPayloadStorageService;
@Mock private org.springframework.transaction.PlatformTransactionManager transactionManager;
@Mock private DistributedJobLockService distributedJobLockService;
@@ -26,7 +26,9 @@ class TaskProgressLightContractTest {
private static final Set<String> WHITELIST = new TreeSet<>(Set.of(
// rowsVersion:结果行版本(「行数据按需拉取」信号,2026-09-14 加入),仍是轻量字段、不含行内容
"taskId", "status", "statusCode", "fileStatus", "fileError", "fileReady", "updatedAt", "rowsVersion"));
"taskId", "status", "statusCode", "fileStatus", "fileError", "fileReady", "updatedAt", "rowsVersion",
// progress*:客户端心跳上报的执行进度(2026-10-10 加入,三个字段同生同灭,用于画进度条)
"progressCurrent", "progressTotal", "progressLabel"));
private final ObjectMapper objectMapper = new ObjectMapper();
@@ -81,6 +83,30 @@ class TaskProgressLightContractTest {
assertTrue(bytes.length < 1024, "单条 light 响应 <1KB,实际 " + bytes.length + "B");
}
/**
* 体积闸门:加了 progress* 三字段后,带满长度标签的单条 light 响应仍须 &lt;1KB。
*
* <p>light 是每 10 秒一次、单次最多 200 个任务的轮询面,标签一旦不封顶就会把心跳全文
* (200 字,含店铺名/关键词/ASIN)按任务数放大成带宽成本。这里用中文标签喂,
* 因为 UTF-8 下一个汉字 3 字节——用 ASCII 喂这条断言等于没测。
*/
@Test
void commonShapeWithFullProgressLabelStillSmall() throws Exception {
TaskProgressLightVo vo = fullItem();
vo.setProgressCurrent(120);
vo.setProgressTotal(150);
StringBuilder filler = new StringBuilder();
while (filler.length() < 60) {
filler.append("店铺=shop-us-east,国家=US,ASIN=B0ABCD1234(3/10),");
}
vo.setProgressLabel(filler.substring(0, 60));
TaskProgressLightBatchVo batch = new TaskProgressLightBatchVo();
batch.getItems().add(vo);
byte[] bytes = objectMapper.writeValueAsBytes(ApiResponse.success(batch));
assertTrue(bytes.length < 1024,
"带 60 字中文标签的单条 light 响应仍须 <1KB,实际 " + bytes.length + "B");
}
@Test
void commonShapeBatchHasOnlyItemsAndMissing() throws Exception {
TaskProgressLightBatchVo batch = new TaskProgressLightBatchVo();
@@ -115,7 +141,8 @@ class TaskProgressLightContractTest {
void commonShapeWhitelistOrderedDocumented() {
// 回归门禁:与 spec 06 §2 白名单一致(taskId/status/statusCode?/fileStatus?/fileError?/fileReady?/updatedAt
// + 2026-09-14 新增 rowsVersion:结果行版本,行数据按需拉取的信号,仍是轻量字段)
assertEquals(List.of("fileError", "fileReady", "fileStatus", "rowsVersion", "status", "statusCode", "taskId", "updatedAt"),
assertEquals(List.of("fileError", "fileReady", "fileStatus", "progressCurrent", "progressLabel",
"progressTotal", "rowsVersion", "status", "statusCode", "taskId", "updatedAt"),
new java.util.ArrayList<>(WHITELIST));
}
@@ -58,6 +58,83 @@ class TaskHeartbeatPositionServiceTest {
verify(stringRedisTemplate).expire(eq("task:heartbeat:position:123"), any(Duration.class));
}
@Test
void readPositionReturnsStructuredNumbers() {
when(stringRedisTemplate.opsForHash()).thenReturn(hashOperations);
when(hashOperations.entries("task:heartbeat:position:123")).thenReturn(Map.of(
"current", "2", "total", "10", "progressText", "US(2/10)"));
TaskHeartbeatPositionService.Position position = service().readPosition(123L);
assertEquals(2, position.current());
assertEquals(10, position.total());
assertEquals("US(2/10)", position.label());
}
/** 没有位置(老客户端、或已过期)必须返回 null,调用方按「没有进度」处理。 */
@Test
void readPositionReturnsNullWhenHashAbsent() {
when(stringRedisTemplate.opsForHash()).thenReturn(hashOperations);
when(hashOperations.entries("task:heartbeat:position:123")).thenReturn(Map.of());
assertNull(service().readPosition(123L));
}
/**
* 只有文本、没有计数时也要返回 null:前端画的是百分比,光有文案算不出分母。
* 若把这种位置透下去,前端只能画「未知/总」的空轨。
*/
@Test
void readPositionReturnsNullWhenCountersAbsent() {
when(stringRedisTemplate.opsForHash()).thenReturn(hashOperations);
when(hashOperations.entries("task:heartbeat:position:123"))
.thenReturn(Map.of("progressText", "shop A"));
assertNull(service().readPosition(123L));
}
/** 计数字段是 Redis 里的裸字符串,脏值/非数字不得把整次轮询带崩。 */
@Test
void readPositionToleratesUnparsableCurrent() {
when(stringRedisTemplate.opsForHash()).thenReturn(hashOperations);
when(hashOperations.entries("task:heartbeat:position:123"))
.thenReturn(Map.of("current", "abc", "total", "10"));
TaskHeartbeatPositionService.Position position = service().readPosition(123L);
assertNull(position.current());
assertEquals(10, position.total());
assertEquals(0, position.drawable().current(), "分子脏值按 0 计,分母仍可用");
}
@Test
void drawableClampsAndRejectsNonPositiveTotal() {
assertNull(new TaskHeartbeatPositionService.Position(5, 0, "x").drawable());
assertNull(new TaskHeartbeatPositionService.Position(5, null, "x").drawable());
assertEquals(50, new TaskHeartbeatPositionService.Position(57, 50, "x").drawable().total());
assertEquals(50, new TaskHeartbeatPositionService.Position(57, 50, "x").drawable().current());
assertEquals(0, new TaskHeartbeatPositionService.Position(-3, 50, "x").drawable().current());
}
/**
* 标签按「,」整段收口:定长硬切会截出「国家 U…」这种半截标签,
* 而 light 的响应体积闸门(单条 &lt;1KB)又是必须守的,所以只能按分隔符截。
*/
@Test
void labelTrimmedAtSeparatorNotMidSegment() {
when(stringRedisTemplate.opsForHash()).thenReturn(hashOperations);
String longLabel = "阶段=crawling,店铺 魏振峰(2/10),国家 US(3/5),关键词 血糖测试仪,ASIN B0ABCDEFGH(4/10)";
when(hashOperations.entries("task:heartbeat:position:123"))
.thenReturn(Map.of("current", "4", "total", "10", "progressText", longLabel));
String label = service().readPosition(123L).label();
assertTrue(label.length() <= 60, "标签须封顶 60 字,实际 " + label.length());
assertTrue(longLabel.startsWith(label), "截短结果必须是原文前缀,不能重排");
assertFalse(label.endsWith("国") || label.endsWith("家"), "不得截在半段中间");
assertEquals("阶段=crawling,店铺 魏振峰(2/10),国家 US(3/5),关键词 血糖测试仪", label);
}
@Test
void recordSkipsWhenNoPosition() {
TaskHeartbeatRequest request = new TaskHeartbeatRequest();
@@ -18,6 +18,7 @@ import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.LinkedHashMap;
import java.util.Map;
import static org.junit.jupiter.api.Assertions.assertEquals;
@@ -49,8 +50,9 @@ class TaskProgressLightAssemblerTest {
private final FileTaskMapper fileTaskMapper = mock(FileTaskMapper.class);
private final FileResultMapper fileResultMapper = mock(FileResultMapper.class);
private final TaskFileJobService taskFileJobService = mock(TaskFileJobService.class);
private final TaskHeartbeatPositionService positionService = mock(TaskHeartbeatPositionService.class);
private final TaskProgressLightAssembler assembler =
new TaskProgressLightAssembler(fileTaskMapper, fileResultMapper, taskFileJobService);
new TaskProgressLightAssembler(fileTaskMapper, fileResultMapper, taskFileJobService, positionService);
private static FileTaskEntity task(long id, String moduleType, String status, LocalDateTime updatedAt) {
FileTaskEntity t = new FileTaskEntity();
@@ -78,6 +80,91 @@ class TaskProgressLightAssemblerTest {
when(taskFileJobService.findAssembleJobsByTaskIds(any(), anyList())).thenReturn(jobs);
}
@Test
void runningTaskCarriesHeartbeatProgress() {
stubTasks(List.of(task(1L, "publish", "RUNNING", LocalDateTime.of(2026, 8, 1, 10, 1))));
stubJobs(Map.of());
when(positionService.readDrawablePositions(anyList()))
.thenReturn(Map.of(1L, new TaskHeartbeatPositionService.Position(12, 50, "shop US(2/5)")));
TaskProgressLightVo item = assembler.assemble("publish", null, List.of(1L)).getItems().get(0);
assertEquals(12, item.getProgressCurrent());
assertEquals(50, item.getProgressTotal());
assertEquals("shop US(2/5)", item.getProgressLabel());
}
/**
* 终态任务一律不读心跳位置:位置 Hash 只有 24h TTL,隔天就读不到了;而且跑完的任务
* 该按状态显示「已完成」,不该显示某个中途百分比。
*/
@Test
void terminalTaskMustNotReadHeartbeatPosition() {
stubTasks(List.of(
task(1L, "publish", "SUCCESS", LocalDateTime.of(2026, 8, 1, 10, 1)),
task(2L, "publish", "FAILED", LocalDateTime.of(2026, 8, 1, 10, 2)),
task(3L, "publish", "CANCELLED", LocalDateTime.of(2026, 8, 1, 10, 3)),
task(4L, "publish", "RUNNING", LocalDateTime.of(2026, 8, 1, 10, 4))));
stubJobs(Map.of());
TaskProgressLightBatchVo vo = assembler.assemble("publish", null, List.of(1L, 2L, 3L, 4L));
assertEquals(4, vo.getItems().size());
for (TaskProgressLightVo item : vo.getItems()) {
assertNull(item.getProgressTotal());
assertNull(item.getProgressCurrent());
}
@SuppressWarnings("unchecked")
ArgumentCaptor<List<Long>> queried = forClass(List.class);
verify(positionService).readDrawablePositions(queried.capture());
assertEquals(List.of(4L), queried.getValue(), "位置读取列表只应含非终态任务");
}
/**
* 终态条目即便 Redis 里还留着位置也必须不给进度。
*
* <p>位置 Hash 有 24h TTL,任务跑完当天仍能读到最后一次心跳的位置;照它画会把一个
* 已完成的任务显示成「87%」,而正确读法是状态徽标+100%。
*/
@Test
void terminalItemsCarryNoProgressEvenWhenPositionStillExists() {
stubTasks(List.of(
task(1L, "publish", "SUCCESS", LocalDateTime.of(2026, 8, 1, 10, 1)),
task(2L, "publish", "RUNNING", LocalDateTime.of(2026, 8, 1, 10, 2))));
stubJobs(Map.of());
when(positionService.readDrawablePositions(anyList())).thenAnswer(invocation -> {
List<Long> ids = invocation.getArgument(0);
Map<Long, TaskHeartbeatPositionService.Position> map = new LinkedHashMap<>();
for (Long id : ids) {
map.put(id, new TaskHeartbeatPositionService.Position(7, 8, "at 7 of 8"));
}
return map;
});
List<TaskProgressLightVo> items = assembler.assemble("publish", null, List.of(1L, 2L)).getItems();
assertNull(items.get(0).getProgressTotal(), "SUCCESS 条目不得带进度");
assertNull(items.get(0).getProgressCurrent());
assertEquals(8, items.get(1).getProgressTotal(), "RUNNING 条目带进度");
assertEquals(7, items.get(1).getProgressCurrent());
}
/** 老客户端不发位置:三个字段必须一起留 null,前端据此不画进度轨(而不是画一条 0%)。 */
@Test
void absentHeartbeatPositionLeavesProgressNull() {
stubTasks(List.of(task(1L, "publish", "RUNNING", LocalDateTime.of(2026, 8, 1, 10, 1))));
stubJobs(Map.of());
when(positionService.readDrawablePositions(anyList())).thenReturn(Map.of());
TaskProgressLightVo item = assembler.assemble("publish", null, List.of(1L)).getItems().get(0);
assertNull(item.getProgressCurrent());
assertNull(item.getProgressTotal());
assertNull(item.getProgressLabel());
}
/**
/** 分子超过分母(客户端计数与分母不同源时会发生)要钳住,不能给出 114% 的进度条。 */
@Test
void statusMappingPreserved() {
stubTasks(List.of(
@@ -36,8 +36,9 @@ class TaskProgressLightPermissionTest {
private final FileTaskMapper fileTaskMapper = mock(FileTaskMapper.class);
private final FileResultMapper fileResultMapper = mock(FileResultMapper.class);
private final TaskFileJobService taskFileJobService = mock(TaskFileJobService.class);
private final TaskHeartbeatPositionService positionService = mock(TaskHeartbeatPositionService.class);
private final TaskProgressLightAssembler assembler =
new TaskProgressLightAssembler(fileTaskMapper, fileResultMapper, taskFileJobService);
new TaskProgressLightAssembler(fileTaskMapper, fileResultMapper, taskFileJobService, positionService);
private final List<FileTaskEntity> taskDb = new ArrayList<>();