feat(接口测试): 单接口用例执行任务,重跑逻辑

--task=1016918 --user=陈建星 失败重跑 https://www.tapd.cn/55049933/s/1609836
This commit is contained in:
AgAngle 2024-11-14 19:49:36 +08:00 committed by Craftsman
parent 9bd017a800
commit 6c9cccf0ab
18 changed files with 239 additions and 98 deletions

View File

@ -68,4 +68,8 @@ public class GetRunScriptRequest implements Serializable {
* {@link io.metersphere.sdk.constants.TaskTriggerMode} * {@link io.metersphere.sdk.constants.TaskTriggerMode}
*/ */
private String triggerMode; private String triggerMode;
/**
* 是否是任务失败重跑
*/
private Boolean rerun;
} }

View File

@ -124,4 +124,8 @@ public class TaskInfo implements Serializable {
* 执行时初始化报告需要记录资源池ID * 执行时初始化报告需要记录资源池ID
*/ */
private String poolId; private String poolId;
/**
* 是否是任务失败重跑
*/
private Boolean rerun;
} }

View File

@ -70,14 +70,10 @@ public class ApiCaseExecuteCallbackService implements ApiExecuteCallbackService
String reportId = taskItem.getReportId(); String reportId = taskItem.getReportId();
if (BooleanUtils.isTrue(request.getBatch()) && !request.getRunModeConfig().isIntegratedReport()) { if (BooleanUtils.isTrue(request.getBatch()) && !request.getRunModeConfig().isIntegratedReport()) {
// 批量执行生成独立报告 // 批量执行生成独立报告
ApiTestCaseRecord apiTestCaseRecord = apiTestCaseBatchRunService.initApiReport(taskItem.getId(), request.getRunModeConfig(), apiTestCase, request.getUserId()); reportId = apiTestCaseBatchRunService.initApiReport(taskItem.getId(), request.getRunModeConfig(), apiTestCase, request.getUserId());
reportId = apiTestCaseRecord.getApiReportId();
} else if (BooleanUtils.isFalse(request.getBatch()) && !ApiExecuteRunMode.isDebug(request.getRunMode())) { } else if (BooleanUtils.isFalse(request.getBatch()) && !ApiExecuteRunMode.isDebug(request.getRunMode())) {
// 单用例执行非调试生成报告 // 单用例执行非调试生成报告
if (StringUtils.isBlank(taskItem.getReportId())) { return apiTestCaseRunService.initApiReport(taskItem.getId(), apiTestCase, request);
reportId = IDGenerator.nextStr();
}
apiTestCaseRunService.initApiReport(taskItem.getId(), apiTestCase, request);
} }
return reportId; return reportId;
} }

View File

@ -1,26 +0,0 @@
package io.metersphere.api.service;
import io.metersphere.sdk.constants.ExecTaskType;
import io.metersphere.system.domain.ExecTask;
import io.metersphere.system.invoker.TaskRerunServiceInvoker;
import io.metersphere.system.service.TaskRerunService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
/**
* @Author: jianxing
* @CreateTime: 2024-02-06 20:47
*/
@Service
@Transactional(rollbackFor = Exception.class)
public class ApiCaseRerunService implements TaskRerunService {
public ApiCaseRerunService() {
TaskRerunServiceInvoker.register(ExecTaskType.API_CASE, this);
}
@Override
public void rerun(ExecTask execTask) {
// todo
}
}

View File

