From 6c9cccf0abda9a42ea2047b9ecdcd55c4032c949 Mon Sep 17 00:00:00 2001 From: AgAngle <1323481023@qq.com> Date: Thu, 14 Nov 2024 19:49:36 +0800 Subject: [PATCH] =?UTF-8?q?feat(=E6=8E=A5=E5=8F=A3=E6=B5=8B=E8=AF=95):=20?= =?UTF-8?q?=E5=8D=95=E6=8E=A5=E5=8F=A3=E7=94=A8=E4=BE=8B=E6=89=A7=E8=A1=8C?= =?UTF-8?q?=E4=BB=BB=E5=8A=A1=EF=BC=8C=E9=87=8D=E8=B7=91=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --task=1016918 --user=陈建星 失败重跑 https://www.tapd.cn/55049933/s/1609836 --- .../sdk/dto/api/task/GetRunScriptRequest.java | 4 ++ .../sdk/dto/api/task/TaskInfo.java | 4 ++ .../ApiCaseExecuteCallbackService.java | 8 +-- .../api/service/ApiCaseRerunService.java | 26 ------- .../service/ApiExecuteResourceService.java | 18 ++--- .../api/service/ApiExecuteService.java | 1 + .../api/service/ApiScenarioRerunService.java | 26 ------- .../ApiTestCaseBatchRunService.java | 2 +- .../definition/ApiTestCaseRunService.java | 72 ++++++++++++++----- .../service/rerun/ApiCaseRerunService.java | 37 ++++++++++ .../rerun/ApiScenarioRerunService.java | 37 ++++++++++ .../scenario/ApiScenarioRunService.java | 22 +++++- .../service/scenario/ApiScenarioService.java | 2 +- .../invoker/TaskRerunServiceInvoker.java | 8 +-- .../system/mapper/ExtExecTaskItemMapper.java | 5 ++ .../system/mapper/ExtExecTaskItemMapper.xml | 30 +++++++- .../system/service/BaseTaskHubService.java | 31 +++++++- .../system/service/TaskRerunService.java | 4 +- 18 files changed, 239 insertions(+), 98 deletions(-) delete mode 100644 backend/services/api-test/src/main/java/io/metersphere/api/service/ApiCaseRerunService.java delete mode 100644 backend/services/api-test/src/main/java/io/metersphere/api/service/ApiScenarioRerunService.java create mode 100644 backend/services/api-test/src/main/java/io/metersphere/api/service/rerun/ApiCaseRerunService.java create mode 100644 backend/services/api-test/src/main/java/io/metersphere/api/service/rerun/ApiScenarioRerunService.java diff --git a/backend/framework/sdk/src/main/java/io/metersphere/sdk/dto/api/task/GetRunScriptRequest.java b/backend/framework/sdk/src/main/java/io/metersphere/sdk/dto/api/task/GetRunScriptRequest.java index fc2a292c92..cff813d45b 100644 --- a/backend/framework/sdk/src/main/java/io/metersphere/sdk/dto/api/task/GetRunScriptRequest.java +++ b/backend/framework/sdk/src/main/java/io/metersphere/sdk/dto/api/task/GetRunScriptRequest.java @@ -68,4 +68,8 @@ public class GetRunScriptRequest implements Serializable { * {@link io.metersphere.sdk.constants.TaskTriggerMode} */ private String triggerMode; + /** + * 是否是任务失败重跑 + */ + private Boolean rerun; } diff --git a/backend/framework/sdk/src/main/java/io/metersphere/sdk/dto/api/task/TaskInfo.java b/backend/framework/sdk/src/main/java/io/metersphere/sdk/dto/api/task/TaskInfo.java index a2d60fd9b3..3693bba54c 100644 --- a/backend/framework/sdk/src/main/java/io/metersphere/sdk/dto/api/task/TaskInfo.java +++ b/backend/framework/sdk/src/main/java/io/metersphere/sdk/dto/api/task/TaskInfo.java @@ -124,4 +124,8 @@ public class TaskInfo implements Serializable { * 执行时初始化报告需要记录资源池ID */ private String poolId; + /** + * 是否是任务失败重跑 + */ + private Boolean rerun; } diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/service/ApiCaseExecuteCallbackService.java b/backend/services/api-test/src/main/java/io/metersphere/api/service/ApiCaseExecuteCallbackService.java index 2bb9e25803..4114ae2659 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/service/ApiCaseExecuteCallbackService.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/service/ApiCaseExecuteCallbackService.java @@ -70,14 +70,10 @@ public class ApiCaseExecuteCallbackService implements ApiExecuteCallbackService String reportId = taskItem.getReportId(); if (BooleanUtils.isTrue(request.getBatch()) && !request.getRunModeConfig().isIntegratedReport()) { // 批量执行,生成独立报告 - ApiTestCaseRecord apiTestCaseRecord = apiTestCaseBatchRunService.initApiReport(taskItem.getId(), request.getRunModeConfig(), apiTestCase, request.getUserId()); - reportId = apiTestCaseRecord.getApiReportId(); + reportId = apiTestCaseBatchRunService.initApiReport(taskItem.getId(), request.getRunModeConfig(), apiTestCase, request.getUserId()); } else if (BooleanUtils.isFalse(request.getBatch()) && !ApiExecuteRunMode.isDebug(request.getRunMode())) { // 单用例执行,非调试,生成报告 - if (StringUtils.isBlank(taskItem.getReportId())) { - reportId = IDGenerator.nextStr(); - } - apiTestCaseRunService.initApiReport(taskItem.getId(), apiTestCase, request); + return apiTestCaseRunService.initApiReport(taskItem.getId(), apiTestCase, request); } return reportId; } diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/service/ApiCaseRerunService.java b/backend/services/api-test/src/main/java/io/metersphere/api/service/ApiCaseRerunService.java deleted file mode 100644 index 78c33d9ed1..0000000000 --- a/backend/services/api-test/src/main/java/io/metersphere/api/service/ApiCaseRerunService.java +++ /dev/null @@ -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 - } -} diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/service/ApiExecuteResourceService.java b/backend/services/api-test/src/main/java/io/metersphere/api/service/ApiExecuteResourceService.java index b2cfc89111..ae6df71546 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/service/ApiExecuteResourceService.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/service/ApiExecuteResourceService.java @@ -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.TaskItem; import io.metersphere.sdk.exception.MSException; -import io.metersphere.sdk.util.CommonBeanFactory; import io.metersphere.sdk.util.EnumValidator; import io.metersphere.sdk.util.LogUtils; import jakarta.annotation.Resource; @@ -71,15 +70,18 @@ public class ApiExecuteResourceService { String reportId = taskItem.getReportId(); ApiExecuteResourceType apiExecuteResourceType = EnumValidator.validateEnum(ApiExecuteResourceType.class, request.getResourceType()); - if (request.getBatch()) { - // 设置缓存成功说明是第一个任务,则设置任务的开始时间和运行状态 - if (taskRunningCache.setIfAbsent(taskId)) { - // 将任务状态更新为运行中 + // 重跑不更新任务状态 + if (BooleanUtils.isFalse(request.getRerun())) { + if (request.getBatch()) { + // 设置缓存成功说明是第一个任务,则设置任务的开始时间和运行状态 + if (taskRunningCache.setIfAbsent(taskId)) { + // 将任务状态更新为运行中 + apiCommonService.updateTaskRunningStatus(taskId); + } + } else { + // 非批量时,直接更新任务状态 apiCommonService.updateTaskRunningStatus(taskId); } - } else { - // 非批量时,直接更新任务状态 - apiCommonService.updateTaskRunningStatus(taskId); } // 更新任务项状态 diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/service/ApiExecuteService.java b/backend/services/api-test/src/main/java/io/metersphere/api/service/ApiExecuteService.java index 6769462fda..0e09cdfa22 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/service/ApiExecuteService.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/service/ApiExecuteService.java @@ -866,6 +866,7 @@ public class ApiExecuteService { TaskItem taskItem = new TaskItem(); taskItem.setReportId(reportId); taskItem.setResourceId(resourceId); + taskItem.setRequestCount(1L); return taskItem; } diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/service/ApiScenarioRerunService.java b/backend/services/api-test/src/main/java/io/metersphere/api/service/ApiScenarioRerunService.java deleted file mode 100644 index 53d12bb50f..0000000000 --- a/backend/services/api-test/src/main/java/io/metersphere/api/service/ApiScenarioRerunService.java +++ /dev/null @@ -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 - } -} diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/service/definition/ApiTestCaseBatchRunService.java b/backend/services/api-test/src/main/java/io/metersphere/api/service/definition/ApiTestCaseBatchRunService.java index da92a5b0d6..108f779895 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/service/definition/ApiTestCaseBatchRunService.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/service/definition/ApiTestCaseBatchRunService.java @@ -404,7 +404,7 @@ public class ApiTestCaseBatchRunService { * @param apiTestCase * @return */ - public ApiTestCaseRecord initApiReport(String taskItemId, ApiRunModeConfigDTO runModeConfig, + public String initApiReport(String taskItemId, ApiRunModeConfigDTO runModeConfig, ApiTestCase apiTestCase, String userId) { // 初始化报告 ApiReport apiReport = getApiReport(runModeConfig, apiTestCase, userId); diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/service/definition/ApiTestCaseRunService.java b/backend/services/api-test/src/main/java/io/metersphere/api/service/definition/ApiTestCaseRunService.java index 7e08d3c90a..e884befc65 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/service/definition/ApiTestCaseRunService.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/service/definition/ApiTestCaseRunService.java @@ -83,6 +83,31 @@ public class ApiTestCaseRunService { 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()); Project project = projectMapper.selectByPrimaryKey(apiTestCase.getProjectId()); - 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()); - - 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()); - + ExecTask execTask = newExecTask(apiTestCase, userId, project); + ExecTaskItem execTaskItem = newExecTaskItem(apiTestCase, userId, project, execTask.getId()); baseTaskHubService.insertExecTaskAndDetail(execTask, execTaskItem); 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()); } + 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 * @return */ - public ApiTestCaseRecord initApiReport(String taskItemId, ApiTestCase apiTestCase, GetRunScriptRequest request) { + public String initApiReport(String taskItemId, ApiTestCase apiTestCase, GetRunScriptRequest request) { // 初始化报告 ApiReport apiReport = getApiReport(apiTestCase, request); + if (StringUtils.isBlank(apiReport.getId())) { + apiReport.setId(IDGenerator.nextStr()); + } 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 * @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); // 初始化报告和用例的关联关系 @@ -241,7 +277,7 @@ public class ApiTestCaseRunService { ApiReportRelateTask apiReportRelateTask = apiCommonService.getApiReportRelateTask(taskItemId, reportId); apiReportService.insertApiReportDetail(apiReportStep, apiTestCaseRecord, apiReportRelateTask); - return apiTestCaseRecord; + return apiTestCaseRecord.getApiReportId(); } public ApiReport getApiReport(ApiTestCase apiTestCase, GetRunScriptRequest request) { diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/service/rerun/ApiCaseRerunService.java b/backend/services/api-test/src/main/java/io/metersphere/api/service/rerun/ApiCaseRerunService.java new file mode 100644 index 0000000000..15d6c54ee3 --- /dev/null +++ b/backend/services/api-test/src/main/java/io/metersphere/api/service/rerun/ApiCaseRerunService.java @@ -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 taskItemIds, String userId) { + ExecTaskItem execTaskItem = execTaskItemMapper.selectByPrimaryKey(taskItemIds.getFirst()); + apiTestCaseRunService.runRun(execTask, execTaskItem, userId); + } +} diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/service/rerun/ApiScenarioRerunService.java b/backend/services/api-test/src/main/java/io/metersphere/api/service/rerun/ApiScenarioRerunService.java new file mode 100644 index 0000000000..9a0d25f736 --- /dev/null +++ b/backend/services/api-test/src/main/java/io/metersphere/api/service/rerun/ApiScenarioRerunService.java @@ -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 taskItemIds, String userId) { + ExecTaskItem execTaskItem = execTaskItemMapper.selectByPrimaryKey(taskItemIds.getFirst()); + apiScenarioRunService.runRun(execTask, execTaskItem, userId); + } +} diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/service/scenario/ApiScenarioRunService.java b/backend/services/api-test/src/main/java/io/metersphere/api/service/scenario/ApiScenarioRunService.java index 75962c4bfa..2be707e503 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/service/scenario/ApiScenarioRunService.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/service/scenario/ApiScenarioRunService.java @@ -467,8 +467,9 @@ public class ApiScenarioRunService { public String initApiScenarioReport(String taskItemId, ApiScenario apiScenario, GetRunScriptRequest request) { // 初始化报告 ApiScenarioReport scenarioReport = getScenarioReport(apiScenario, request); + scenarioReport.setStartTime(System.currentTimeMillis()); 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; } + + 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); + } } diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/service/scenario/ApiScenarioService.java b/backend/services/api-test/src/main/java/io/metersphere/api/service/scenario/ApiScenarioService.java index 68b8b1ac1d..5d9f3b1e66 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/service/scenario/ApiScenarioService.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/service/scenario/ApiScenarioService.java @@ -1512,7 +1512,7 @@ public class ApiScenarioService extends MoveNodeService { return apiScenarioDetailDTO; } - private ApiScenario checkResourceIsNoDeleted(String id) { + public ApiScenario checkResourceIsNoDeleted(String id) { ApiScenarioExample example = new ApiScenarioExample(); example.createCriteria().andIdEqualTo(id).andDeletedEqualTo(false); List apiScenarios = apiScenarioMapper.selectByExample(example); diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/invoker/TaskRerunServiceInvoker.java b/backend/services/system-setting/src/main/java/io/metersphere/system/invoker/TaskRerunServiceInvoker.java index 5c51732472..0f073824a7 100644 --- a/backend/services/system-setting/src/main/java/io/metersphere/system/invoker/TaskRerunServiceInvoker.java +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/invoker/TaskRerunServiceInvoker.java @@ -6,6 +6,7 @@ import io.metersphere.system.domain.ExecTask; import io.metersphere.system.service.TaskRerunService; import java.util.HashMap; +import java.util.List; import java.util.Map; /** @@ -28,10 +29,7 @@ public class TaskRerunServiceInvoker { return EnumValidator.validateEnum(ExecTaskType.class, execTaskType); } - public static void rerun(ExecTask execTask) { - TaskRerunService taskRerunService = getTaskRerunService(getExecTaskType(execTask.getTaskType())); - if (taskRerunService != null) { - taskRerunService.rerun(execTask); - } + public static void rerun(ExecTask execTask, List taskItemIds, String userId) { + getTaskRerunService(getExecTaskType(execTask.getTaskType())).rerun(execTask, taskItemIds, userId); } } diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/mapper/ExtExecTaskItemMapper.java b/backend/services/system-setting/src/main/java/io/metersphere/system/mapper/ExtExecTaskItemMapper.java index 6b824fead4..5fdec3a782 100644 --- a/backend/services/system-setting/src/main/java/io/metersphere/system/mapper/ExtExecTaskItemMapper.java +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/mapper/ExtExecTaskItemMapper.java @@ -64,4 +64,9 @@ public interface ExtExecTaskItemMapper { long getUnDeleteScenarioExecCount(@Param("projectId") String projectId, @Param("startTime") Long startTime, @Param("endTime") Long endTime, @Param("resourceTypes") List resourceTypes); + List selectRerunIds(@Param("taskId") String taskId); + + void resetRerunTaskItem(@Param("taskId") String taskId, @Param("userId") String userId); + + void deleteRerunTaskItemReportRelation(@Param("taskId") String taskId); } diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/mapper/ExtExecTaskItemMapper.xml b/backend/services/system-setting/src/main/java/io/metersphere/system/mapper/ExtExecTaskItemMapper.xml index 1938f60945..ad48393ad6 100644 --- a/backend/services/system-setting/src/main/java/io/metersphere/system/mapper/ExtExecTaskItemMapper.xml +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/mapper/ExtExecTaskItemMapper.xml @@ -1,7 +1,6 @@ - SELECT id, @@ -321,4 +319,32 @@ + + + + + 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' + + + + 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 + \ No newline at end of file diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/service/BaseTaskHubService.java b/backend/services/system-setting/src/main/java/io/metersphere/system/service/BaseTaskHubService.java index 00621af461..06a41f6049 100644 --- a/backend/services/system-setting/src/main/java/io/metersphere/system/service/BaseTaskHubService.java +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/service/BaseTaskHubService.java @@ -45,6 +45,7 @@ import io.metersphere.system.utils.TaskRunnerClient; import jakarta.annotation.Resource; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.collections4.ListUtils; +import org.apache.commons.lang3.BooleanUtils; import org.apache.commons.lang3.StringUtils; import org.apache.ibatis.session.ExecutorType; import org.apache.ibatis.session.SqlSession; @@ -568,10 +569,34 @@ public class BaseTaskHubService { throw new MSException(Translator.get("no_permission_to_resource")); } - // 更新任务状态 - extExecTaskMapper.batchUpdateTaskStatus(List.of(id), userId, orgId, projectId, ExecStatus.RERUNNING.name()); + // 查询待执行的任务项 + List 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 ids) { diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/service/TaskRerunService.java b/backend/services/system-setting/src/main/java/io/metersphere/system/service/TaskRerunService.java index 7d7060f13d..1e22a07c3a 100644 --- a/backend/services/system-setting/src/main/java/io/metersphere/system/service/TaskRerunService.java +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/service/TaskRerunService.java @@ -2,6 +2,8 @@ package io.metersphere.system.service; import io.metersphere.system.domain.ExecTask; +import java.util.List; + /** * @Author: jianxing * @CreateTime: 2024-02-06 20:47 @@ -10,5 +12,5 @@ public interface TaskRerunService { /** * 任务重跑 */ - void rerun(ExecTask execTask); + void rerun(ExecTask execTask, List taskItemIds, String userId); }