|
|
@@ -7,6 +7,8 @@ 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.model.InstanceManagementResponse;
|
|
|
+import cn.com.yusys.manager.model.InstanceConfigRequest;
|
|
|
import cn.com.yusys.manager.util.ParseInstanceClient;
|
|
|
import cn.com.yusys.manager.common.PortPool;
|
|
|
import lombok.RequiredArgsConstructor;
|
|
|
@@ -51,6 +53,10 @@ public class InstanceMonitorService {
|
|
|
@Resource
|
|
|
private PortPool portPool;
|
|
|
|
|
|
+ // 注入任务日志服务
|
|
|
+ @Resource
|
|
|
+ private TaskLogService taskLogService;
|
|
|
+
|
|
|
// 最大重试次数
|
|
|
@org.springframework.beans.factory.annotation.Value("${parser.task.max-retry:3}")
|
|
|
private int maxRetry;
|
|
|
@@ -399,19 +405,38 @@ public class InstanceMonitorService {
|
|
|
*/
|
|
|
public ExecuteResponse processMultimodalTask(Task task) {
|
|
|
Map<String, InstanceStatus> activeInstancePool = instancestatusRegistry.getActiveInstancePool();
|
|
|
+ String taskId = task.getTaskId();
|
|
|
+
|
|
|
try {
|
|
|
+ // 记录任务开始日志
|
|
|
+ taskLogService.logTaskStart(taskId, task.getFilePath());
|
|
|
+
|
|
|
// 检查是否有空闲的解析实例
|
|
|
InstanceStatus idleInstance = findIdleInstance(activeInstancePool);
|
|
|
if (idleInstance == null) {
|
|
|
log.debug("当前无空闲解析实例");
|
|
|
+ taskLogService.logTaskFailure(taskId, "当前无空闲解析实例");
|
|
|
return ExecuteResponse.fail(300,"当前无空闲解析实例");
|
|
|
}
|
|
|
+
|
|
|
+ // 记录实例分配日志
|
|
|
+ taskLogService.logInstanceAllocation(taskId, idleInstance.getContainerId());
|
|
|
|
|
|
// 执行任务解析
|
|
|
- return executeTaskWithRetry(idleInstance, task.getFilePath());
|
|
|
+ ExecuteResponse response = executeTaskWithRetry(idleInstance, task);
|
|
|
+
|
|
|
+ // 记录任务完成日志
|
|
|
+ if (response != null && response.getCode() == 200) {
|
|
|
+ taskLogService.logTaskComplete(taskId, response.getMessage());
|
|
|
+ } else {
|
|
|
+ taskLogService.logTaskFailure(taskId, response != null ? response.getMessage() : "未知错误");
|
|
|
+ }
|
|
|
+
|
|
|
+ return response;
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
log.error("多模态任务解析定时任务执行失败", e);
|
|
|
+ taskLogService.logTaskFailure(taskId, e.getMessage());
|
|
|
return ExecuteResponse.fail("多模态任务解析定时任务执行失败");
|
|
|
}
|
|
|
}
|
|
|
@@ -430,7 +455,7 @@ public class InstanceMonitorService {
|
|
|
/**
|
|
|
* 执行任务并处理重试逻辑
|
|
|
*/
|
|
|
- private ExecuteResponse executeTaskWithRetry(InstanceStatus instance, String taskMessage) {
|
|
|
+ private ExecuteResponse executeTaskWithRetry(InstanceStatus instance, Task task) {
|
|
|
String instanceId = instance.getContainerId();
|
|
|
int retryCount = 0;
|
|
|
// 标记实例为运行中
|
|
|
@@ -440,10 +465,10 @@ public class InstanceMonitorService {
|
|
|
while (retryCount <= maxRetry ) {
|
|
|
try {
|
|
|
log.info("开始执行任务,实例:{},重试次数:{}/{},任务内容:{}",
|
|
|
- instanceId, retryCount, maxRetry, taskMessage);
|
|
|
+ instanceId, retryCount, maxRetry, task.getFilePath());
|
|
|
|
|
|
// 调用解析器执行任务
|
|
|
- ExecuteResponse response = callParser(instance, taskMessage);
|
|
|
+ ExecuteResponse response = callParser(instance, task);
|
|
|
|
|
|
if (response != null && response.getCode() == 200) {
|
|
|
log.info("任务执行成功,实例:{},响应:{}", instanceId, response);
|
|
|
@@ -469,12 +494,12 @@ public class InstanceMonitorService {
|
|
|
/**
|
|
|
* 调用解析器执行任务
|
|
|
*/
|
|
|
- private ExecuteResponse callParser(InstanceStatus instance, String taskMessage) {
|
|
|
+ private ExecuteResponse callParser(InstanceStatus instance, Task task) {
|
|
|
try {
|
|
|
ExecuteResponse response = instanceClient.executeTask(
|
|
|
instance.getIp(),
|
|
|
instance.getPort(),
|
|
|
- taskMessage);
|
|
|
+ task);
|
|
|
|
|
|
if (response == null || response.getCode() != 200) {
|
|
|
log.warn("调用解析器返回失败,实例:{},响应:{}", instance.getContainerId(), response);
|
|
|
@@ -486,4 +511,84 @@ public class InstanceMonitorService {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 获取实例管理信息
|
|
|
+ * @return 实例管理信息
|
|
|
+ */
|
|
|
+ public InstanceManagementResponse getInstanceManagementInfo() {
|
|
|
+ try {
|
|
|
+ Map<String, InstanceStatus> activeInstancePool = instancestatusRegistry.getActiveInstancePool();
|
|
|
+
|
|
|
+ // 统计各状态实例数量
|
|
|
+ long idleCount = activeInstancePool.values().stream()
|
|
|
+ .filter(status -> status.getStatus() == 0)
|
|
|
+ .count();
|
|
|
+ long runningCount = activeInstancePool.values().stream()
|
|
|
+ .filter(status -> status.getStatus() == 1)
|
|
|
+ .count();
|
|
|
+ long errorCount = activeInstancePool.values().stream()
|
|
|
+ .filter(status -> status.getStatus() == 2)
|
|
|
+ .count();
|
|
|
+
|
|
|
+ // 构建实例详情列表
|
|
|
+ List<InstanceManagementResponse.InstanceDetail> instanceDetails = activeInstancePool.values().stream()
|
|
|
+ .map(status -> InstanceManagementResponse.InstanceDetail.builder()
|
|
|
+ .containerId(status.getContainerId())
|
|
|
+ .ip(status.getIp())
|
|
|
+ .port(status.getPort())
|
|
|
+ .status(status.getStatus())
|
|
|
+ .cpuUsage(status.getCpuUsage())
|
|
|
+ .memoryUsage(status.getMemoryUsage())
|
|
|
+ .gpuUsage(status.getGpuUsage())
|
|
|
+ .gpuMemory(status.getGpuMemory())
|
|
|
+ .lastHeartbeatTime(status.getLastHeartbeatTime())
|
|
|
+ .build())
|
|
|
+ .collect(Collectors.toList());
|
|
|
+
|
|
|
+ // 构建响应数据
|
|
|
+ InstanceManagementResponse.InstanceManagementData data = InstanceManagementResponse.InstanceManagementData.builder()
|
|
|
+ .totalInstances(activeInstancePool.size())
|
|
|
+ .idleInstances((int) idleCount)
|
|
|
+ .runningInstances((int) runningCount)
|
|
|
+ .errorInstances((int) errorCount)
|
|
|
+ .instanceDetails(instanceDetails)
|
|
|
+ .build();
|
|
|
+
|
|
|
+ return InstanceManagementResponse.success(data);
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("获取实例管理信息失败", e);
|
|
|
+ return InstanceManagementResponse.fail("获取实例管理信息失败: " + e.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 更新实例配置
|
|
|
+ * @param request 实例配置请求
|
|
|
+ * @return 操作结果
|
|
|
+ */
|
|
|
+ public InstanceManagementResponse updateInstanceConfig(InstanceConfigRequest request) {
|
|
|
+ try {
|
|
|
+ int minActiveInstance = request.getMinActiveInstance();
|
|
|
+ int maxActiveInstance = request.getMaxActiveInstance();
|
|
|
+
|
|
|
+ // 校验最小实例数不能大于最大实例数
|
|
|
+ if (minActiveInstance > maxActiveInstance) {
|
|
|
+ return InstanceManagementResponse.fail("最小活跃实例数不能大于最大活跃实例数");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 更新配置
|
|
|
+ parserConfig.MIN_ACTIVE_INSTANCE = minActiveInstance;
|
|
|
+ parserConfig.MAX_ACTIVE_INSTANCE = maxActiveInstance;
|
|
|
+
|
|
|
+ log.info("实例配置已更新:最小活跃实例数={}, 最大活跃实例数={}",
|
|
|
+ minActiveInstance, maxActiveInstance);
|
|
|
+
|
|
|
+ // 获取更新后的实例管理信息
|
|
|
+ return getInstanceManagementInfo();
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("更新实例配置失败", e);
|
|
|
+ return InstanceManagementResponse.fail("更新实例配置失败: " + e.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
}
|