fix(接口测试): 执行队列异常保护清理失败队列

This commit is contained in:
fit2-zhao 2022-01-26 17:46:43 +08:00 committed by fit2-zhao
parent 6ee4272612
commit 555793c747
3 changed files with 16 additions and 22 deletions

View File

@ -1,7 +1,6 @@
package io.metersphere.api.jmeter; package io.metersphere.api.jmeter;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSON;
import io.metersphere.api.dto.RunRequest;
import io.metersphere.api.exec.queue.ExecThreadPoolExecutor; import io.metersphere.api.exec.queue.ExecThreadPoolExecutor;
import io.metersphere.api.exec.utils.GenerateHashTreeUtil; import io.metersphere.api.exec.utils.GenerateHashTreeUtil;
import io.metersphere.api.service.ApiScenarioReportService; import io.metersphere.api.service.ApiScenarioReportService;
@ -155,18 +154,12 @@ public class JMeterService {
ResponseEntity<String> result = restTemplate.postForEntity(uri, request, String.class); ResponseEntity<String> result = restTemplate.postForEntity(uri, request, String.class);
if (result == null || !StringUtils.equals("SUCCESS", result.getBody())) { if (result == null || !StringUtils.equals("SUCCESS", result.getBody())) {
RemakeReportService remakeReportService = CommonBeanFactory.getBean(RemakeReportService.class); RemakeReportService remakeReportService = CommonBeanFactory.getBean(RemakeReportService.class);
RunRequest runRequest = new RunRequest();
runRequest.setTestId(request.getTestId());
runRequest.setRunMode(request.getRunMode());
remakeReportService.remake(request); remakeReportService.remake(request);
LoggerUtil.error("发送请求[ " + request.getTestId() + " ] 到" + uri + " 节点执行失败"); LoggerUtil.error("发送请求[ " + request.getTestId() + " ] 到" + uri + " 节点执行失败");
LoggerUtil.info(result.getBody()); LoggerUtil.info(result.getBody());
} }
} catch (Exception e) { } catch (Exception e) {
RemakeReportService remakeReportService = CommonBeanFactory.getBean(RemakeReportService.class); RemakeReportService remakeReportService = CommonBeanFactory.getBean(RemakeReportService.class);
RunRequest runRequest = new RunRequest();
runRequest.setTestId(request.getTestId());
runRequest.setRunMode(request.getRunMode());
remakeReportService.remake(request); remakeReportService.remake(request);
LoggerUtil.error("发送请求[ " + request.getTestId() + " ] 执行失败:" + e.getMessage()); LoggerUtil.error("发送请求[ " + request.getTestId() + " ] 执行失败:" + e.getMessage());
LoggerUtil.error(e); LoggerUtil.error(e);

View File

@ -11,7 +11,6 @@ import io.metersphere.base.mapper.ApiExecutionQueueDetailMapper;
import io.metersphere.base.mapper.ApiScenarioMapper; import io.metersphere.base.mapper.ApiScenarioMapper;
import io.metersphere.base.mapper.TestPlanApiScenarioMapper; import io.metersphere.base.mapper.TestPlanApiScenarioMapper;
import io.metersphere.commons.constants.ApiRunMode; import io.metersphere.commons.constants.ApiRunMode;
import io.metersphere.commons.exception.MSException;
import io.metersphere.commons.utils.CommonBeanFactory; import io.metersphere.commons.utils.CommonBeanFactory;
import io.metersphere.commons.utils.FileUtils; import io.metersphere.commons.utils.FileUtils;
import io.metersphere.commons.utils.LogUtil; import io.metersphere.commons.utils.LogUtil;
@ -70,7 +69,7 @@ public class ApiJmeterFileService {
if (detail != null && StringUtils.isNotEmpty(detail.getEvnMap())) { if (detail != null && StringUtils.isNotEmpty(detail.getEvnMap())) {
envMap = JSON.parseObject(detail.getEvnMap(), Map.class); envMap = JSON.parseObject(detail.getEvnMap(), Map.class);
} }
HashTree hashTree; HashTree hashTree = null;
if (StringUtils.equalsAnyIgnoreCase(runMode, ApiRunMode.DEFINITION.name(), ApiRunMode.JENKINS_API_PLAN.name(), ApiRunMode.API_PLAN.name(), ApiRunMode.SCHEDULE_API_PLAN.name(), ApiRunMode.MANUAL_PLAN.name())) { if (StringUtils.equalsAnyIgnoreCase(runMode, ApiRunMode.DEFINITION.name(), ApiRunMode.JENKINS_API_PLAN.name(), ApiRunMode.API_PLAN.name(), ApiRunMode.SCHEDULE_API_PLAN.name(), ApiRunMode.MANUAL_PLAN.name())) {
hashTree = apiScenarioSerialService.generateHashTree(remoteTestId, reportId, runMode, envMap); hashTree = apiScenarioSerialService.generateHashTree(remoteTestId, reportId, runMode, envMap);
} else { } else {
@ -78,7 +77,8 @@ public class ApiJmeterFileService {
scenario = apiScenarioMapper.selectByPrimaryKey(remoteTestId); scenario = apiScenarioMapper.selectByPrimaryKey(remoteTestId);
} }
if (scenario == null) { if (scenario == null) {
MSException.throwException("未找到执行场景。"); // 清除队列
executionQueueDetailMapper.deleteByPrimaryKey(queueId);
} }
if (envMap != null && !envMap.isEmpty()) { if (envMap != null && !envMap.isEmpty()) {
planEnvMap = envMap; planEnvMap = envMap;
@ -92,7 +92,11 @@ public class ApiJmeterFileService {
planEnvMap = environmentGroupProjectService.getEnvMap(envGroupId); planEnvMap = environmentGroupProjectService.getEnvMap(envGroupId);
} }
} }
hashTree = GenerateHashTreeUtil.generateHashTree(scenario, reportId, planEnvMap, reportType); try {
hashTree = GenerateHashTreeUtil.generateHashTree(scenario, reportId, planEnvMap, reportType);
} catch (Exception e) {
executionQueueDetailMapper.deleteByPrimaryKey(queueId);
}
} }
return zipFilesToByteArray((reportId + "_" + remoteTestId), hashTree); return zipFilesToByteArray((reportId + "_" + remoteTestId), hashTree);
} }

