fix(任务中心): 全部停止时仅停止当前页的十个任务其他没停止

--bug=1008088 --user=王孝刚 [任务中心]-全部停止 仅停止当前页的十个任务其他没停止
https://www.tapd.cn/55049933/s/1070788
This commit is contained in:
wxg0103 2021-11-19 19:53:54 +08:00 committed by 刘瑞斌
parent e8b9a29092
commit a180146682
6 changed files with 122 additions and 47 deletions

View File

@ -6,5 +6,6 @@ import lombok.Data;
public class TaskRequest { public class TaskRequest {
private String type; private String type;
private String reportId; private String reportId;
private String projectId;
} }

View File

@ -275,14 +275,31 @@
</select> </select>
<select id="selectPreviousReportByScenarioId" resultType="io.metersphere.base.domain.ApiScenarioReport"> <select id="selectPreviousReportByScenarioId" resultType="io.metersphere.base.domain.ApiScenarioReport">
select * from api_scenario_report select *
WHERE execute_type in ("Completed","Debug") and scenario_id=#{scenarioId} and id != #{nowId} ORDER BY create_time desc LIMIT 5,1 from api_scenario_report
WHERE execute_type in ("Completed", "Debug")
and scenario_id = #{scenarioId}
and id != #{nowId}
ORDER BY create_time desc LIMIT 5, 1
</select> </select>
<select id="countByApiScenarioId" resultType="io.metersphere.dto.ApiReportCountDTO"> <select id="countByApiScenarioId" resultType="io.metersphere.dto.ApiReportCountDTO">
SELECT scenario_id AS id,count(id) AS countNum SELECT scenario_id AS id, count(id) AS countNum
FROM api_scenario_report FROM api_scenario_report
WHERE scenario_id is not null GROUP BY scenario_id; WHERE scenario_id is not null
GROUP BY scenario_id;
</select>
<select id="selectReportByProjectId" resultType="io.metersphere.base.domain.ApiScenarioReport">
select t.*
from api_scenario_report t
left join `user` t1 ON t.user_id = t1.id
left join test_resource_pool t2 on t.actuator = t2.id
where to_days(FROM_UNIXTIME(t.create_time / 1000)) = to_days(now())
and t.execute_type !='Debug' and t.execute_type !='Marge' and t.project_id=#{projectId}
and t.`STATUS` in ('running'
, 'waiting')
</select> </select>
<select id="selectStatusByIds" resultType="io.metersphere.base.domain.ApiScenarioReport"> <select id="selectStatusByIds" resultType="io.metersphere.base.domain.ApiScenarioReport">

View File

@ -1,5 +1,6 @@
package io.metersphere.base.mapper.ext; package io.metersphere.base.mapper.ext;
import io.metersphere.base.domain.LoadTestReport;
import io.metersphere.base.domain.LoadTestReportWithBLOBs; import io.metersphere.base.domain.LoadTestReportWithBLOBs;
import io.metersphere.dto.DashboardTestDTO; import io.metersphere.dto.DashboardTestDTO;
import io.metersphere.dto.ReportDTO; import io.metersphere.dto.ReportDTO;
@ -19,4 +20,6 @@ public interface ExtLoadTestReportMapper {
List<String> selectResourceId(@Param("reportId") String reportId); List<String> selectResourceId(@Param("reportId") String reportId);
void updateJmxContentIfAbsent(LoadTestReportWithBLOBs record); void updateJmxContentIfAbsent(LoadTestReportWithBLOBs record);
List<LoadTestReport> selectReportByProjectId(String projectId);
} }

View File

@ -149,7 +149,8 @@
FROM load_test FROM load_test
JOIN project ON load_test.project_id = project.id JOIN project ON load_test.project_id = project.id
WHERE workspace_id = #{workspaceId,jdbcType=VARCHAR}) WHERE workspace_id = #{workspaceId,jdbcType=VARCHAR})
AND create_time > #{startTimestamp} AND create_time
> #{startTimestamp}
GROUP BY x GROUP BY x
</select> </select>
@ -159,11 +160,22 @@
WHERE report_id = #{reportId} WHERE report_id = #{reportId}
GROUP BY resource_id GROUP BY resource_id
</select> </select>
<select id="selectReportByProjectId" resultType="io.metersphere.base.domain.LoadTestReport">
select t.*
from load_test_report t
left join `user` t1 ON t.user_id = t1.id
left join test_resource_pool t2 on t.test_resource_pool_id = t2.id
where to_days(FROM_UNIXTIME(t.create_time / 1000)) = to_days(now())
and t.project_id = #{projectId}
and t.status = 'running'
</select>
<update id="updateJmxContentIfAbsent"> <update id="updateJmxContentIfAbsent">
update load_test_report update load_test_report
set jmx_content = #{jmxContent,jdbcType=VARCHAR} set jmx_content = #{jmxContent,jdbcType=VARCHAR}
WHERE id = #{id,jdbcType=VARCHAR} and jmx_content is null WHERE id = #{id,jdbcType=VARCHAR}
and jmx_content is null
</update> </update>
</mapper> </mapper>

