feat(任务判死): 心跳正常但 180 分钟无结果上报的二次判死线(13 模块)+ 同期待发改动
判死线(治 28131 型「主线程卡死、心跳线程照发」): - 判据改看 biz_task_scope_state.last_chunk_at(HTTP 心跳不刷新它);从未上报跳过不判 - 中央线覆盖 DELETE_BRAND/PRODUCT_RISK_RESOLVE/PRICE_TRACK/SHOP_MATCH/PATROL_DELETE/QUERY_ASIN/WITHDRAW - 自带线接入 COLLECT_DATA/SIMILAR_ASIN/APPEARANCE_PATENT/SHOP_DATA_CRAWL/PUBLISH/BRAND - 客户端心跳带处理位置 progressText,判死文案含最后位置;no-result-upload-timeout-minutes 默认 180(0 关闭) 同期带上另一工作流的待发改动:跟价换 IP 重试、品牌检测重试上限与 LLM 并发下调、 教程包后台管理页与 V126 迁移、admin-vue 教程记录页。
This commit is contained in:
+4
@@ -3,6 +3,7 @@ package com.nanri.aiimage.modules.task.controller;
|
||||
import com.nanri.aiimage.common.api.ApiResponse;
|
||||
import com.nanri.aiimage.modules.task.model.dto.TaskHeartbeatRequest;
|
||||
import com.nanri.aiimage.modules.task.model.vo.TaskHeartbeatVo;
|
||||
import com.nanri.aiimage.modules.task.service.TaskHeartbeatPositionService;
|
||||
import com.nanri.aiimage.modules.task.service.TaskHeartbeatService;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.Parameter;
|
||||
@@ -22,6 +23,7 @@ import org.springframework.web.bind.annotation.RestController;
|
||||
public class TaskHeartbeatController {
|
||||
|
||||
private final TaskHeartbeatService taskHeartbeatService;
|
||||
private final TaskHeartbeatPositionService taskHeartbeatPositionService;
|
||||
|
||||
@PostMapping("/{taskId}/heartbeat")
|
||||
@Operation(
|
||||
@@ -31,6 +33,8 @@ public class TaskHeartbeatController {
|
||||
@Parameter(description = "任务 ID", required = true, example = "200")
|
||||
@PathVariable Long taskId,
|
||||
@Valid @RequestBody(required = false) TaskHeartbeatRequest request) {
|
||||
// 先记录「当前处理位置」(仅用于判死文案与排查,内部异常降级,不影响心跳本身)
|
||||
taskHeartbeatPositionService.record(taskId, request);
|
||||
return ApiResponse.success(taskHeartbeatService.heartbeat(taskId, request));
|
||||
}
|
||||
|
||||
|
||||
+25
@@ -1,9 +1,34 @@
|
||||
package com.nanri.aiimage.modules.task.mapper;
|
||||
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import com.nanri.aiimage.modules.task.model.dto.TaskScopeLastChunkDto;
|
||||
import com.nanri.aiimage.modules.task.model.entity.TaskScopeStateEntity;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
import org.apache.ibatis.annotations.Select;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Mapper
|
||||
public interface TaskScopeStateMapper extends BaseMapper<TaskScopeStateEntity> {
|
||||
|
||||
/**
|
||||
* 各任务「最后一次结果分片上报时间」(二次判死线专用)。
|
||||
*
|
||||
* <p>last_chunk_at 只随结果分片上报刷新(TaskScopePayloadStorageService.saveScopePayload),
|
||||
* 不随 HTTP 心跳刷新——是区分「客户端进程活着」与「任务有进展」的可靠信号。
|
||||
* HAVING 过滤掉全 NULL 的组:从未上报的任务不出现在结果里,调用方据此跳过(不判死)。
|
||||
* GROUP BY 走 uk_task_scope 的 task_id 前缀。
|
||||
*/
|
||||
@Select("""
|
||||
<script>
|
||||
SELECT task_id AS taskId, MAX(last_chunk_at) AS lastChunkAt
|
||||
FROM biz_task_scope_state
|
||||
WHERE task_id IN
|
||||
<foreach item="taskId" collection="taskIds" open="(" separator="," close=")">#{taskId}</foreach>
|
||||
GROUP BY task_id
|
||||
HAVING MAX(last_chunk_at) IS NOT NULL
|
||||
</script>
|
||||
""")
|
||||
List<TaskScopeLastChunkDto> selectLastChunkAtByTaskIds(@Param("taskIds") List<Long> taskIds);
|
||||
}
|
||||
|
||||
+4
@@ -38,4 +38,8 @@ public class TaskHeartbeatRequest {
|
||||
|
||||
@Schema(description = "详情页 ASIN 总数", example = "120")
|
||||
private Integer detailTotalAsins;
|
||||
|
||||
@Schema(description = "客户端归一化后的当前处理位置描述(如「店铺 张三(2/10),国家 德国(1/3)」),"
|
||||
+ "用于判死日志与排查;老客户端不传,服务端不得以位置为空作为判死依据", example = "店铺 魏振峰(2/10)")
|
||||
private String progressText;
|
||||
}
|
||||
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
package com.nanri.aiimage.modules.task.model.dto;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
/**
|
||||
* 任务「最后一次结果分片上报时间」(「心跳正常但无结果上报」二次判死线用)。
|
||||
*
|
||||
* <p>lastChunkAt 取 biz_task_scope_state 的 MAX(last_chunk_at)——该列只随结果分片上报刷新
|
||||
* (TaskScopePayloadStorageService.saveScopePayload),不随 HTTP 心跳刷新。
|
||||
*/
|
||||
public record TaskScopeLastChunkDto(Long taskId, LocalDateTime lastChunkAt) {
|
||||
}
|
||||
+98
@@ -0,0 +1,98 @@
|
||||
package com.nanri.aiimage.modules.task.service;
|
||||
|
||||
import com.nanri.aiimage.modules.task.model.dto.TaskHeartbeatRequest;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.time.Instant;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 任务心跳的「当前处理位置」存储(Redis Hash,24h TTL)。
|
||||
*
|
||||
* <p>客户端主线程(浏览器自动化)卡死时心跳线程照发,仅凭心跳无法区分
|
||||
* 「进程活着」与「任务有进展」。位置文本用于判死日志 / errorMessage 说明卡在哪,
|
||||
* 以及后台人工排查。位置缺失只退化文案,<b>不得作为任何判死依据</b>。
|
||||
* 老客户端不传 progressText、位置全空时不写;读取失败返回 null。
|
||||
*/
|
||||
@Slf4j
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class TaskHeartbeatPositionService {
|
||||
|
||||
private static final String KEY_PREFIX = "task:heartbeat:position:";
|
||||
private static final Duration POSITION_TTL = Duration.ofHours(24);
|
||||
|
||||
private final StringRedisTemplate stringRedisTemplate;
|
||||
|
||||
public void record(Long taskId, TaskHeartbeatRequest request) {
|
||||
if (taskId == null || taskId <= 0 || request == null) {
|
||||
return;
|
||||
}
|
||||
String progressText = request.getProgressText() == null ? "" : request.getProgressText().trim();
|
||||
boolean hasPosition = !progressText.isEmpty()
|
||||
|| request.getCurrent() != null
|
||||
|| request.getTotal() != null
|
||||
|| (request.getPhase() != null && !request.getPhase().isBlank());
|
||||
if (!hasPosition) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
Map<String, String> values = new HashMap<>();
|
||||
values.put("phase", request.getPhase() == null ? "" : request.getPhase());
|
||||
values.put("current", request.getCurrent() == null ? "" : String.valueOf(request.getCurrent()));
|
||||
values.put("total", request.getTotal() == null ? "" : String.valueOf(request.getTotal()));
|
||||
values.put("progressText", progressText);
|
||||
values.put("at", String.valueOf(Instant.now().toEpochMilli()));
|
||||
String key = KEY_PREFIX + taskId;
|
||||
stringRedisTemplate.opsForHash().putAll(key, values);
|
||||
stringRedisTemplate.expire(key, POSITION_TTL);
|
||||
} catch (Exception ex) {
|
||||
log.warn("[heartbeat-position] 记录位置失败 taskId={} msg={}", taskId, ex.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/** 读取「最后处理位置」描述;无位置信息返回 null(调用方自行降级)。 */
|
||||
public String describe(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;
|
||||
}
|
||||
String text = value(entries, "progressText");
|
||||
if (!text.isEmpty()) {
|
||||
return text;
|
||||
}
|
||||
String phase = value(entries, "phase");
|
||||
String current = value(entries, "current");
|
||||
String total = value(entries, "total");
|
||||
StringBuilder sb = new StringBuilder();
|
||||
if (!phase.isEmpty()) {
|
||||
sb.append("阶段=").append(phase);
|
||||
}
|
||||
if (!current.isEmpty() || !total.isEmpty()) {
|
||||
if (sb.length() > 0) {
|
||||
sb.append(",");
|
||||
}
|
||||
sb.append(current.isEmpty() ? "0" : current).append("/").append(total.isEmpty() ? "0" : total);
|
||||
}
|
||||
return sb.length() == 0 ? null : sb.toString();
|
||||
}
|
||||
|
||||
private String value(Map<Object, Object> entries, String key) {
|
||||
Object raw = entries.get(key);
|
||||
return raw == null ? "" : String.valueOf(raw).trim();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user