View File

@ -8,7 +8,6 @@ import io.metersphere.commons.constants.ApiRunMode;
import io.metersphere.commons.utils.BeanUtils; import io.metersphere.commons.utils.BeanUtils;
import io.metersphere.commons.utils.CommonBeanFactory; import io.metersphere.commons.utils.CommonBeanFactory;
import io.metersphere.commons.utils.LogUtil; import io.metersphere.commons.utils.LogUtil;
import io.metersphere.constants.RunModeConstants;
import io.metersphere.dto.JmeterRunRequestDTO; import io.metersphere.dto.JmeterRunRequestDTO;
import io.metersphere.dto.ResultDTO; import io.metersphere.dto.ResultDTO;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
@ -109,16 +108,14 @@ public class RemakeReportService {
} }
} }
// 处理队列 // 处理队列
if (StringUtils.equals(request.getReportType(), RunModeConstants.SET_REPORT.toString())) { ApiExecutionQueueDetailExample example = new ApiExecutionQueueDetailExample();
ApiExecutionQueueDetailExample example = new ApiExecutionQueueDetailExample(); example.createCriteria().andQueueIdEqualTo(request.getQueueId()).andTestIdEqualTo(request.getTestId());
example.createCriteria().andQueueIdEqualTo(request.getQueueId()).andTestIdEqualTo(request.getTestId()); CommonBeanFactory.getBean(ApiExecutionQueueDetailMapper.class).deleteByExample(example);
CommonBeanFactory.getBean(ApiExecutionQueueDetailMapper.class).deleteByExample(example); ResultDTO dto = new ResultDTO();
ResultDTO dto = new ResultDTO(); BeanUtils.copyBean(dto, request);
BeanUtils.copyBean(dto, request); dto.setQueueId(request.getQueueId());
dto.setQueueId(request.getQueueId()); dto.setTestId(request.getTestId());
dto.setTestId(request.getTestId()); CommonBeanFactory.getBean(ApiExecutionQueueService.class).queueNext(dto);
CommonBeanFactory.getBean(ApiExecutionQueueService.class).queueNext(dto);
}
} catch (Exception e) { } catch (Exception e) {
LogUtil.error(e); LogUtil.error(e);
} }