进度(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
@@ -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());
}
}