From db96c49133708e54ed84dc6c0807e3cd949c141a Mon Sep 17 00:00:00 2001 From: AgAngle <1323481023@qq.com> Date: Fri, 25 Oct 2024 18:21:36 +0800 Subject: [PATCH] =?UTF-8?q?fix(=E6=8E=A5=E5=8F=A3=E6=B5=8B=E8=AF=95):=20?= =?UTF-8?q?=E4=BB=BB=E5=8A=A1=E5=81=9C=E6=AD=A2=EF=BC=8C=E8=B5=84=E6=BA=90?= =?UTF-8?q?=E6=B1=A0=E5=BC=82=E5=B8=B8=EF=BC=8C=E6=97=A0=E6=B3=95=E5=81=9C?= =?UTF-8?q?=E6=AD=A2=E6=88=90=E5=8A=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --bug=1047966 --user=陈建星 【任务中心】-执行测试计划,在任务中心停止正在执行的任务,停止失败,提示资源池不存在 https://www.tapd.cn/55049933/s/1598605 --- .../ApiTestCaseBatchRunService.java | 10 +-- .../scenario/ApiScenarioBatchRunService.java | 10 +-- .../scenario/ApiScenarioRunService.java | 5 +- .../system/service/BaseTaskHubService.java | 67 ++++++++++++------- 4 files changed, 49 insertions(+), 43 deletions(-) diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/service/definition/ApiTestCaseBatchRunService.java b/backend/services/api-test/src/main/java/io/metersphere/api/service/definition/ApiTestCaseBatchRunService.java index 51e2c66ba4..e3244e8553 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/service/definition/ApiTestCaseBatchRunService.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/service/definition/ApiTestCaseBatchRunService.java @@ -123,7 +123,7 @@ public class ApiTestCaseBatchRunService { // 分批查询 SubListUtils.dealForSubList(ids, TASK_BATCH_SIZE, subIds -> { - List apiTestCases = getOrderApiTestCases(subIds, runModeConfig); + List apiTestCases = getOrderApiTestCases(subIds); // 初始化任务项 List execTaskItems = initExecTaskItem(apiTestCases, userId, project, execTask); @@ -199,7 +199,7 @@ public class ApiTestCaseBatchRunService { // 分批查询 SubListUtils.dealForSubList(ids, TASK_BATCH_SIZE, subIds -> { - List apiTestCases = getOrderApiTestCases(subIds, runModeConfig); + List apiTestCases = getOrderApiTestCases(subIds); // 初始化任务项 Map resourceExecTaskItemMap = initExecTaskItem(apiTestCases, userId, project, execTask) .stream() @@ -239,7 +239,7 @@ public class ApiTestCaseBatchRunService { * @param ids * @return */ - private List getOrderApiTestCases(List ids, ApiRunModeConfigDTO runModeConfig) { + private List getOrderApiTestCases(List ids) { List apiTestCases = new ArrayList<>(TASK_BATCH_SIZE); // 分批查询 List finalApiTestCases = apiTestCases; @@ -253,10 +253,6 @@ public class ApiTestCaseBatchRunService { // 按照ID顺序排序 ApiTestCase apiTestCase = apiCaseMap.get(id); if (apiTestCase == null) { - if (runModeConfig.isIntegratedReport()) { - // 用例不存在,则在执行集合中删除 - apiExecutionSetService.removeItem(runModeConfig.getCollectionReport().getReportId(), id); - } LogUtils.info("当前执行任务的用例已删除 {}", id); break; } diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/service/scenario/ApiScenarioBatchRunService.java b/backend/services/api-test/src/main/java/io/metersphere/api/service/scenario/ApiScenarioBatchRunService.java index 87f0c517ce..a3b214cef0 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/service/scenario/ApiScenarioBatchRunService.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/service/scenario/ApiScenarioBatchRunService.java @@ -127,7 +127,7 @@ public class ApiScenarioBatchRunService { // 分批查询 SubListUtils.dealForSubList(ids, TASK_BATCH_SIZE, subIds -> { - List apiScenarios = getOrderScenarios(subIds, runModeConfig); + List apiScenarios = getOrderScenarios(subIds); // 初始化任务项 List execTaskItems = initExecTaskItem(subIds, apiScenarios, userId, project, execTask); @@ -171,7 +171,7 @@ public class ApiScenarioBatchRunService { // 分批查询 SubListUtils.dealForSubList(ids, TASK_BATCH_SIZE, subIds -> { - List apiScenarios = getOrderScenarios(subIds, runModeConfig); + List apiScenarios = getOrderScenarios(subIds); Map caseReportMap = null; // 初始化任务项 @@ -218,7 +218,7 @@ public class ApiScenarioBatchRunService { * @param ids * @return */ - private List getOrderScenarios(List ids, ApiRunModeConfigDTO runModeConfig) { + private List getOrderScenarios(List ids) { List apiScenarios = new ArrayList<>(TASK_BATCH_SIZE); // 分批查询 List finalApiScenarios = apiScenarios; @@ -232,10 +232,6 @@ public class ApiScenarioBatchRunService { // 按照ID顺序排序 ApiScenario apiScenario = apiScenarioMap.get(id); if (apiScenario == null) { - if (runModeConfig.isIntegratedReport()) { - // 用例不存在,则在执行集合中删除 - apiExecutionSetService.removeItem(runModeConfig.getCollectionReport().getReportId(), id); - } LogUtils.info("当前执行任务的用例已删除 {}", id); break; } diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/service/scenario/ApiScenarioRunService.java b/backend/services/api-test/src/main/java/io/metersphere/api/service/scenario/ApiScenarioRunService.java index cf1ca3d9a4..6e4b0a9458 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/service/scenario/ApiScenarioRunService.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/service/scenario/ApiScenarioRunService.java @@ -300,7 +300,6 @@ public class ApiScenarioRunService { } public GetRunScriptResult getRunScript(GetRunScriptRequest request, ApiScenarioDetail apiScenarioDetail) { - String id = apiScenarioDetail.getId(); TaskItem taskItem = request.getTaskItem(); ApiRunModeConfigDTO runModeConfig = request.getRunModeConfig(); String reportId = taskItem.getReportId(); @@ -308,9 +307,9 @@ public class ApiScenarioRunService { if (apiScenarioDetail == null) { if (runModeConfig.isIntegratedReport()) { // 用例不存在,则在执行集合中删除 - apiExecutionSetService.removeItem(runModeConfig.getCollectionReport().getReportId(), id); + apiExecutionSetService.removeItem(request.getTaskId(), taskItem.getId()); } - LogUtils.info("当前执行任务的用例已删除 {}", id); + LogUtils.info("当前执行任务的用例已删除 {}", taskItem.getId()); return null; } diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/service/BaseTaskHubService.java b/backend/services/system-setting/src/main/java/io/metersphere/system/service/BaseTaskHubService.java index f849618699..e1502be46e 100644 --- a/backend/services/system-setting/src/main/java/io/metersphere/system/service/BaseTaskHubService.java +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/service/BaseTaskHubService.java @@ -582,7 +582,11 @@ public class BaseTaskHubService { //2.更新任务明细状态 extExecTaskItemMapper.batchUpdateTaskItemStatus(List.of(id), userId, orgId, projectId, ExecStatus.STOPPED.name()); - handleStopTask(List.of(id)); + handleStopTaskAsync(List.of(id)); + } + + private void handleStopTaskAsync(List ids) { + Thread.startVirtualThread(() -> handleStopTask(ids)); } private void handleStopTask(List ids) { @@ -590,16 +594,20 @@ public class BaseTaskHubService { List list = extExecTaskItemMapper.getResourcePoolsByTaskIds(ids); Map> resourcePoolMaps = list.stream().collect(Collectors.groupingBy(ExecTaskItem::getResourcePoolId)); resourcePoolMaps.forEach((k, v) -> { - //判断资源池类型 - TestResourcePoolReturnDTO testResourcePoolDTO = testResourcePoolService.getTestResourcePoolDetail(k); - boolean isK8SResourcePool = StringUtils.equals(testResourcePoolDTO.getType(), ResourcePoolTypeEnum.K8S.getName()); - if (isK8SResourcePool) { - List taskIds = list.stream().map(ExecTaskItem::getTaskId).distinct().toList(); - //K8S - handleK8STask(taskIds, testResourcePoolDTO); - } else { - Map> nodeItem = list.stream().collect(Collectors.groupingBy(ExecTaskItem::getResourcePoolNode)); - handleNodeTask(nodeItem); + try { + //判断资源池类型 + TestResourcePoolReturnDTO testResourcePoolDTO = testResourcePoolService.getTestResourcePoolDetail(k); + boolean isK8SResourcePool = StringUtils.equals(testResourcePoolDTO.getType(), ResourcePoolTypeEnum.K8S.getName()); + if (isK8SResourcePool) { + List taskIds = list.stream().map(ExecTaskItem::getTaskId).distinct().toList(); + //K8S + handleK8STask(taskIds, testResourcePoolDTO); + } else { + Map> nodeItem = list.stream().collect(Collectors.groupingBy(ExecTaskItem::getResourcePoolNode)); + handleNodeTask(nodeItem); + } + } catch (Exception e) { + LogUtils.error(e); } }); } @@ -640,7 +648,7 @@ public class BaseTaskHubService { execTaskItemMapper.deleteByExample(itemExample); //3.删除任务与报告关联关系 deleteReportRelateTask(List.of(id)); - handleStopTask(List.of(id)); + handleStopTaskAsync(List.of(id)); } private void deleteReportRelateTask(List ids) { @@ -657,7 +665,7 @@ public class BaseTaskHubService { extExecTaskMapper.batchUpdateTaskStatus(ids, userId, orgId, projectId, ExecStatus.STOPPED.name()); //2.更新任务明细状态 extExecTaskItemMapper.batchUpdateTaskItemStatus(ids, userId, orgId, projectId, ExecStatus.STOPPED.name()); - handleStopTask(ids); + handleStopTaskAsync(ids); } } @@ -684,7 +692,7 @@ public class BaseTaskHubService { execTaskItemMapper.deleteByExample(itemExample); //3.删除任务与报告关联关系 deleteReportRelateTask(ids); - handleStopTask(ids); + handleStopTaskAsync(ids); } } @@ -700,24 +708,31 @@ public class BaseTaskHubService { //1.更新任务明细状态 extExecTaskItemMapper.batchUpdateTaskItemStatusByIds(List.of(id), userId, orgId, projectId, ExecStatus.STOPPED.name()); //2.通过任务项id停止任务 - handleStopTaskItem(List.of(id)); + handleStopTaskItemAsync(List.of(id)); } + private void handleStopTaskItemAsync(List ids) { + Thread.startVirtualThread(() -> handleStopTaskItem(ids)); + } private void handleStopTaskItem(List ids) { List execTaskItems = extExecTaskItemMapper.getResourcePoolsByItemIds(ids); Map> resourcePoolMaps = execTaskItems.stream().collect(Collectors.groupingBy(ExecTaskItem::getResourcePoolId)); resourcePoolMaps.forEach((k, v) -> { - //判断资源池类型 - TestResourcePoolReturnDTO testResourcePoolDTO = testResourcePoolService.getTestResourcePoolDetail(k); - boolean isK8SResourcePool = StringUtils.equals(testResourcePoolDTO.getType(), ResourcePoolTypeEnum.K8S.getName()); - if (isK8SResourcePool) { - List itemIds = v.stream().map(ExecTaskItem::getId).toList(); - //K8S 通过任务项id停止任务项 - handleK8STaskItem(itemIds, testResourcePoolDTO); - } else { - Map> nodeItem = execTaskItems.stream().collect(Collectors.groupingBy(ExecTaskItem::getResourcePoolNode)); - handleNodeTask(nodeItem); + try { + //判断资源池类型 + TestResourcePoolReturnDTO testResourcePoolDTO = testResourcePoolService.getTestResourcePoolDetail(k); + boolean isK8SResourcePool = StringUtils.equals(testResourcePoolDTO.getType(), ResourcePoolTypeEnum.K8S.getName()); + if (isK8SResourcePool) { + List itemIds = v.stream().map(ExecTaskItem::getId).toList(); + //K8S 通过任务项id停止任务项 + handleK8STaskItem(itemIds, testResourcePoolDTO); + } else { + Map> nodeItem = execTaskItems.stream().collect(Collectors.groupingBy(ExecTaskItem::getResourcePoolNode)); + handleNodeTask(nodeItem); + } + } catch (Exception e) { + LogUtils.error(e); } }); } @@ -739,7 +754,7 @@ public class BaseTaskHubService { //1.更新任务明细状态 extExecTaskItemMapper.batchUpdateTaskItemStatusByIds(itemIds, userId, orgId, projectId, ExecStatus.STOPPED.name()); //2.通过任务项id停止任务 - handleStopTaskItem(itemIds); + handleStopTaskItemAsync(itemIds); } }