View File

@ -10,6 +10,9 @@ import io.metersphere.base.mapper.ApiDefinitionExecResultMapper;
import io.metersphere.base.mapper.ApiScenarioReportMapper; import io.metersphere.base.mapper.ApiScenarioReportMapper;
import io.metersphere.base.mapper.TestResourceMapper; import io.metersphere.base.mapper.TestResourceMapper;
import io.metersphere.base.mapper.TestResourcePoolMapper; import io.metersphere.base.mapper.TestResourcePoolMapper;
import io.metersphere.base.mapper.ext.ExtApiDefinitionExecResultMapper;
import io.metersphere.base.mapper.ext.ExtApiScenarioReportMapper;
import io.metersphere.base.mapper.ext.ExtLoadTestReportMapper;
import io.metersphere.base.mapper.ext.ExtTaskMapper; import io.metersphere.base.mapper.ext.ExtTaskMapper;
import io.metersphere.commons.utils.LogUtil; import io.metersphere.commons.utils.LogUtil;
import io.metersphere.dto.NodeDTO; import io.metersphere.dto.NodeDTO;
@ -46,6 +49,12 @@ public class TaskService {
private TestResourcePoolMapper testResourcePoolMapper; private TestResourcePoolMapper testResourcePoolMapper;
@Resource @Resource
private TestResourceMapper testResourceMapper; private TestResourceMapper testResourceMapper;
@Resource
private ExtApiDefinitionExecResultMapper extApiDefinitionExecResultMapper;
@Resource
private ExtApiScenarioReportMapper extApiScenarioReportMapper;
@Resource
private ExtLoadTestReportMapper extLoadTestReportMapper;
public List<TaskCenterDTO> getTasks(TaskCenterRequest request) { public List<TaskCenterDTO> getTasks(TaskCenterRequest request) {
if (StringUtils.isEmpty(request.getProjectId())) { if (StringUtils.isEmpty(request.getProjectId())) {
@ -102,42 +111,82 @@ public class TaskService {
Map<String, List<String>> poolMap = new HashMap<>(); Map<String, List<String>> poolMap = new HashMap<>();
for (TaskRequest request : reportIds) { for (TaskRequest request : reportIds) {
String actuator = null; String actuator = null;
if (StringUtils.equals(request.getType(), "API")) { if (StringUtils.isNotEmpty(request.getReportId())) {
ApiDefinitionExecResult result = apiDefinitionExecResultMapper.selectByPrimaryKey(request.getReportId()); if (StringUtils.equals(request.getType(), "API")) {
if (result != null) { ApiDefinitionExecResult result = apiDefinitionExecResultMapper.selectByPrimaryKey(request.getReportId());
result.setStatus("STOP"); if (result != null) {
apiDefinitionExecResultMapper.updateByPrimaryKeySelective(result); result.setStatus("STOP");
actuator = result.getActuator(); apiDefinitionExecResultMapper.updateByPrimaryKeySelective(result);
MessageCache.caseExecResourceLock.remove(result.getId()); actuator = result.getActuator();
} MessageCache.caseExecResourceLock.remove(result.getId());
} else if (StringUtils.equals(request.getType(), "SCENARIO")) { }
ApiScenarioReport report = apiScenarioReportMapper.selectByPrimaryKey(request.getReportId()); } else if (StringUtils.equals(request.getType(), "SCENARIO")) {
if (report != null) { ApiScenarioReport report = apiScenarioReportMapper.selectByPrimaryKey(request.getReportId());
report.setStatus("STOP"); if (report != null) {
apiScenarioReportMapper.updateByPrimaryKeySelective(report); report.setStatus("STOP");
actuator = report.getActuator(); apiScenarioReportMapper.updateByPrimaryKeySelective(report);
} actuator = report.getActuator();
} else if (StringUtils.equals(request.getType(), "PERFORMANCE")) { }
performanceTestService.stopTest(request.getReportId(), false); } else if (StringUtils.equals(request.getType(), "PERFORMANCE")) {
} performanceTestService.stopTest(request.getReportId(), false);
if (StringUtils.isNotEmpty(actuator) && !StringUtils.equals(actuator, "LOCAL")) {
if (poolMap.containsKey(actuator)) {
poolMap.get(actuator).add(request.getReportId());
} else {
poolMap.put(actuator, new ArrayList<String>() {{
this.add(request.getReportId());
}});
} }
extracted(poolMap, request, actuator);
} else { } else {
new LocalRunner().stop(request.getReportId()); if (StringUtils.equals(request.getType(), "API")) {
List<ApiDefinitionExecResult> result = extApiDefinitionExecResultMapper.selectApiResultByProjectId(request.getProjectId());
if (CollectionUtils.isNotEmpty(result)) {
for (ApiDefinitionExecResult item : result) {
item.setStatus("STOP");
apiDefinitionExecResultMapper.updateByPrimaryKeySelective(item);
actuator = item.getActuator();
MessageCache.caseExecResourceLock.remove(item.getId());
request.setReportId(item.getId());
extracted(poolMap, request, actuator);
}
}
} else if (StringUtils.equals(request.getType(), "SCENARIO")) {
List<ApiScenarioReport> reports = extApiScenarioReportMapper.selectReportByProjectId(request.getProjectId());
if (CollectionUtils.isNotEmpty(reports)) {
for (ApiScenarioReport report : reports) {
report.setStatus("STOP");
apiScenarioReportMapper.updateByPrimaryKeySelective(report);
actuator = report.getActuator();
request.setReportId(report.getId());
extracted(poolMap, request, actuator);
}
}
} else if (StringUtils.equals(request.getType(), "PERFORMANCE")) {
List<LoadTestReport> loadTestReports = extLoadTestReportMapper.selectReportByProjectId(request.getProjectId());
if (CollectionUtils.isNotEmpty(loadTestReports)) {
for (LoadTestReport loadTestReport : loadTestReports) {
performanceTestService.stopTest(loadTestReport.getId(), false);
request.setReportId(loadTestReport.getId());
extracted(poolMap, request, actuator);
}
}
}
}
if (!poolMap.isEmpty()) {
this.send(poolMap);
} }
MessageCache.cache.remove(request.getReportId());
MessageCache.terminationOrderDeque.add(request.getReportId());
}
if (!poolMap.isEmpty()) {
this.send(poolMap);
} }
} }
return "SUCCESS"; return "SUCCESS";
} }
private void extracted(Map<String, List<String>> poolMap, TaskRequest request, String actuator) {
if (StringUtils.isNotEmpty(actuator) && !StringUtils.equals(actuator, "LOCAL")) {
if (poolMap.containsKey(actuator)) {
poolMap.get(actuator).add(request.getReportId());
} else {
poolMap.put(actuator, new ArrayList<String>() {{
this.add(request.getReportId());
}});
}
} else {
new LocalRunner().stop(request.getReportId());
}
MessageCache.cache.remove(request.getReportId());
MessageCache.terminationOrderDeque.add(request.getReportId());
}
} }

View File

@ -230,16 +230,9 @@ export default {
let request = {type: row.executionModule, reportId: row.id}; let request = {type: row.executionModule, reportId: row.id};
array = [request]; array = [request];
} else { } else {
this.taskData.forEach(item => { array.push({type: 'API', projectId: getCurrentProjectID()});
if (this.showStop(item.executionStatus)) { array.push({type: 'SCENARIO', projectId: getCurrentProjectID()});
let request = {type: item.executionModule, reportId: item.id}; array.push({type: 'PERFORMANCE', projectId: getCurrentProjectID()});
array.push(request);
}
});
}
if (array.length === 0) {
this.$warning("没有需要停止的任务");
return;
} }
this.$post('/api/automation/stop/batch', array, response => { this.$post('/api/automation/stop/batch', array, response => {
this.$success(this.$t('report.test_stop_success')); this.$success(this.$t('report.test_stop_success'));