diff --git a/backend/src/main/java/io/metersphere/api/controller/ApiAutomationController.java b/backend/src/main/java/io/metersphere/api/controller/ApiAutomationController.java index 7fde7c77f1..7c9e945c2c 100644 --- a/backend/src/main/java/io/metersphere/api/controller/ApiAutomationController.java +++ b/backend/src/main/java/io/metersphere/api/controller/ApiAutomationController.java @@ -17,7 +17,6 @@ import io.metersphere.commons.utils.Pager; import io.metersphere.controller.request.ResetOrderRequest; import io.metersphere.controller.request.ScheduleRequest; import io.metersphere.dto.MsExecResponseDTO; -import io.metersphere.jmeter.LocalRunner; import io.metersphere.log.annotation.MsAuditLog; import io.metersphere.notice.annotation.SendNotice; import io.metersphere.task.service.TaskService; @@ -33,6 +32,7 @@ import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; import javax.annotation.Resource; +import java.util.ArrayList; import java.util.List; @RestController @@ -328,8 +328,12 @@ public class ApiAutomationController { @GetMapping(value = "/stop/{reportId}") public void stop(@PathVariable String reportId) { if (StringUtils.isNotEmpty(reportId)) { - execThreadPoolExecutor.removeQueue(reportId); - new LocalRunner().stop(reportId); + List reportIds = new ArrayList<>(); + TaskRequest taskRequest = new TaskRequest(); + taskRequest.setReportId(reportId); + taskRequest.setType("SCENARIO"); + reportIds.add(taskRequest); + taskService.stop(reportIds); } } diff --git a/backend/src/main/java/io/metersphere/api/service/ApiExecutionQueueService.java b/backend/src/main/java/io/metersphere/api/service/ApiExecutionQueueService.java index ce0ab28c53..bbc8ed4704 100644 --- a/backend/src/main/java/io/metersphere/api/service/ApiExecutionQueueService.java +++ b/backend/src/main/java/io/metersphere/api/service/ApiExecutionQueueService.java @@ -163,7 +163,7 @@ public class ApiExecutionQueueService { } // 更新测试计划报告 if (StringUtils.isNotEmpty(dto.getTestPlanReportId())) { - CommonBeanFactory.getBean(TestPlanReportService.class).finishedTestPlanReport(dto.getTestPlanReportId()); + CommonBeanFactory.getBean(TestPlanReportService.class).finishedTestPlanReport(dto.getTestPlanReportId(), TestPlanReportStatus.COMPLETED.name()); } queueMapper.deleteByPrimaryKey(executionQueue.getId()); LoggerUtil.info("队列:" + dto.getQueueId() + " 执行结束"); @@ -222,11 +222,31 @@ public class ApiExecutionQueueService { queues.forEach(item -> { // 更新测试计划报告 if (StringUtils.isNotEmpty(item.getReportId())) { - CommonBeanFactory.getBean(TestPlanReportService.class).finishedTestPlanReport(item.getReportId()); + CommonBeanFactory.getBean(TestPlanReportService.class).finishedTestPlanReport(item.getReportId(), TestPlanReportStatus.COMPLETED.name()); } }); } // 清除异常队列/一般是服务突然停止产生 extApiExecutionQueueMapper.delete(); } + + public void stop(String reportId) { + ApiExecutionQueueDetailExample example = new ApiExecutionQueueDetailExample(); + example.createCriteria().andReportIdEqualTo(reportId); + List details = executionQueueDetailMapper.selectByExample(example); + details.forEach(detail -> { + executionQueueDetailMapper.deleteByPrimaryKey(detail.getId()); + + ApiExecutionQueueDetailExample queueDetailExample = new ApiExecutionQueueDetailExample(); + queueDetailExample.createCriteria().andQueueIdEqualTo(detail.getQueueId()); + long queueDetailSize = executionQueueDetailMapper.countByExample(queueDetailExample); + if (queueDetailSize <= 1) { + ApiExecutionQueue queue = queueMapper.selectByPrimaryKey(detail.getQueueId()); + // 更新测试计划报告 + if (queue != null && StringUtils.isNotEmpty(queue.getReportId())) { + CommonBeanFactory.getBean(TestPlanReportService.class).finishedTestPlanReport(queue.getReportId(), "Stopped"); + } + } + }); + } } diff --git a/backend/src/main/java/io/metersphere/task/service/TaskService.java b/backend/src/main/java/io/metersphere/task/service/TaskService.java index ab213addee..62b51aaee4 100644 --- a/backend/src/main/java/io/metersphere/task/service/TaskService.java +++ b/backend/src/main/java/io/metersphere/task/service/TaskService.java @@ -4,6 +4,7 @@ import com.alibaba.fastjson.JSON; import io.metersphere.api.dto.automation.TaskRequest; import io.metersphere.api.exec.queue.ExecThreadPoolExecutor; import io.metersphere.api.jmeter.JMeterService; +import io.metersphere.api.service.ApiExecutionQueueService; import io.metersphere.base.domain.*; import io.metersphere.base.mapper.ApiDefinitionExecResultMapper; import io.metersphere.base.mapper.ApiScenarioReportMapper; @@ -57,6 +58,8 @@ public class TaskService { private ExtLoadTestReportMapper extLoadTestReportMapper; @Resource private ExecThreadPoolExecutor execThreadPoolExecutor; + @Resource + private ApiExecutionQueueService apiExecutionQueueService; public List getTasks(TaskCenterRequest request) { if (StringUtils.isEmpty(request.getProjectId())) { @@ -112,11 +115,11 @@ public class TaskService { // 聚类,同一批资源池的一批发送 Map> poolMap = new HashMap<>(); for (TaskRequest request : reportIds) { - // 从队列移除 - execThreadPoolExecutor.removeQueue(request.getReportId()); - String actuator = null; if (StringUtils.isNotEmpty(request.getReportId())) { + // 从队列移除 + execThreadPoolExecutor.removeQueue(request.getReportId()); + apiExecutionQueueService.stop(request.getReportId()); if (StringUtils.equals(request.getType(), "API")) { ApiDefinitionExecResult result = apiDefinitionExecResultMapper.selectByPrimaryKey(request.getReportId()); if (result != null) { @@ -145,6 +148,9 @@ public class TaskService { actuator = item.getActuator(); request.setReportId(item.getId()); extracted(poolMap, request, actuator); + // 从队列移除 + execThreadPoolExecutor.removeQueue(item.getId()); + apiExecutionQueueService.stop(item.getId()); } } } else if (StringUtils.equals(request.getType(), "SCENARIO")) { @@ -156,6 +162,9 @@ public class TaskService { actuator = report.getActuator(); request.setReportId(report.getId()); extracted(poolMap, request, actuator); + // 从队列移除 + execThreadPoolExecutor.removeQueue(report.getId()); + apiExecutionQueueService.stop(report.getId()); } } } else if (StringUtils.equals(request.getType(), "PERFORMANCE")) { @@ -165,6 +174,9 @@ public class TaskService { performanceTestService.stopTest(loadTestReport.getId(), false); request.setReportId(loadTestReport.getId()); extracted(poolMap, request, actuator); + // 从队列移除 + execThreadPoolExecutor.removeQueue(loadTestReport.getId()); + apiExecutionQueueService.stop(loadTestReport.getId()); } } } diff --git a/backend/src/main/java/io/metersphere/track/service/TestPlanReportService.java b/backend/src/main/java/io/metersphere/track/service/TestPlanReportService.java index 4e98ee2031..257cc3ab82 100644 --- a/backend/src/main/java/io/metersphere/track/service/TestPlanReportService.java +++ b/backend/src/main/java/io/metersphere/track/service/TestPlanReportService.java @@ -305,14 +305,17 @@ public class TestPlanReportService { return reportContent; } - public TestPlanReport finishedTestPlanReport(String testPlanReportId) { + public TestPlanReport finishedTestPlanReport(String testPlanReportId, String status) { TestPlanReport testPlanReport = this.getTestPlanReport(testPlanReportId); + if (testPlanReport != null && StringUtils.equalsIgnoreCase(testPlanReport.getStatus(), "stopped")) { + return testPlanReport; + } if (testPlanReport != null) { //初始化测试计划包含组件信息 int[] componentIndexArr = new int[]{1, 3, 4}; testPlanReport.setComponents(JSONArray.toJSONString(componentIndexArr)); //计算测试计划状态 - testPlanReport.setStatus(TestPlanReportStatus.COMPLETED.name()); + testPlanReport.setStatus(status); //如果测试案例没有未结束的功能用例,则更新最后结束日期。 TestPlanTestCaseMapper testPlanTestCaseMapper = CommonBeanFactory.getBean(TestPlanTestCaseMapper.class); TestPlanTestCaseExample testPlanTestCaseExample = new TestPlanTestCaseExample(); @@ -332,7 +335,7 @@ public class TestPlanReportService { TestPlanReportContentWithBLOBs content = new TestPlanReportContentWithBLOBs(); content.setStartTime(testPlanReport.getStartTime()); content.setEndTime(endTime); - testPlanReportContentMapper.updateByExampleSelective(content,contentExample); + testPlanReportContentMapper.updateByExampleSelective(content, contentExample); //更新测试计划并发送通知 @@ -734,7 +737,7 @@ public class TestPlanReportService { TestPlanReportContentExample example = new TestPlanReportContentExample(); example.createCriteria().andTestPlanReportIdEqualTo(testPlanReportID); long dataCount = testPlanReportContentMapper.countByExample(example); - if(dataCount == 0){ + if (dataCount == 0) { TestPlanReportContentWithBLOBs content = new TestPlanReportContentWithBLOBs(); content.setId(UUID.randomUUID().toString()); content.setTestPlanReportId(testPlanReportID); diff --git a/backend/src/main/resources/blacklist/beanshell.bk b/backend/src/main/resources/blacklist/beanshell.bk index f1b66f3780..e69de29bb2 100644 --- a/backend/src/main/resources/blacklist/beanshell.bk +++ b/backend/src/main/resources/blacklist/beanshell.bk @@ -1 +0,0 @@ -exec \ No newline at end of file diff --git a/backend/src/main/resources/blacklist/groovy.bk b/backend/src/main/resources/blacklist/groovy.bk index f1b66f3780..e69de29bb2 100644 --- a/backend/src/main/resources/blacklist/groovy.bk +++ b/backend/src/main/resources/blacklist/groovy.bk @@ -1 +0,0 @@ -exec \ No newline at end of file