fix(接口测试): 场景定时任务执行报错

This commit is contained in:
AgAngle 2024-11-18 19:02:16 +08:00 committed by Craftsman
parent 481336a518
commit fb14703c3a
1 changed files with 18 additions and 57 deletions

View File

@ -1,18 +1,16 @@
package io.metersphere.api.job; package io.metersphere.api.job;
import io.metersphere.api.domain.ApiScenarioReport; import io.metersphere.api.domain.ApiScenario;
import io.metersphere.api.dto.ApiScenarioParamConfig; import io.metersphere.api.mapper.ApiScenarioMapper;
import io.metersphere.api.dto.ApiScenarioParseTmpParam;
import io.metersphere.api.dto.debug.ApiResourceRunRequest;
import io.metersphere.api.dto.request.MsScenario;
import io.metersphere.api.dto.scenario.ApiScenarioDetail;
import io.metersphere.api.dto.scenario.ApiScenarioParseParam;
import io.metersphere.api.service.ApiCommonService; import io.metersphere.api.service.ApiCommonService;
import io.metersphere.api.service.ApiExecuteService; import io.metersphere.api.service.ApiExecuteService;
import io.metersphere.api.service.scenario.ApiScenarioRunService; import io.metersphere.api.service.scenario.ApiScenarioRunService;
import io.metersphere.project.domain.Project; import io.metersphere.project.domain.Project;
import io.metersphere.project.mapper.ProjectMapper; import io.metersphere.project.mapper.ProjectMapper;
import io.metersphere.sdk.constants.*; import io.metersphere.sdk.constants.ApiExecuteResourceType;
import io.metersphere.sdk.constants.ApiExecuteRunMode;
import io.metersphere.sdk.constants.ExecTaskType;
import io.metersphere.sdk.constants.TaskTriggerMode;
import io.metersphere.sdk.dto.api.task.ApiRunModeConfigDTO; import io.metersphere.sdk.dto.api.task.ApiRunModeConfigDTO;
import io.metersphere.sdk.dto.api.task.TaskInfo; import io.metersphere.sdk.dto.api.task.TaskInfo;
import io.metersphere.sdk.dto.api.task.TaskItem; import io.metersphere.sdk.dto.api.task.TaskItem;
@ -24,8 +22,6 @@ import io.metersphere.system.domain.ExecTask;
import io.metersphere.system.domain.ExecTaskItem; import io.metersphere.system.domain.ExecTaskItem;
import io.metersphere.system.schedule.BaseScheduleJob; import io.metersphere.system.schedule.BaseScheduleJob;
import io.metersphere.system.service.BaseTaskHubService; import io.metersphere.system.service.BaseTaskHubService;
import io.metersphere.system.uid.IDGenerator;
import org.apache.commons.lang3.StringUtils;
import org.quartz.JobExecutionContext; import org.quartz.JobExecutionContext;
import org.quartz.JobKey; import org.quartz.JobKey;
import org.quartz.TriggerKey; import org.quartz.TriggerKey;
@ -35,40 +31,22 @@ public class ApiScenarioScheduleJob extends BaseScheduleJob {
protected void businessExecute(JobExecutionContext context) { protected void businessExecute(JobExecutionContext context) {
ApiExecuteService apiExecuteService = CommonBeanFactory.getBean(ApiExecuteService.class); ApiExecuteService apiExecuteService = CommonBeanFactory.getBean(ApiExecuteService.class);
ApiScenarioRunService apiScenarioRunService = CommonBeanFactory.getBean(ApiScenarioRunService.class); ApiScenarioRunService apiScenarioRunService = CommonBeanFactory.getBean(ApiScenarioRunService.class);
ApiScenarioMapper apiScenarioMapper = CommonBeanFactory.getBean(ApiScenarioMapper.class);
ApiCommonService apiCommonService = CommonBeanFactory.getBean(ApiCommonService.class); ApiCommonService apiCommonService = CommonBeanFactory.getBean(ApiCommonService.class);
ProjectMapper projectMapper = CommonBeanFactory.getBean(ProjectMapper.class); ProjectMapper projectMapper = CommonBeanFactory.getBean(ProjectMapper.class);
BaseTaskHubService baseTaskHubService = CommonBeanFactory.getBean(BaseTaskHubService.class); BaseTaskHubService baseTaskHubService = CommonBeanFactory.getBean(BaseTaskHubService.class);
ApiRunModeConfigDTO apiRunModeConfigDTO = JSON.parseObject(context.getJobDetail().getJobDataMap().get("config").toString(), ApiRunModeConfigDTO.class); ApiRunModeConfigDTO apiRunModeConfigDTO = JSON.parseObject(context.getJobDetail().getJobDataMap().get("config").toString(), ApiRunModeConfigDTO.class);
ApiScenarioDetail apiScenarioDetail = apiScenarioRunService.getForRun(resourceId); ApiScenario apiScenario = apiScenarioMapper.selectByPrimaryKey(resourceId);
if (apiScenarioDetail == null) { if (apiScenario == null) {
LogUtils.info("当前定时任务的场景已删除 {}", resourceId); LogUtils.info("当前定时任务的场景已删除 {}", resourceId);
return; return;
} }
MsScenario msScenario = apiScenarioRunService.getMsScenario(apiScenarioDetail);
ApiScenarioParseParam parseParam = apiScenarioRunService.getApiScenarioParseParam(apiScenarioDetail);
parseParam.setEnvironmentId(apiRunModeConfigDTO.getEnvironmentId());
parseParam.setGrouped(apiRunModeConfigDTO.getGrouped());
if (StringUtils.isBlank(apiRunModeConfigDTO.getEnvironmentId())) { Project project = projectMapper.selectByPrimaryKey(apiScenario.getProjectId());
parseParam.setEnvironmentId(apiScenarioDetail.getEnvironmentId());
parseParam.setGrouped(apiScenarioDetail.getGrouped());
}
if (StringUtils.isBlank(apiRunModeConfigDTO.getPoolId())) {
apiRunModeConfigDTO.setPoolId(apiExecuteService.getProjectApiResourcePoolId(apiScenarioDetail.getProjectId()));
}
msScenario.setResourceId(apiScenarioDetail.getId());
// 解析生成场景树并保存临时变量
ApiScenarioParseTmpParam tmpParam = apiScenarioRunService.parse(msScenario, apiScenarioDetail.getSteps(), parseParam);
ApiResourceRunRequest runRequest = apiScenarioRunService.getApiResourceRunRequest(msScenario, tmpParam);
Project project = projectMapper.selectByPrimaryKey(apiScenarioDetail.getProjectId());
ExecTask execTask = apiCommonService.newExecTask(project.getId(), userId); ExecTask execTask = apiCommonService.newExecTask(project.getId(), userId);
execTask.setCaseCount(1L); execTask.setCaseCount(1L);
execTask.setTaskName(apiScenarioDetail.getName()); execTask.setTaskName(apiScenario.getName());
execTask.setOrganizationId(project.getOrganizationId()); execTask.setOrganizationId(project.getOrganizationId());
execTask.setTriggerMode(TaskTriggerMode.SCHEDULE.name()); execTask.setTriggerMode(TaskTriggerMode.SCHEDULE.name());
execTask.setTaskType(ExecTaskType.API_SCENARIO.name()); execTask.setTaskType(ExecTaskType.API_SCENARIO.name());
@ -76,12 +54,12 @@ public class ApiScenarioScheduleJob extends BaseScheduleJob {
ExecTaskItem execTaskItem = apiCommonService.newExecTaskItem(execTask.getId(), project.getId(), userId); ExecTaskItem execTaskItem = apiCommonService.newExecTaskItem(execTask.getId(), project.getId(), userId);
execTaskItem.setOrganizationId(project.getOrganizationId()); execTaskItem.setOrganizationId(project.getOrganizationId());
execTaskItem.setResourceType(ApiExecuteResourceType.API_SCENARIO.name()); execTaskItem.setResourceType(ApiExecuteResourceType.API_SCENARIO.name());
execTaskItem.setResourceId(apiScenarioDetail.getId()); execTaskItem.setResourceId(apiScenario.getId());
execTaskItem.setCaseId(apiScenarioDetail.getId()); execTaskItem.setCaseId(apiScenario.getId());
execTaskItem.setResourceName(apiScenarioDetail.getName()); execTaskItem.setResourceName(apiScenario.getName());
baseTaskHubService.insertExecTaskAndDetail(execTask, execTaskItem); baseTaskHubService.insertExecTaskAndDetail(execTask, execTaskItem);
TaskRequestDTO taskRequest = apiScenarioRunService.getTaskRequest(IDGenerator.nextStr(), apiScenarioDetail.getId(), apiScenarioDetail.getProjectId(), ApiExecuteRunMode.SCHEDULE.name()); TaskRequestDTO taskRequest = apiScenarioRunService.getTaskRequest(null, apiScenario.getId(), apiScenario.getProjectId(), ApiExecuteRunMode.SCHEDULE.name());
TaskInfo taskInfo = taskRequest.getTaskInfo(); TaskInfo taskInfo = taskRequest.getTaskInfo();
TaskItem taskItem = taskRequest.getTaskItem(); TaskItem taskItem = taskRequest.getTaskItem();
taskInfo.getRunModeConfig().setPoolId(apiRunModeConfigDTO.getPoolId()); taskInfo.getRunModeConfig().setPoolId(apiRunModeConfigDTO.getPoolId());
@ -89,28 +67,11 @@ public class ApiScenarioScheduleJob extends BaseScheduleJob {
taskInfo.setSaveResult(true); taskInfo.setSaveResult(true);
taskInfo.setRealTime(false); taskInfo.setRealTime(false);
taskInfo.setUserId(userId); taskInfo.setUserId(userId);
taskInfo.getRunModeConfig().setEnvironmentId(parseParam.getEnvironmentId()); taskInfo.getRunModeConfig().setEnvironmentId(apiRunModeConfigDTO.getEnvironmentId());
taskItem.setRequestCount(tmpParam.getRequestCount().get()); taskInfo.getRunModeConfig().setGrouped(apiRunModeConfigDTO.getGrouped());
taskItem.setId(execTaskItem.getId()); taskItem.setId(execTaskItem.getId());
ApiScenarioParamConfig parseConfig = apiScenarioRunService.getApiScenarioParamConfig(msScenario.getProjectId(), parseParam, tmpParam.getScenarioParseEnvInfo()); apiExecuteService.execute(taskRequest);
parseConfig.setTaskItemId(taskItem.getId());
// 初始化报告
ApiScenarioReport scenarioReport = apiScenarioRunService.getScenarioReport(userId);
scenarioReport.setId(taskItem.getReportId());
scenarioReport.setTriggerMode(TaskTriggerMode.SCHEDULE.name());
scenarioReport.setRunMode(ApiBatchRunMode.PARALLEL.name());
scenarioReport.setPoolId(apiRunModeConfigDTO.getPoolId());
scenarioReport.setEnvironmentId(parseParam.getEnvironmentId());
scenarioReport.setWaitingTime(apiScenarioRunService.getGlobalWaitTime(parseParam.getScenarioConfig()));
apiScenarioRunService.initApiScenarioReport(taskItem.getId(), apiScenarioDetail, scenarioReport);
// 初始化报告步骤
apiScenarioRunService.initScenarioReportSteps(apiScenarioDetail.getSteps(), taskItem.getReportId());
apiExecuteService.execute(runRequest, taskRequest, parseConfig);
} }