fix(接口测试): 资源池禁用,任务项没有错误提示

--bug=1049106 --user=陈建星 【任务中心】-禁用测试计划使用的资源池后,执行测试计划,执行完成后,任务详情的排队状态没有异常提示 https://www.tapd.cn/55049933/s/1616375
This commit is contained in:
AgAngle 2024-11-25 14:50:30 +08:00
parent 66551a3b4f
commit 73ac20c2ab
2 changed files with 49 additions and 26 deletions

View File

@ -35,6 +35,7 @@ import io.metersphere.sdk.constants.TaskItemErrorMessage;
import io.metersphere.sdk.dto.api.task.GetRunScriptRequest;
import io.metersphere.sdk.dto.api.task.TaskBatchRequestDTO;
import io.metersphere.sdk.dto.api.task.TaskItem;
import io.metersphere.sdk.dto.api.task.TaskRequestDTO;
import io.metersphere.sdk.exception.MSException;
import io.metersphere.sdk.util.BeanUtils;
import io.metersphere.sdk.util.CommonBeanFactory;
@ -552,6 +553,16 @@ public class ApiCommonService {
}
}
@Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRES_NEW)
public void batchUpdateTaskItemErrorMassage(TaskItemErrorMessage errorMessage, TaskRequestDTO taskRequestDTO) {
// 更新任务项的异常信息
ExecTaskItem execTaskItem = new ExecTaskItem();
execTaskItem.setId(taskRequestDTO.getTaskItem().getId());
execTaskItem.setErrorMessage(errorMessage.name());
execTaskItemMapper.updateByPrimaryKeySelective(execTaskItem);
}
@Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRES_NEW)
public void updateTaskItemErrorMassage(String taskItemId, TaskItemErrorMessage errorMessage) {
// 更新任务项的异常信息

View File

@ -32,6 +32,7 @@ import io.metersphere.project.dto.environment.http.SelectModule;
import io.metersphere.project.service.*;
import io.metersphere.sdk.constants.*;
import io.metersphere.sdk.dto.api.task.*;
import io.metersphere.sdk.exception.IResultCode;
import io.metersphere.sdk.exception.MSException;
import io.metersphere.sdk.exception.TaskRunnerResultCode;
import io.metersphere.sdk.file.FileCenter;
@ -264,6 +265,10 @@ public class ApiExecuteService {
throw new MSException(RESOURCE_POOL_EXECUTE_ERROR, e.getMessage());
} catch (MSException e) {
LogUtils.error(e);
IResultCode errorCode = e.getErrorCode();
if (errorCode == ApiResultCode.EXECUTE_RESOURCE_POOL_NOT_CONFIG) {
apiCommonService.batchUpdateTaskItemErrorMassage(TaskItemErrorMessage.INVALID_RESOURCE_POOL, taskRequest);
}
throw e;
} catch (Exception e) {
LogUtils.error(e);
@ -373,29 +378,41 @@ public class ApiExecuteService {
*/
public void batchExecute(TaskBatchRequestDTO taskRequest) {
setTaskRequestParams(taskRequest.getTaskInfo());
TaskInfo taskInfo = taskRequest.getTaskInfo();
// 获取资源池
TestResourcePoolReturnDTO testResourcePool = getGetResourcePoolNodeDTO(taskInfo.getRunModeConfig(), taskInfo.getProjectId());
if (StringUtils.isNotBlank(testResourcePool.getServerUrl())) {
// 如果资源池配置了当前站点则使用资源池的
taskInfo.setMsUrl(testResourcePool.getServerUrl());
}
taskInfo.setPoolId(testResourcePool.getId());
taskRequest.getTaskItems().forEach(taskItem -> {
if (StringUtils.isBlank(taskItem.getReportId())) {
// 预先生成报告ID避免资源池获取执行脚本时超时重试导致数据重复创建
taskItem.setReportId(IDGenerator.nextStr());
try {
// 获取资源池
TestResourcePoolReturnDTO testResourcePool = getGetResourcePoolNodeDTO(taskInfo.getRunModeConfig(), taskInfo.getProjectId());
if (StringUtils.isNotBlank(testResourcePool.getServerUrl())) {
// 如果资源池配置了当前站点则使用资源池的
taskInfo.setMsUrl(testResourcePool.getServerUrl());
}
});
taskInfo.setPoolId(testResourcePool.getId());
taskRequest.getTaskItems().forEach(taskItem -> {
if (StringUtils.isBlank(taskItem.getReportId())) {
// 预先生成报告ID避免资源池获取执行脚本时超时重试导致数据重复创建
taskItem.setReportId(IDGenerator.nextStr());
}
});
// 判断是否为 K8S 资源池
boolean isK8SResourcePool = StringUtils.equals(testResourcePool.getType(), ResourcePoolTypeEnum.K8S.getName());
if (isK8SResourcePool) {
k8sBatchExecute(taskRequest, taskInfo, testResourcePool);
} else {
nodeBatchExecute(taskRequest, taskInfo, testResourcePool);
// 判断是否为 K8S 资源池
boolean isK8SResourcePool = StringUtils.equals(testResourcePool.getType(), ResourcePoolTypeEnum.K8S.getName());
if (isK8SResourcePool) {
k8sBatchExecute(taskRequest, taskInfo, testResourcePool);
} else {
nodeBatchExecute(taskRequest, taskInfo, testResourcePool);
}
} catch (MSException e) {
LogUtils.error(e);
IResultCode errorCode = e.getErrorCode();
if (errorCode == ApiResultCode.EXECUTE_RESOURCE_POOL_NOT_CONFIG) {
apiCommonService.batchUpdateTaskItemErrorMassage(TaskItemErrorMessage.INVALID_RESOURCE_POOL, taskRequest);
}
throw e;
} catch (Exception e) {
LogUtils.error(e);
throw new MSException(RESOURCE_POOL_EXECUTE_ERROR, e.getMessage());
}
}
@ -463,19 +480,14 @@ public class ApiExecuteService {
}
}
private void k8sBatchExecute(TaskBatchRequestDTO taskRequest, TaskInfo taskInfo, TestResourcePoolReturnDTO testResourcePool) {
private void k8sBatchExecute(TaskBatchRequestDTO taskRequest, TaskInfo taskInfo, TestResourcePoolReturnDTO testResourcePool) throws Exception {
TestResourceDTO testResourceDTO = new TestResourceDTO();
BeanUtils.copyBean(testResourceDTO, testResourcePool.getTestResourceReturnDTO());
testResourceDTO.setId(testResourcePool.getId());
taskInfo.setPoolSize(testResourceDTO.getConcurrentNumber());
taskInfo.setPerTaskSize(testResourceDTO.getPodThreads());
try {
EngineFactory.batchRunApi(taskRequest, testResourceDTO);
} catch (Exception e) {
LogUtils.error(e);
apiCommonService.batchUpdateTaskItemErrorMassage(TaskItemErrorMessage.INVALID_RESOURCE_POOL, taskRequest);
}
EngineFactory.batchRunApi(taskRequest, testResourceDTO);
}
private List<TaskBatchRequestDTO> getDistributeTaskBatchRequest(TaskBatchRequestDTO taskRequest, int distributeSize) {