From 63189f3ca03f4eed806b3a0cfee30671a73df0fb Mon Sep 17 00:00:00 2001 From: AgAngle <1323481023@qq.com> Date: Mon, 25 Nov 2024 14:50:30 +0800 Subject: [PATCH] =?UTF-8?q?fix(=E6=8E=A5=E5=8F=A3=E6=B5=8B=E8=AF=95):=20?= =?UTF-8?q?=E8=B5=84=E6=BA=90=E6=B1=A0=E7=A6=81=E7=94=A8=EF=BC=8C=E4=BB=BB?= =?UTF-8?q?=E5=8A=A1=E9=A1=B9=E6=B2=A1=E6=9C=89=E9=94=99=E8=AF=AF=E6=8F=90?= =?UTF-8?q?=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --bug=1049106 --user=陈建星 【任务中心】-禁用测试计划使用的资源池后,执行测试计划,执行完成后,任务详情的排队状态没有异常提示 https://www.tapd.cn/55049933/s/1616375 --- .../api/service/ApiCommonService.java | 11 ++++ .../api/service/ApiExecuteService.java | 64 +++++++++++-------- 2 files changed, 49 insertions(+), 26 deletions(-) diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/service/ApiCommonService.java b/backend/services/api-test/src/main/java/io/metersphere/api/service/ApiCommonService.java index dde4a478cf..e9331c2a88 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/service/ApiCommonService.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/service/ApiCommonService.java @@ -35,6 +35,7 @@ import io.metersphere.sdk.constants.TaskItemErrorMessage; import io.metersphere.sdk.dto.api.task.GetRunScriptRequest; import io.metersphere.sdk.dto.api.task.TaskBatchRequestDTO; import io.metersphere.sdk.dto.api.task.TaskItem; +import io.metersphere.sdk.dto.api.task.TaskRequestDTO; import io.metersphere.sdk.exception.MSException; import io.metersphere.sdk.util.BeanUtils; import io.metersphere.sdk.util.CommonBeanFactory; @@ -552,6 +553,16 @@ public class ApiCommonService { } } + @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRES_NEW) + public void batchUpdateTaskItemErrorMassage(TaskItemErrorMessage errorMessage, TaskRequestDTO taskRequestDTO) { + // 更新任务项的异常信息 + ExecTaskItem execTaskItem = new ExecTaskItem(); + execTaskItem.setId(taskRequestDTO.getTaskItem().getId()); + execTaskItem.setErrorMessage(errorMessage.name()); + execTaskItemMapper.updateByPrimaryKeySelective(execTaskItem); + } + + @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRES_NEW) public void updateTaskItemErrorMassage(String taskItemId, TaskItemErrorMessage errorMessage) { // 更新任务项的异常信息 diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/service/ApiExecuteService.java b/backend/services/api-test/src/main/java/io/metersphere/api/service/ApiExecuteService.java index 390d5f584e..18fc78db7a 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/service/ApiExecuteService.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/service/ApiExecuteService.java @@ -32,6 +32,7 @@ import io.metersphere.project.dto.environment.http.SelectModule; import io.metersphere.project.service.*; import io.metersphere.sdk.constants.*; import io.metersphere.sdk.dto.api.task.*; +import io.metersphere.sdk.exception.IResultCode; import io.metersphere.sdk.exception.MSException; import io.metersphere.sdk.exception.TaskRunnerResultCode; import io.metersphere.sdk.file.FileCenter; @@ -264,6 +265,10 @@ public class ApiExecuteService { throw new MSException(RESOURCE_POOL_EXECUTE_ERROR, e.getMessage()); } catch (MSException e) { LogUtils.error(e); + IResultCode errorCode = e.getErrorCode(); + if (errorCode == ApiResultCode.EXECUTE_RESOURCE_POOL_NOT_CONFIG) { + apiCommonService.batchUpdateTaskItemErrorMassage(TaskItemErrorMessage.INVALID_RESOURCE_POOL, taskRequest); + } throw e; } catch (Exception e) { LogUtils.error(e); @@ -373,29 +378,41 @@ public class ApiExecuteService { */ public void batchExecute(TaskBatchRequestDTO taskRequest) { setTaskRequestParams(taskRequest.getTaskInfo()); - TaskInfo taskInfo = taskRequest.getTaskInfo(); - // 获取资源池 - TestResourcePoolReturnDTO testResourcePool = getGetResourcePoolNodeDTO(taskInfo.getRunModeConfig(), taskInfo.getProjectId()); - if (StringUtils.isNotBlank(testResourcePool.getServerUrl())) { - // 如果资源池配置了当前站点,则使用资源池的 - taskInfo.setMsUrl(testResourcePool.getServerUrl()); - } - taskInfo.setPoolId(testResourcePool.getId()); - taskRequest.getTaskItems().forEach(taskItem -> { - if (StringUtils.isBlank(taskItem.getReportId())) { - // 预先生成报告ID,避免资源池获取执行脚本时,超时重试,导致数据重复创建 - taskItem.setReportId(IDGenerator.nextStr()); + try { + // 获取资源池 + TestResourcePoolReturnDTO testResourcePool = getGetResourcePoolNodeDTO(taskInfo.getRunModeConfig(), taskInfo.getProjectId()); + + if (StringUtils.isNotBlank(testResourcePool.getServerUrl())) { + // 如果资源池配置了当前站点,则使用资源池的 + taskInfo.setMsUrl(testResourcePool.getServerUrl()); } - }); + taskInfo.setPoolId(testResourcePool.getId()); + taskRequest.getTaskItems().forEach(taskItem -> { + if (StringUtils.isBlank(taskItem.getReportId())) { + // 预先生成报告ID,避免资源池获取执行脚本时,超时重试,导致数据重复创建 + taskItem.setReportId(IDGenerator.nextStr()); + } + }); - // 判断是否为 K8S 资源池 - boolean isK8SResourcePool = StringUtils.equals(testResourcePool.getType(), ResourcePoolTypeEnum.K8S.getName()); - if (isK8SResourcePool) { - k8sBatchExecute(taskRequest, taskInfo, testResourcePool); - } else { - nodeBatchExecute(taskRequest, taskInfo, testResourcePool); + // 判断是否为 K8S 资源池 + boolean isK8SResourcePool = StringUtils.equals(testResourcePool.getType(), ResourcePoolTypeEnum.K8S.getName()); + if (isK8SResourcePool) { + k8sBatchExecute(taskRequest, taskInfo, testResourcePool); + } else { + nodeBatchExecute(taskRequest, taskInfo, testResourcePool); + } + } catch (MSException e) { + LogUtils.error(e); + IResultCode errorCode = e.getErrorCode(); + if (errorCode == ApiResultCode.EXECUTE_RESOURCE_POOL_NOT_CONFIG) { + apiCommonService.batchUpdateTaskItemErrorMassage(TaskItemErrorMessage.INVALID_RESOURCE_POOL, taskRequest); + } + throw e; + } catch (Exception e) { + LogUtils.error(e); + throw new MSException(RESOURCE_POOL_EXECUTE_ERROR, e.getMessage()); } } @@ -463,19 +480,14 @@ public class ApiExecuteService { } } - private void k8sBatchExecute(TaskBatchRequestDTO taskRequest, TaskInfo taskInfo, TestResourcePoolReturnDTO testResourcePool) { + private void k8sBatchExecute(TaskBatchRequestDTO taskRequest, TaskInfo taskInfo, TestResourcePoolReturnDTO testResourcePool) throws Exception { TestResourceDTO testResourceDTO = new TestResourceDTO(); BeanUtils.copyBean(testResourceDTO, testResourcePool.getTestResourceReturnDTO()); testResourceDTO.setId(testResourcePool.getId()); taskInfo.setPoolSize(testResourceDTO.getConcurrentNumber()); taskInfo.setPerTaskSize(testResourceDTO.getPodThreads()); - try { - EngineFactory.batchRunApi(taskRequest, testResourceDTO); - } catch (Exception e) { - LogUtils.error(e); - apiCommonService.batchUpdateTaskItemErrorMassage(TaskItemErrorMessage.INVALID_RESOURCE_POOL, taskRequest); - } + EngineFactory.batchRunApi(taskRequest, testResourceDTO); } private List getDistributeTaskBatchRequest(TaskBatchRequestDTO taskRequest, int distributeSize) {