From ffc3bfc93442248ba9cf9b71d63bbbc2c190448b Mon Sep 17 00:00:00 2001 From: wxg0103 <727495428@qq.com> Date: Sat, 15 Jun 2024 20:30:47 +0800 Subject: [PATCH] =?UTF-8?q?refactor(=E6=B5=8B=E8=AF=95=E8=AE=A1=E5=88=92):?= =?UTF-8?q?=20=E5=81=9C=E6=AD=A2=E6=B5=8B=E8=AF=95=E8=AE=A1=E5=88=92?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../plan/service/TestPlanExecuteService.java | 76 ++++++++++--------- .../service/TestPlanTaskCenterService.java | 25 ++++-- 2 files changed, 57 insertions(+), 44 deletions(-) diff --git a/backend/services/test-plan/src/main/java/io/metersphere/plan/service/TestPlanExecuteService.java b/backend/services/test-plan/src/main/java/io/metersphere/plan/service/TestPlanExecuteService.java index 2fbe7818a5..460776468e 100644 --- a/backend/services/test-plan/src/main/java/io/metersphere/plan/service/TestPlanExecuteService.java +++ b/backend/services/test-plan/src/main/java/io/metersphere/plan/service/TestPlanExecuteService.java @@ -1,8 +1,8 @@ package io.metersphere.plan.service; import com.esotericsoftware.minlog.Log; -import io.metersphere.plan.dto.TestPlanReportPostParam; import io.metersphere.plan.domain.*; +import io.metersphere.plan.dto.TestPlanReportPostParam; import io.metersphere.plan.dto.request.TestPlanBatchExecuteRequest; import io.metersphere.plan.dto.request.TestPlanExecuteRequest; import io.metersphere.plan.dto.request.TestPlanReportGenRequest; @@ -66,12 +66,12 @@ public class TestPlanExecuteService { private TestPlanReportMapper testPlanReportMapper; // 停止测试计划的执行 - public void stopTestPlanRunning(String testPlanReportId){ + public void stopTestPlanRunning(String testPlanReportId) { TestPlanReport testPlanReport = testPlanReportMapper.selectByPrimaryKey(testPlanReportId); - if(testPlanReport ==null){ + if (testPlanReport == null) { return; } - if(testPlanReport.getIntegrated()){ + if (testPlanReport.getIntegrated()) { TestPlanReportExample reportExample = new TestPlanReportExample(); reportExample.createCriteria().andParentIdEqualTo(testPlanReportId); List testPlanItemReport = testPlanReportMapper.selectByExample(reportExample); @@ -86,21 +86,21 @@ public class TestPlanExecuteService { 继续执行 test-plan-batch-execute:randomId 队列的下一条 */ // 获取下一个要执行的测试计划节点,目的是得到最后一条的queueId - TestPlanExecutionQueue nextTestPlanQueue = this.getNextQueue(testPlanReportId,QUEUE_PREFIX_TEST_PLAN_GROUP_EXECUTE); - if(nextTestPlanQueue == null || !StringUtils.equalsIgnoreCase(nextTestPlanQueue.getParentQueueType(),QUEUE_PREFIX_TEST_PLAN_BATCH_EXECUTE)){ - throw new MSException("停止执行测试计划组失败!无法获取到执行队列!"); + TestPlanExecutionQueue nextTestPlanQueue = this.getNextQueue(testPlanReportId, QUEUE_PREFIX_TEST_PLAN_GROUP_EXECUTE); + if (nextTestPlanQueue == null || !StringUtils.equalsIgnoreCase(nextTestPlanQueue.getParentQueueType(), QUEUE_PREFIX_TEST_PLAN_BATCH_EXECUTE)) { + return; } - String groupExecuteQueueId = genQueueKey(testPlanReportId,QUEUE_PREFIX_TEST_PLAN_GROUP_EXECUTE); + String groupExecuteQueueId = genQueueKey(testPlanReportId, QUEUE_PREFIX_TEST_PLAN_GROUP_EXECUTE); this.deleteRedisKey(groupExecuteQueueId); testPlanItemReport.forEach(item -> { this.deepDeleteTestPlanCaseType(item); //统计子测试计划报告 - summaryTestPlanReport(item.getId(),false); + summaryTestPlanReport(item.getId(), false); }); - summaryTestPlanReport(testPlanReportId,true); - this.testPlanExecuteQueueFinish(nextTestPlanQueue.getParentQueueId(),nextTestPlanQueue.getParentQueueType()); - }else { + summaryTestPlanReport(testPlanReportId, true); + this.testPlanExecuteQueueFinish(nextTestPlanQueue.getParentQueueId(), nextTestPlanQueue.getParentQueueType()); + } else { /* 独立报告中,停止的是单独测试计划的执行 这条测试计划是在批量执行队列中,还是在测试计划组执行队中, 通过要删除的队列1(因为当前节点在执行之前就被弹出)来确定。 @@ -111,26 +111,26 @@ public class TestPlanExecuteService { 进行当前报告结算 继续执行 队列的下一条 */ - TestPlanExecutionQueue nextTestPlanQueue = this.getNextQueue(testPlanReportId,QUEUE_PREFIX_TEST_PLAN_CASE_TYPE); - if(nextTestPlanQueue == null || !StringUtils.equalsAnyIgnoreCase(nextTestPlanQueue.getParentQueueType(),QUEUE_PREFIX_TEST_PLAN_GROUP_EXECUTE,QUEUE_PREFIX_TEST_PLAN_BATCH_EXECUTE)){ - throw new MSException("停止测试计划执行失败!无法获取到执行队列!"); + TestPlanExecutionQueue nextTestPlanQueue = this.getNextQueue(testPlanReportId, QUEUE_PREFIX_TEST_PLAN_CASE_TYPE); + if (nextTestPlanQueue == null || !StringUtils.equalsAnyIgnoreCase(nextTestPlanQueue.getParentQueueType(), QUEUE_PREFIX_TEST_PLAN_GROUP_EXECUTE, QUEUE_PREFIX_TEST_PLAN_BATCH_EXECUTE)) { + return; } this.deepDeleteTestPlanCaseType(testPlanReport); - summaryTestPlanReport(testPlanReportId,false); - this.testPlanExecuteQueueFinish(nextTestPlanQueue.getParentQueueId(),nextTestPlanQueue.getParentQueueType()); + summaryTestPlanReport(testPlanReportId, false); + this.testPlanExecuteQueueFinish(nextTestPlanQueue.getParentQueueId(), nextTestPlanQueue.getParentQueueType()); } // todo @wxg 是在 deepDeleteTestPlanCaseType()方法中删除用例执行队列时,同步到执行机停止执行任务,还是其它操作, 由你来决定了 } - private void deepDeleteTestPlanCaseType(TestPlanReport report){ - this.deleteRedisKey(genQueueKey(report.getId(),QUEUE_PREFIX_TEST_PLAN_CASE_TYPE)); + private void deepDeleteTestPlanCaseType(TestPlanReport report) { + this.deleteRedisKey(genQueueKey(report.getId(), QUEUE_PREFIX_TEST_PLAN_CASE_TYPE)); TestPlanCollectionExample collectionExample = new TestPlanCollectionExample(); collectionExample.createCriteria().andTestPlanIdEqualTo(report.getTestPlanId()).andParentIdEqualTo(TestPlanConstants.DEFAULT_PARENT_ID); List parentTestPlanCollectionList = testPlanCollectionMapper.selectByExample(collectionExample); - parentTestPlanCollectionList.forEach( parentCollection -> { + parentTestPlanCollectionList.forEach(parentCollection -> { - this.deleteRedisKey(genQueueKey(report.getId()+"_"+parentCollection.getId(),QUEUE_PREFIX_TEST_PLAN_COLLECTION)); + this.deleteRedisKey(genQueueKey(report.getId() + "_" + parentCollection.getId(), QUEUE_PREFIX_TEST_PLAN_COLLECTION)); //todo @Chen-Jianxing 这里要同步清理用例/场景的执行队列 }); @@ -159,6 +159,7 @@ public class TestPlanExecuteService { redisTemplate.delete(redisKey); redisTemplate.delete(genQueueKey(redisKey, LAST_QUEUE_PREFIX)); } + //批量执行测试计划组 public void batchExecuteTestPlan(TestPlanBatchExecuteRequest request, String userId) { List rightfulIds = testPlanService.selectRightfulIds(request.getExecuteIds()); @@ -218,7 +219,7 @@ public class TestPlanExecuteService { if (StringUtils.equalsIgnoreCase(testPlan.getType(), TestPlanConstants.TEST_PLAN_TYPE_GROUP)) { List children = testPlanService.selectNotArchivedChildren(testPlan.getId()); // 预生成计划组报告 - Map reportMap = testPlanReportService.genReportByExecution(executionQueue.getPrepareReportId(),genReportRequest, executionQueue.getCreateUser()); + Map reportMap = testPlanReportService.genReportByExecution(executionQueue.getPrepareReportId(), genReportRequest, executionQueue.getCreateUser()); long pos = 0; List childrenQueue = new ArrayList<>(); @@ -248,7 +249,7 @@ public class TestPlanExecuteService { this.setRedisForList(genQueueKey(queueId, queueType), childrenQueue.stream().map(JSON::toJSONString).toList()); // 更新报告的执行时间 - extTestPlanReportMapper.batchUpdateExecuteTime(System.currentTimeMillis(),reportMap.values().stream().toList()); + extTestPlanReportMapper.batchUpdateExecuteTime(System.currentTimeMillis(), reportMap.values().stream().toList()); if (StringUtils.equalsIgnoreCase(executionQueue.getRunMode(), ApiBatchRunMode.SERIAL.name())) { //串行 @@ -264,9 +265,9 @@ public class TestPlanExecuteService { return executionQueue.getPrepareReportId(); } else { - Map reportMap = testPlanReportService.genReportByExecution(executionQueue.getPrepareReportId(),genReportRequest, executionQueue.getCreateUser()); + Map reportMap = testPlanReportService.genReportByExecution(executionQueue.getPrepareReportId(), genReportRequest, executionQueue.getCreateUser()); executionQueue.setPrepareReportId(reportMap.get(executionQueue.getSourceID())); - extTestPlanReportMapper.batchUpdateExecuteTime(System.currentTimeMillis(),reportMap.values().stream().toList()); + extTestPlanReportMapper.batchUpdateExecuteTime(System.currentTimeMillis(), reportMap.values().stream().toList()); this.executeTestPlan(executionQueue); return executionQueue.getPrepareReportId(); } @@ -338,7 +339,7 @@ public class TestPlanExecuteService { int pos = 0; List childrenQueue = new ArrayList<>(); - String queueId = executionQueue.getPrepareReportId()+"_"+parentCollection.getId(); + String queueId = executionQueue.getPrepareReportId() + "_" + parentCollection.getId(); String queueType = QUEUE_PREFIX_TEST_PLAN_COLLECTION; for (TestPlanCollection collection : childrenList) { childrenQueue.add( @@ -379,6 +380,7 @@ public class TestPlanExecuteService { /** * 批量执行单个测试集的用例 + * * @param testPlanExecutionQueue */ private void executeCase(TestPlanExecutionQueue testPlanExecutionQueue) { @@ -504,11 +506,11 @@ public class TestPlanExecuteService { } } - private void summaryTestPlanReport(String reportId,boolean isGroupReport){ + private void summaryTestPlanReport(String reportId, boolean isGroupReport) { try { - if(isGroupReport){ + if (isGroupReport) { testPlanReportService.summaryGroupReport(reportId); - }else { + } else { testPlanReportService.summaryPlanReport(reportId); } @@ -519,34 +521,34 @@ public class TestPlanExecuteService { postParam.setExecStatus(ExecStatus.COMPLETED.name()); testPlanReportService.postHandleReport(postParam); - if(!isGroupReport){ + if (!isGroupReport) { TestPlanReport testPlanReport = testPlanReportService.selectById(reportId); - if(testPlanReport!=null){ + if (testPlanReport != null) { testPlanService.refreshTestPlanStatus(testPlanReport.getTestPlanId()); } } - }catch (Exception e){ + } catch (Exception e) { LogUtils.error("Cannot find test plan report for " + reportId, e); } } private void queueExecuteFinish(TestPlanExecutionQueue queue) { if (StringUtils.equalsIgnoreCase(queue.getParentQueueType(), QUEUE_PREFIX_TEST_PLAN_BATCH_EXECUTE)) { - if(StringUtils.equalsIgnoreCase(queue.getQueueType(),QUEUE_PREFIX_TEST_PLAN_GROUP_EXECUTE)){ + if (StringUtils.equalsIgnoreCase(queue.getQueueType(), QUEUE_PREFIX_TEST_PLAN_GROUP_EXECUTE)) { // 计划组报告汇总并统计 - this.summaryTestPlanReport(queue.getQueueId(),true); - }else if(StringUtils.equalsIgnoreCase(queue.getQueueType(),QUEUE_PREFIX_TEST_PLAN_CASE_TYPE)){ + this.summaryTestPlanReport(queue.getQueueId(), true); + } else if (StringUtils.equalsIgnoreCase(queue.getQueueType(), QUEUE_PREFIX_TEST_PLAN_CASE_TYPE)) { /* 此时处于批量勾选执行中的游离态测试计划执行。所以队列顺序为:QUEUE_PREFIX_TEST_PLAN_BATCH_EXECUTE -> QUEUE_PREFIX_TEST_PLAN_CASE_TYPE。 此时queue节点为testPlanCollection的节点。 而测试计划节点(串行状态下)在执行之前就被弹出了。 所以获取报告ID的方式为读取queueId (caseType队列和collection队列的queueId都是报告ID) */ - this.summaryTestPlanReport(queue.getQueueId(),false); + this.summaryTestPlanReport(queue.getQueueId(), false); } this.testPlanGroupQueueFinish(queue.getParentQueueId(), queue.getParentQueueType()); } else if (StringUtils.equalsIgnoreCase(queue.getParentQueueType(), QUEUE_PREFIX_TEST_PLAN_GROUP_EXECUTE)) { // 计划报告汇总并统计 - this.summaryTestPlanReport(queue.getQueueId(),false); + this.summaryTestPlanReport(queue.getQueueId(), false); this.testPlanExecuteQueueFinish(queue.getParentQueueId(), queue.getParentQueueType()); } else if (StringUtils.equalsIgnoreCase(queue.getParentQueueType(), QUEUE_PREFIX_TEST_PLAN_CASE_TYPE)) { this.caseTypeExecuteQueueFinish(queue.getParentQueueId(), queue.getParentQueueType()); diff --git a/backend/services/test-plan/src/main/java/io/metersphere/plan/service/TestPlanTaskCenterService.java b/backend/services/test-plan/src/main/java/io/metersphere/plan/service/TestPlanTaskCenterService.java index 54c0b812c2..1d9e0dd583 100644 --- a/backend/services/test-plan/src/main/java/io/metersphere/plan/service/TestPlanTaskCenterService.java +++ b/backend/services/test-plan/src/main/java/io/metersphere/plan/service/TestPlanTaskCenterService.java @@ -82,10 +82,9 @@ public class TestPlanTaskCenterService { ExtApiReportMapper extApiReportMapper; @Resource ExtApiScenarioReportMapper extApiScenarioReportMapper; + @Resource + TestPlanExecuteService testPlanExecuteService; private static final String DEFAULT_SORT = "start_time desc"; - private final static String PROJECT_STOP = "/task/center/api/project/stop"; - private final static String ORG_STOP = "/task/center/api/org/stop"; - private final static String SYSTEM_STOP = "/task/center/api/system/stop"; /** * 任务中心实时任务列表-项目级 @@ -275,12 +274,24 @@ public class TestPlanTaskCenterService { } //根据id去重 List allReportIds = reports.stream().distinct().map(ReportDTO::getId).toList(); + if (CollectionUtils.isNotEmpty(allReportIds)) { // 查找和测试计划管理的接口用例的所有数据 - List apiReports = extTestPlanReportMapper.getCaseReports(allReportIds); - detailReport(request, apiReports, userId, module, ApiExecuteResourceType.TEST_PLAN_API_CASE.name()); - List scenarioReports = extTestPlanReportMapper.getScenarioReports(allReportIds); - detailReport(request, scenarioReports, userId, module, ApiExecuteResourceType.TEST_PLAN_API_SCENARIO.name()); + //停止测试计划的队列 + SubListUtils.dealForSubList(allReportIds, 10, (subList) -> { + subList.forEach(item -> { + try { + LogUtils.info(String.format("开始停止测试计划队列:%s", item)); + testPlanExecuteService.stopTestPlanRunning(item); + } catch (Exception e) { + LogUtils.error("停止测试计划队列异常", e); + } + }); + List apiReports = extTestPlanReportMapper.getCaseReports(subList); + detailReport(request, apiReports, userId, module, ApiExecuteResourceType.TEST_PLAN_API_CASE.name()); + List scenarioReports = extTestPlanReportMapper.getScenarioReports(subList); + detailReport(request, scenarioReports, userId, module, ApiExecuteResourceType.TEST_PLAN_API_SCENARIO.name()); + }); } }