|
|
@@ -0,0 +1,101 @@
|
|
|
+package com.zsjz.ai.module.dm.service;
|
|
|
+
|
|
|
+import jakarta.annotation.PreDestroy;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+
|
|
|
+import java.util.concurrent.ScheduledFuture;
|
|
|
+import java.util.concurrent.ScheduledThreadPoolExecutor;
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 上传文件「服务端解析/提取」阶段的心跳日志。
|
|
|
+ *
|
|
|
+ * <p>为什么需要:上传接口是同步的 —— 浏览器把字节发完(前端进度 100%)后,
|
|
|
+ * 服务端还要做落盘、MD5 去重、表格解析(大 Excel 可能几十秒)、OCR/转写/文档样本抽取。
|
|
|
+ * 这段时间前端没有新进度、也没有 SSE 事件,排障时容易误判为「连接断了/请求卡死」。
|
|
|
+ *
|
|
|
+ * <p>本组件在提取期间按 {@code dm.upload.heartbeat-seconds}(默认 5 秒)打一条 INFO 日志:
|
|
|
+ *
|
|
|
+ * <pre>
|
|
|
+ * [upload-heartbeat] 提取进行中: name=xxx.xlsx, batchId=123, elapsed=15s(请求连接正常)
|
|
|
+ * </pre>
|
|
|
+ *
|
|
|
+ * 提取结束(无论成功/失败)在 {@link Handle#close()} 里打一条带耗时的收尾日志。
|
|
|
+ * 心跳只在超过一个周期后才出现,普通小文件不会多出心跳噪音。
|
|
|
+ */
|
|
|
+@Slf4j
|
|
|
+@Component
|
|
|
+public class ExtractionHeartbeat {
|
|
|
+
|
|
|
+ /** 心跳间隔(秒),可由 {@code dm.upload.heartbeat-seconds} 覆盖 */
|
|
|
+ private final long heartbeatSeconds;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 单线程调度器即可:任务只是打日志,多个上传并发时各任务按间隔排队执行。
|
|
|
+ * 守护线程,不阻止 JVM 退出。
|
|
|
+ */
|
|
|
+ private final ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(1, runnable -> {
|
|
|
+ Thread thread = new Thread(runnable, "upload-extraction-heartbeat");
|
|
|
+ thread.setDaemon(true);
|
|
|
+ return thread;
|
|
|
+ });
|
|
|
+
|
|
|
+ public ExtractionHeartbeat(@Value("${dm.upload.heartbeat-seconds:5}") long heartbeatSeconds) {
|
|
|
+ this.heartbeatSeconds = Math.max(1, heartbeatSeconds);
|
|
|
+ executor.setRemoveOnCancelPolicy(true);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 开始记录一次提取;用 try-with-resources 包住同步解析代码。
|
|
|
+ *
|
|
|
+ * @param fileName 源文件名(日志定位)
|
|
|
+ * @param batchId 上传批次
|
|
|
+ */
|
|
|
+ public Handle start(String fileName, Long batchId) {
|
|
|
+ long startedAt = System.currentTimeMillis();
|
|
|
+ log.info("[upload] 开始提取: name={}, batchId={}(上传已完成,服务端解析中)", fileName, batchId);
|
|
|
+ ScheduledFuture<?> future = executor.scheduleAtFixedRate(() -> {
|
|
|
+ long elapsedSeconds = (System.currentTimeMillis() - startedAt) / 1000;
|
|
|
+ log.info("[upload-heartbeat] 提取进行中: name={}, batchId={}, elapsed={}s(请求连接正常)",
|
|
|
+ fileName, batchId, elapsedSeconds);
|
|
|
+ }, heartbeatSeconds, heartbeatSeconds, TimeUnit.SECONDS);
|
|
|
+ return new Handle(fileName, batchId, startedAt, future);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 一次提取的句柄:close 时停止心跳并记录总耗时。
|
|
|
+ */
|
|
|
+ public static final class Handle implements AutoCloseable {
|
|
|
+
|
|
|
+ private final String fileName;
|
|
|
+ private final Long batchId;
|
|
|
+ private final long startedAt;
|
|
|
+ private final ScheduledFuture<?> future;
|
|
|
+ private boolean closed;
|
|
|
+
|
|
|
+ private Handle(String fileName, Long batchId, long startedAt, ScheduledFuture<?> future) {
|
|
|
+ this.fileName = fileName;
|
|
|
+ this.batchId = batchId;
|
|
|
+ this.startedAt = startedAt;
|
|
|
+ this.future = future;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void close() {
|
|
|
+ if (closed) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ closed = true;
|
|
|
+ future.cancel(false);
|
|
|
+ long costMs = System.currentTimeMillis() - startedAt;
|
|
|
+ log.info("[upload] 提取结束: name={}, batchId={}, costMs={}", fileName, batchId, costMs);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @PreDestroy
|
|
|
+ public void shutdown() {
|
|
|
+ executor.shutdownNow();
|
|
|
+ }
|
|
|
+}
|