@ -10,7 +10,6 @@ import io.metersphere.sdk.dto.api.task.GetRunScriptRequest;
import io.metersphere.sdk.dto.api.task.GetRunScriptResult; import io.metersphere.sdk.dto.api.task.GetRunScriptResult;
import io.metersphere.sdk.dto.api.task.TaskItem; import io.metersphere.sdk.dto.api.task.TaskItem;
import io.metersphere.sdk.exception.MSException; import io.metersphere.sdk.exception.MSException;
import io.metersphere.sdk.util.CommonBeanFactory;
import io.metersphere.sdk.util.EnumValidator; import io.metersphere.sdk.util.EnumValidator;
import io.metersphere.sdk.util.LogUtils; import io.metersphere.sdk.util.LogUtils;
import jakarta.annotation.Resource; import jakarta.annotation.Resource;
@ -71,15 +70,18 @@ public class ApiExecuteResourceService {
String reportId = taskItem.getReportId(); String reportId = taskItem.getReportId();
ApiExecuteResourceType apiExecuteResourceType = EnumValidator.validateEnum(ApiExecuteResourceType.class, request.getResourceType()); ApiExecuteResourceType apiExecuteResourceType = EnumValidator.validateEnum(ApiExecuteResourceType.class, request.getResourceType());
if (request.getBatch()) { // 重跑不更新任务状态
// 设置缓存成功说明是第一个任务则设置任务的开始时间和运行状态 if (BooleanUtils.isFalse(request.getRerun())) {
if (taskRunningCache.setIfAbsent(taskId)) { if (request.getBatch()) {
// 将任务状态更新为运行中 // 设置缓存成功说明是第一个任务则设置任务的开始时间和运行状态
if (taskRunningCache.setIfAbsent(taskId)) {
// 将任务状态更新为运行中
apiCommonService.updateTaskRunningStatus(taskId);
}
} else {
// 非批量时直接更新任务状态
apiCommonService.updateTaskRunningStatus(taskId); apiCommonService.updateTaskRunningStatus(taskId);
} }
} else {
// 非批量时直接更新任务状态
apiCommonService.updateTaskRunningStatus(taskId);
} }
// 更新任务项状态 // 更新任务项状态

View File

@ -866,6 +866,7 @@ public class ApiExecuteService {
TaskItem taskItem = new TaskItem(); TaskItem taskItem = new TaskItem();
taskItem.setReportId(reportId); taskItem.setReportId(reportId);
taskItem.setResourceId(resourceId); taskItem.setResourceId(resourceId);
taskItem.setRequestCount(1L);
return taskItem; return taskItem;
} }

View File

@ -1,26 +0,0 @@
package io.metersphere.api.service;
import io.metersphere.sdk.constants.ExecTaskType;
import io.metersphere.system.domain.ExecTask;
import io.metersphere.system.invoker.TaskRerunServiceInvoker;
import io.metersphere.system.service.TaskRerunService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
/**
* @Author: jianxing
* @CreateTime: 2024-02-06 20:47
*/
@Service
@Transactional(rollbackFor = Exception.class)
public class ApiScenarioRerunService implements TaskRerunService {
public ApiScenarioRerunService() {
TaskRerunServiceInvoker.register(ExecTaskType.API_SCENARIO, this);
}
@Override
public void rerun(ExecTask execTask) {
// todo
}
}

View File

