|
|
@@ -2,11 +2,13 @@ package cn.com.yusys.manager.service;
|
|
|
|
|
|
import cn.com.yusys.manager.common.ParseInstanceStatusRegistry;
|
|
|
import cn.com.yusys.manager.config.ParserConfig;
|
|
|
+import cn.com.yusys.manager.model.ExecuteResponse;
|
|
|
import cn.com.yusys.manager.model.InstanceStatus;
|
|
|
import cn.com.yusys.manager.model.InstanceStatusResponse;
|
|
|
import cn.com.yusys.manager.instanceManager.Impl.DockerInstanceManager;
|
|
|
+import cn.com.yusys.manager.model.Task;
|
|
|
import cn.com.yusys.manager.util.ParseInstanceClient;
|
|
|
-import cn.com.yusys.manager.common.PortPool;
|
|
|
+import cn.com.yusys.manager.common.PortPool;
|
|
|
import lombok.RequiredArgsConstructor;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.scheduling.annotation.Scheduled;
|
|
|
@@ -18,7 +20,6 @@ import java.util.Comparator;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
-import java.util.concurrent.ConcurrentHashMap;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
/**
|
|
|
@@ -50,6 +51,10 @@ public class InstanceMonitorService {
|
|
|
@Resource
|
|
|
private PortPool portPool;
|
|
|
|
|
|
+ // 最大重试次数
|
|
|
+ @org.springframework.beans.factory.annotation.Value("${parser.task.max-retry:3}")
|
|
|
+ private int maxRetry;
|
|
|
+
|
|
|
@PostConstruct
|
|
|
public void initParseInstance(){
|
|
|
log.info("开始初始化解析实例...");
|
|
|
@@ -77,9 +82,9 @@ public class InstanceMonitorService {
|
|
|
/**
|
|
|
* 核心监控定时任务:
|
|
|
* - initialDelay = 30000:首次执行延迟30秒
|
|
|
- * - fixedRate = 5000:之后每5秒执行一次
|
|
|
+ * - fixedRate = 10000:之后每10秒执行一次
|
|
|
*/
|
|
|
- @Scheduled(initialDelay = 30000, fixedRate = 5000)
|
|
|
+ @Scheduled(initialDelay = 30000, fixedRate = 10000)
|
|
|
public void parserInstanceMonitor() {
|
|
|
Map<String, InstanceStatus> activeInstancePool = instancestatusRegistry.getActiveInstancePool();
|
|
|
try {
|
|
|
@@ -387,4 +392,98 @@ public class InstanceMonitorService {
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 执行多模态任务解析任务,阻塞调用底层解析器执行任务
|
|
|
+ * 失败任务重试maxRetry次后,转入失败Topic
|
|
|
+ */
|
|
|
+ public ExecuteResponse processMultimodalTask(Task task) {
|
|
|
+ Map<String, InstanceStatus> activeInstancePool = instancestatusRegistry.getActiveInstancePool();
|
|
|
+ try {
|
|
|
+ // 检查是否有空闲的解析实例
|
|
|
+ InstanceStatus idleInstance = findIdleInstance(activeInstancePool);
|
|
|
+ if (idleInstance == null) {
|
|
|
+ log.debug("当前无空闲解析实例");
|
|
|
+ return ExecuteResponse.fail(300,"当前无空闲解析实例");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 执行任务解析
|
|
|
+ return executeTaskWithRetry(idleInstance, task.getFilePath());
|
|
|
+
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("多模态任务解析定时任务执行失败", e);
|
|
|
+ return ExecuteResponse.fail("多模态任务解析定时任务执行失败");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 查找空闲的解析实例
|
|
|
+ */
|
|
|
+ private InstanceStatus findIdleInstance(Map<String, InstanceStatus> activeInstancePool) {
|
|
|
+ return activeInstancePool.values().stream()
|
|
|
+ .filter(status -> status.getStatus() == 0) // 状态为0表示空闲
|
|
|
+ .findFirst()
|
|
|
+ .orElse(null);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 执行任务并处理重试逻辑
|
|
|
+ */
|
|
|
+ private ExecuteResponse executeTaskWithRetry(InstanceStatus instance, String taskMessage) {
|
|
|
+ String instanceId = instance.getContainerId();
|
|
|
+ int retryCount = 0;
|
|
|
+ // 标记实例为运行中
|
|
|
+ instance.setStatus(1);
|
|
|
+
|
|
|
+ try {
|
|
|
+ while (retryCount <= maxRetry ) {
|
|
|
+ try {
|
|
|
+ log.info("开始执行任务,实例:{},重试次数:{}/{},任务内容:{}",
|
|
|
+ instanceId, retryCount, maxRetry, taskMessage);
|
|
|
+
|
|
|
+ // 调用解析器执行任务
|
|
|
+ ExecuteResponse response = callParser(instance, taskMessage);
|
|
|
+
|
|
|
+ if (response != null && response.getCode() == 200) {
|
|
|
+ log.info("任务执行成功,实例:{},响应:{}", instanceId, response);
|
|
|
+ return response;
|
|
|
+ } else {
|
|
|
+ log.warn("任务执行返回失败,实例:{},响应:{},准备重试", instanceId, response);
|
|
|
+ retryCount++;
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("任务执行异常,实例:{},重试次数:{}/{}", instanceId, retryCount, maxRetry, e);
|
|
|
+ retryCount++;
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ return ExecuteResponse.fail("任务执行失败,已达最大重试次数");
|
|
|
+ } finally {
|
|
|
+ // 恢复实例状态为空闲
|
|
|
+ instance.setStatus(0);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 调用解析器执行任务
|
|
|
+ */
|
|
|
+ private ExecuteResponse callParser(InstanceStatus instance, String taskMessage) {
|
|
|
+ try {
|
|
|
+ ExecuteResponse response = instanceClient.executeTask(
|
|
|
+ instance.getIp(),
|
|
|
+ instance.getPort(),
|
|
|
+ taskMessage);
|
|
|
+
|
|
|
+ if (response == null || response.getCode() != 200) {
|
|
|
+ log.warn("调用解析器返回失败,实例:{},响应:{}", instance.getContainerId(), response);
|
|
|
+ }
|
|
|
+ return response;
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("调用解析器失败,实例:{}", instance.getContainerId(), e);
|
|
|
+ throw e;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
}
|