@ -404,7 +404,7 @@ public class ApiTestCaseBatchRunService {
* @param apiTestCase * @param apiTestCase
* @return * @return
*/ */
public ApiTestCaseRecord initApiReport(String taskItemId, ApiRunModeConfigDTO runModeConfig, public String initApiReport(String taskItemId, ApiRunModeConfigDTO runModeConfig,
ApiTestCase apiTestCase, String userId) { ApiTestCase apiTestCase, String userId) {
// 初始化报告 // 初始化报告
ApiReport apiReport = getApiReport(runModeConfig, apiTestCase, userId); ApiReport apiReport = getApiReport(runModeConfig, apiTestCase, userId);

View File

@ -83,6 +83,31 @@ public class ApiTestCaseRunService {
return executeRun(runRequest, apiTestCase, reportId, userId); return executeRun(runRequest, apiTestCase, reportId, userId);
} }
/**
* 任务重跑
*
* @param userId
* @return
*/
public TaskRequestDTO runRun(ExecTask execTask, ExecTaskItem execTaskItem, String userId) {
String id = execTaskItem.getResourceId();
ApiTestCase apiTestCase = apiTestCaseService.checkResourceExist(id);
String poolId = apiExecuteService.getProjectApiResourcePoolId(apiTestCase.getProjectId());
TaskRequestDTO taskRequest = getTaskRequest(null, id, apiTestCase.getProjectId(), ApiExecuteRunMode.RUN.name());
TaskItem taskItem = taskRequest.getTaskItem();
TaskInfo taskInfo = taskRequest.getTaskInfo();
taskInfo.getRunModeConfig().setPoolId(poolId);
taskInfo.setSaveResult(true);
taskInfo.setUserId(userId);
taskInfo.setTaskId(execTask.getId());
taskInfo.setNeedParseScript(true);
taskInfo.setRerun(true);
taskItem.setId(execTaskItem.getId());
return apiExecuteService.execute(taskRequest);
}
/** /**
* 接口执行 * 接口执行
* 保存报告 * 保存报告
@ -97,20 +122,8 @@ public class ApiTestCaseRunService {
String poolId = apiExecuteService.getProjectApiResourcePoolId(apiTestCase.getProjectId()); String poolId = apiExecuteService.getProjectApiResourcePoolId(apiTestCase.getProjectId());
Project project = projectMapper.selectByPrimaryKey(apiTestCase.getProjectId()); Project project = projectMapper.selectByPrimaryKey(apiTestCase.getProjectId());
ExecTask execTask = apiCommonService.newExecTask(project.getId(), userId); ExecTask execTask = newExecTask(apiTestCase, userId, project);
execTask.setCaseCount(1L); ExecTaskItem execTaskItem = newExecTaskItem(apiTestCase, userId, project, execTask.getId());
execTask.setTaskName(apiTestCase.getName());
execTask.setOrganizationId(project.getOrganizationId());
execTask.setTriggerMode(TaskTriggerMode.MANUAL.name());
execTask.setTaskType(ExecTaskType.API_CASE.name());
ExecTaskItem execTaskItem = apiCommonService.newExecTaskItem(execTask.getId(), project.getId(), userId);
execTaskItem.setOrganizationId(project.getOrganizationId());
execTaskItem.setResourceType(ApiExecuteResourceType.API_CASE.name());
execTaskItem.setResourceId(apiTestCase.getId());
execTaskItem.setCaseId(apiTestCase.getId());
execTaskItem.setResourceName(apiTestCase.getName());
baseTaskHubService.insertExecTaskAndDetail(execTask, execTaskItem); baseTaskHubService.insertExecTaskAndDetail(execTask, execTaskItem);
TaskRequestDTO taskRequest = getTaskRequest(reportId, apiTestCase.getId(), apiTestCase.getProjectId(), ApiExecuteRunMode.RUN.name()); TaskRequestDTO taskRequest = getTaskRequest(reportId, apiTestCase.getId(), apiTestCase.getProjectId(), ApiExecuteRunMode.RUN.name());
@ -133,6 +146,26 @@ public class ApiTestCaseRunService {
return doExecute(taskRequest, runRequest, apiTestCase.getApiDefinitionId(), apiTestCase.getEnvironmentId()); return doExecute(taskRequest, runRequest, apiTestCase.getApiDefinitionId(), apiTestCase.getEnvironmentId());
} }
private ExecTaskItem newExecTaskItem(ApiTestCase apiTestCase, String userId, Project project, String taskId) {
ExecTaskItem execTaskItem = apiCommonService.newExecTaskItem(taskId, project.getId(), userId);
execTaskItem.setOrganizationId(project.getOrganizationId());
execTaskItem.setResourceType(ApiExecuteResourceType.API_CASE.name());
execTaskItem.setResourceId(apiTestCase.getId());
execTaskItem.setCaseId(apiTestCase.getId());
execTaskItem.setResourceName(apiTestCase.getName());
return execTaskItem;
}
private ExecTask newExecTask(ApiTestCase apiTestCase, String userId, Project project) {
ExecTask execTask = apiCommonService.newExecTask(project.getId(), userId);
execTask.setCaseCount(1L);
execTask.setTaskName(apiTestCase.getName());
execTask.setOrganizationId(project.getOrganizationId());
execTask.setTriggerMode(TaskTriggerMode.MANUAL.name());
execTask.setTaskType(ExecTaskType.API_CASE.name());
return execTask;
}
/** /**
* 接口调试 * 接口调试
* 不存报告实时获取结果 * 不存报告实时获取结果
@ -219,11 +252,14 @@ public class ApiTestCaseRunService {
* @param apiTestCase * @param apiTestCase
* @return * @return
*/ */
public ApiTestCaseRecord initApiReport(String taskItemId, ApiTestCase apiTestCase, GetRunScriptRequest request) { public String initApiReport(String taskItemId, ApiTestCase apiTestCase, GetRunScriptRequest request) {
// 初始化报告 // 初始化报告
ApiReport apiReport = getApiReport(apiTestCase, request); ApiReport apiReport = getApiReport(apiTestCase, request);
if (StringUtils.isBlank(apiReport.getId())) {
apiReport.setId(IDGenerator.nextStr());
}
apiReportService.insertApiReport(apiReport); apiReportService.insertApiReport(apiReport);
return initApiReportDetail(taskItemId, apiTestCase, request.getTaskItem().getReportId()); return initApiReportDetail(taskItemId, apiTestCase, apiReport.getId());
} }
/** /**
@ -232,7 +268,7 @@ public class ApiTestCaseRunService {
* @param apiTestCase * @param apiTestCase
* @return * @return
*/ */
public ApiTestCaseRecord initApiReportDetail(String taskItemId, ApiTestCase apiTestCase, String reportId) { public String initApiReportDetail(String taskItemId, ApiTestCase apiTestCase, String reportId) {
// 初始化步骤 // 初始化步骤
ApiReportStep apiReportStep = getApiReportStep(apiTestCase, reportId, 1); ApiReportStep apiReportStep = getApiReportStep(apiTestCase, reportId, 1);
// 初始化报告和用例的关联关系 // 初始化报告和用例的关联关系
@ -241,7 +277,7 @@ public class ApiTestCaseRunService {
ApiReportRelateTask apiReportRelateTask = apiCommonService.getApiReportRelateTask(taskItemId, reportId); ApiReportRelateTask apiReportRelateTask = apiCommonService.getApiReportRelateTask(taskItemId, reportId);
apiReportService.insertApiReportDetail(apiReportStep, apiTestCaseRecord, apiReportRelateTask); apiReportService.insertApiReportDetail(apiReportStep, apiTestCaseRecord, apiReportRelateTask);
return apiTestCaseRecord; return apiTestCaseRecord.getApiReportId();
} }
public ApiReport getApiReport(ApiTestCase apiTestCase, GetRunScriptRequest request) { public ApiReport getApiReport(ApiTestCase apiTestCase, GetRunScriptRequest request) {

View File

@ -0,0 +1,37 @@
package io.metersphere.api.service.rerun;
import io.metersphere.api.service.definition.ApiTestCaseRunService;
import io.metersphere.sdk.constants.ExecTaskType;
import io.metersphere.system.domain.ExecTask;
import io.metersphere.system.domain.ExecTaskItem;
import io.metersphere.system.invoker.TaskRerunServiceInvoker;
import io.metersphere.system.mapper.ExecTaskItemMapper;
import io.metersphere.system.service.TaskRerunService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
/**
* @Author: jianxing
* @CreateTime: 2024-02-06 20:47
*/
@Service
@Transactional(rollbackFor = Exception.class)
public class ApiCaseRerunService implements TaskRerunService {
@Resource
private ApiTestCaseRunService apiTestCaseRunService;
@Resource
private ExecTaskItemMapper execTaskItemMapper;
public ApiCaseRerunService() {
TaskRerunServiceInvoker.register(ExecTaskType.API_CASE, this);
}
@Override
public void rerun(ExecTask execTask, List<String> taskItemIds, String userId) {
ExecTaskItem execTaskItem = execTaskItemMapper.selectByPrimaryKey(taskItemIds.getFirst());
apiTestCaseRunService.runRun(execTask, execTaskItem, userId);
}
}

View File

@ -0,0 +1,37 @@
package io.metersphere.api.service.rerun;
import io.metersphere.api.service.scenario.ApiScenarioRunService;
import io.metersphere.sdk.constants.ExecTaskType;
import io.metersphere.system.domain.ExecTask;
import io.metersphere.system.domain.ExecTaskItem;
import io.metersphere.system.invoker.TaskRerunServiceInvoker;
import io.metersphere.system.mapper.ExecTaskItemMapper;
import io.metersphere.system.service.TaskRerunService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
/**
* @Author: jianxing
* @CreateTime: 2024-02-06 20:47
*/
@Service
@Transactional(rollbackFor = Exception.class)
public class ApiScenarioRerunService implements TaskRerunService {
@Resource
private ApiScenarioRunService apiScenarioRunService;
@Resource
private ExecTaskItemMapper execTaskItemMapper;
public ApiScenarioRerunService() {
TaskRerunServiceInvoker.register(ExecTaskType.API_SCENARIO, this);
}
@Override
public void rerun(ExecTask execTask, List<String> taskItemIds, String userId) {
ExecTaskItem execTaskItem = execTaskItemMapper.selectByPrimaryKey(taskItemIds.getFirst());
apiScenarioRunService.runRun(execTask, execTaskItem, userId);
}
}

View File

@ -467,8 +467,9 @@ public class ApiScenarioRunService {
public String initApiScenarioReport(String taskItemId, ApiScenario apiScenario, GetRunScriptRequest request) { public String initApiScenarioReport(String taskItemId, ApiScenario apiScenario, GetRunScriptRequest request) {
// 初始化报告 // 初始化报告
ApiScenarioReport scenarioReport = getScenarioReport(apiScenario, request); ApiScenarioReport scenarioReport = getScenarioReport(apiScenario, request);
scenarioReport.setStartTime(System.currentTimeMillis());
apiScenarioReportService.insertApiScenarioReport(scenarioReport); apiScenarioReportService.insertApiScenarioReport(scenarioReport);
return initApiScenarioReportDetail(taskItemId, apiScenario.getId(), request.getTaskItem().getReportId()); return initApiScenarioReportDetail(taskItemId, apiScenario.getId(), scenarioReport.getId());
} }
/** /**
@ -1096,4 +1097,23 @@ public class ApiScenarioRunService {
} }
return projectEnvMap; return projectEnvMap;
} }
public void runRun(ExecTask execTask, ExecTaskItem execTaskItem, String userId) {
ApiScenario apiScenario = apiScenarioService.checkResourceIsNoDeleted(execTaskItem.getResourceId());
String poolId = apiExecuteService.getProjectApiResourcePoolId(apiScenario.getProjectId());
TaskRequestDTO taskRequest = getTaskRequest(null, apiScenario.getId(), apiScenario.getProjectId(), ApiExecuteRunMode.RUN.name());
TaskInfo taskInfo = taskRequest.getTaskInfo();
TaskItem taskItem = taskRequest.getTaskItem();
taskItem.setId(execTaskItem.getId());
taskInfo.setTaskId(execTask.getId());
taskInfo.getRunModeConfig().setPoolId(poolId);
taskInfo.setSaveResult(true);
taskInfo.setTriggerMode(TaskTriggerMode.MANUAL.name());
taskInfo.setUserId(userId);
taskInfo.setRealTime(false);
apiExecuteService.execute(taskRequest);
}
} }

View File

@ -1512,7 +1512,7 @@ public class ApiScenarioService extends MoveNodeService {
return apiScenarioDetailDTO; return apiScenarioDetailDTO;
} }
private ApiScenario checkResourceIsNoDeleted(String id) { public ApiScenario checkResourceIsNoDeleted(String id) {
ApiScenarioExample example = new ApiScenarioExample(); ApiScenarioExample example = new ApiScenarioExample();
example.createCriteria().andIdEqualTo(id).andDeletedEqualTo(false); example.createCriteria().andIdEqualTo(id).andDeletedEqualTo(false);
List<ApiScenario> apiScenarios = apiScenarioMapper.selectByExample(example); List<ApiScenario> apiScenarios = apiScenarioMapper.selectByExample(example);

View File

@ -6,6 +6,7 @@ import io.metersphere.system.domain.ExecTask;
import io.metersphere.system.service.TaskRerunService; import io.metersphere.system.service.TaskRerunService;
import java.util.HashMap; import java.util.HashMap;
import java.util.List;
import java.util.Map; import java.util.Map;
/** /**
@ -28,10 +29,7 @@ public class TaskRerunServiceInvoker {
return EnumValidator.validateEnum(ExecTaskType.class, execTaskType); return EnumValidator.validateEnum(ExecTaskType.class, execTaskType);
} }
public static void rerun(ExecTask execTask) { public static void rerun(ExecTask execTask, List<String> taskItemIds, String userId) {
TaskRerunService taskRerunService = getTaskRerunService(getExecTaskType(execTask.getTaskType())); getTaskRerunService(getExecTaskType(execTask.getTaskType())).rerun(execTask, taskItemIds, userId);
if (taskRerunService != null) {
taskRerunService.rerun(execTask);
}
} }
} }

View File

@ -64,4 +64,9 @@ public interface ExtExecTaskItemMapper {
long getUnDeleteScenarioExecCount(@Param("projectId") String projectId, @Param("startTime") Long startTime, @Param("endTime") Long endTime, @Param("resourceTypes") List<String> resourceTypes); long getUnDeleteScenarioExecCount(@Param("projectId") String projectId, @Param("startTime") Long startTime, @Param("endTime") Long endTime, @Param("resourceTypes") List<String> resourceTypes);
List<String> selectRerunIds(@Param("taskId") String taskId);
void resetRerunTaskItem(@Param("taskId") String taskId, @Param("userId") String userId);
void deleteRerunTaskItemReportRelation(@Param("taskId") String taskId);
} }

View File

@ -1,7 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="io.metersphere.system.mapper.ExtExecTaskItemMapper"> <mapper namespace="io.metersphere.system.mapper.ExtExecTaskItemMapper">
<select id="selectList" resultType="io.metersphere.system.dto.taskhub.TaskHubItemDTO"> <select id="selectList" resultType="io.metersphere.system.dto.taskhub.TaskHubItemDTO">
SELECT exec_task_item.*, exec_task.num, exec_task.task_name, exec_task.trigger_mode SELECT exec_task_item.*, exec_task.num, exec_task.task_name, exec_task.trigger_mode
FROM exec_task_item FROM exec_task_item
@ -237,7 +236,6 @@
and `status` = 'RUNNING' and `status` = 'RUNNING'
</update> </update>
<select id="getResourcePoolsByItemIds" resultType="io.metersphere.system.domain.ExecTaskItem"> <select id="getResourcePoolsByItemIds" resultType="io.metersphere.system.domain.ExecTaskItem">
SELECT SELECT
id, id,
@ -321,4 +319,32 @@
</foreach> </foreach>
</if> </if>
</select> </select>
<select id="selectRerunIds" resultType="java.lang.String">
select id from exec_task_item where task_id = #{taskId} and `result` = 'ERROR' and deleted = false
</select>
<update id="resetRerunTaskItem">
UPDATE exec_task_item
SET `status` = 'PENDING',
`result` = 'PENDING',
resource_pool_id = '',
resource_pool_node = null,
thread_id = null,
start_time = null,
end_time = null,
collection_id = null,
error_message = null,
executor = #{userId}
WHERE
deleted = false
and task_id = #{taskId}
and `result` = 'ERROR'
</update>
<delete id="deleteRerunTaskItemReportRelation">
delete arrt
from api_report_relate_task arrt join exec_task_item eti on eti.id = arrt.task_resource_id
where eti.task_id = #{taskId} and eti.`result` = 'ERROR' and eti.deleted = false
</delete>
</mapper> </mapper>

View File

@ -45,6 +45,7 @@ import io.metersphere.system.utils.TaskRunnerClient;
import jakarta.annotation.Resource; import jakarta.annotation.Resource;
import org.apache.commons.collections.CollectionUtils; import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections4.ListUtils; import org.apache.commons.collections4.ListUtils;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.ExecutorType; import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSession;
@ -568,10 +569,34 @@ public class BaseTaskHubService {
throw new MSException(Translator.get("no_permission_to_resource")); throw new MSException(Translator.get("no_permission_to_resource"));
} }
// 更新任务状态 // 查询待执行的任务项
extExecTaskMapper.batchUpdateTaskStatus(List.of(id), userId, orgId, projectId, ExecStatus.RERUNNING.name()); List<String> taskItemIds = extExecTaskItemMapper.selectRerunIds(execTask.getId());
TaskRerunServiceInvoker.rerun(execTask); if (CollectionUtils.isEmpty(taskItemIds)) {
return;
}
// 更新任务状态
execTask.setStatus(ExecStatus.RERUNNING.name());
execTask.setCreateUser(userId);
execTask.setEndTime(null);
execTask.setResult(ExecStatus.PENDING.name());
execTaskMapper.updateByPrimaryKey(execTask);
if (BooleanUtils.isFalse(execTask.getIntegrated()) && !StringUtils.equalsAny(execTask.getTaskType(), ExecTaskType.TEST_PLAN.name(), ExecTaskType.TEST_PLAN_GROUP.name())) {
// 非集合报告和测试计划执行则删除任务和报告的关联关系
ApiReportRelateTaskExample example = new ApiReportRelateTaskExample();
example.createCriteria().andTaskResourceIdEqualTo(execTask.getId());
apiReportRelateTaskMapper.deleteByExample(example);
}
// 删除任务项和报告的关联关系
extExecTaskItemMapper.deleteRerunTaskItemReportRelation(execTask.getId());
// 更新任务项状态等
extExecTaskItemMapper.resetRerunTaskItem(execTask.getId(), userId);
TaskRerunServiceInvoker.rerun(execTask, taskItemIds, userId);
} }
private void handleStopTaskAsync(List<String> ids) { private void handleStopTaskAsync(List<String> ids) {

View File

@ -2,6 +2,8 @@ package io.metersphere.system.service;
import io.metersphere.system.domain.ExecTask; import io.metersphere.system.domain.ExecTask;
import java.util.List;
/** /**
* @Author: jianxing * @Author: jianxing
* @CreateTime: 2024-02-06 20:47 * @CreateTime: 2024-02-06 20:47
@ -10,5 +12,5 @@ public interface TaskRerunService {
/** /**
* 任务重跑 * 任务重跑
*/ */
void rerun(ExecTask execTask); void rerun(ExecTask execTask, List<String> taskItemIds, String userId);
} }