From 22f95f84a5c645ed9e7a2b92be5cd0282427a458 Mon Sep 17 00:00:00 2001 From: AgAngle <1323481023@qq.com> Date: Mon, 25 Nov 2024 17:16:50 +0800 Subject: [PATCH] =?UTF-8?q?fix(=E6=8E=A5=E5=8F=A3=E6=B5=8B=E8=AF=95):=20?= =?UTF-8?q?=E8=B5=84=E6=BA=90=E6=B1=A0=E4=B8=8D=E5=8F=AF=E7=94=A8=EF=BC=8C?= =?UTF-8?q?=E6=B8=85=E7=90=86=E6=89=A7=E8=A1=8C=E9=98=9F=E5=88=97=E7=AD=89?= =?UTF-8?q?=E4=BF=A1=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --bug=1049133 --user=陈建星 【任务中心】-计划组的部分子计划关联的资源池不可用,失败重跑时,任务无法执行完成 https://www.tapd.cn/55049933/s/1616882 --- .../api/service/ApiBatchRunBaseService.java | 12 ++++++- .../ApiTestCaseBatchRunService.java | 7 ++++- .../service/queue/ApiExecutionSetService.java | 17 ++++++++-- .../scenario/ApiScenarioBatchRunService.java | 7 ++++- .../PlanRunTestPlanApiCaseService.java | 31 +++++++++++++------ .../PlanRunTestPlanApiScenarioService.java | 20 +++++++++--- .../TestPlanApiCaseBatchRunService.java | 24 +++++++++++--- .../TestPlanApiScenarioBatchRunService.java | 23 +++++++++++--- 8 files changed, 115 insertions(+), 26 deletions(-) diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/service/ApiBatchRunBaseService.java b/backend/services/api-test/src/main/java/io/metersphere/api/service/ApiBatchRunBaseService.java index c27d606631..15538e7de1 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/service/ApiBatchRunBaseService.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/service/ApiBatchRunBaseService.java @@ -5,6 +5,7 @@ import io.metersphere.api.domain.ApiReportRelateTaskExample; import io.metersphere.api.domain.ApiScenarioReport; import io.metersphere.api.mapper.ApiReportRelateTaskMapper; import io.metersphere.api.service.queue.ApiExecutionQueueService; +import io.metersphere.api.service.queue.ApiExecutionSetService; import io.metersphere.sdk.constants.ApiBatchRunMode; import io.metersphere.sdk.constants.CommonConstants; import io.metersphere.sdk.constants.ExecStatus; @@ -47,6 +48,8 @@ public class ApiBatchRunBaseService { private ExecTaskMapper execTaskMapper; @Resource private ApiReportRelateTaskMapper apiReportRelateTaskMapper; + @Resource + private ApiExecutionSetService apiExecutionSetService; public static final int BATCH_TASK_ITEM_SIZE = 500; @@ -271,7 +274,14 @@ public class ApiBatchRunBaseService { if (count > 0) { taskRequest.setTaskItems(taskItems); - apiExecuteService.batchExecute(taskRequest); + try { + apiExecuteService.batchExecute(taskRequest); + } catch (Exception e) { + // 执行失败,删除执行集合中的任务项 + List taskItemIds = taskRequest.getTaskItems().stream().map(TaskItem::getId).toList(); + apiExecutionSetService.removeItems(taskRequest.getTaskInfo().getSetId(), taskItemIds); + LogUtils.error(e); + } } } 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 016fdf63a5..cf42e7dfbe 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 @@ -395,7 +395,12 @@ public class ApiTestCaseBatchRunService { taskRequest.getTaskItem().setRequestCount(1L); taskRequest.getTaskItem().setId(taskItemId); - apiExecuteService.execute(taskRequest); + try { + apiExecuteService.execute(taskRequest); + } catch (Exception e) { + // 执行失败,删除队列 + apiExecutionQueueService.deleteQueue(queue.getQueueId()); + } } private TaskRequestDTO getTaskRequestDTO(String reportId, ApiTestCase apiTestCase, ApiRunModeConfigDTO runModeConfig) { diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/service/queue/ApiExecutionSetService.java b/backend/services/api-test/src/main/java/io/metersphere/api/service/queue/ApiExecutionSetService.java index e1761dae8c..f092777909 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/service/queue/ApiExecutionSetService.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/service/queue/ApiExecutionSetService.java @@ -34,8 +34,21 @@ public class ApiExecutionSetService { /** * 从执行集合中去除选项 */ - public Long removeItem(String setId, String resourceId) { - stringRedisTemplate.opsForSet().remove(SET_PREFIX + setId, resourceId); + public Long removeItem(String setId, String taskItemId) { + stringRedisTemplate.opsForSet().remove(SET_PREFIX + setId, taskItemId); + Long size = stringRedisTemplate.opsForSet().size(SET_PREFIX + setId); + if (size == null || size == 0) { + // 集合没有元素,则删除集合 + stringRedisTemplate.delete(SET_PREFIX + setId); + } + return size; + } + + /** + * 从执行集合中去除选项 + */ + public Long removeItems(String setId, List taskItemIds) { + stringRedisTemplate.opsForSet().remove(SET_PREFIX + setId, taskItemIds.toArray()); Long size = stringRedisTemplate.opsForSet().size(SET_PREFIX + setId); if (size == null || size == 0) { // 集合没有元素,则删除集合 diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/service/scenario/ApiScenarioBatchRunService.java b/backend/services/api-test/src/main/java/io/metersphere/api/service/scenario/ApiScenarioBatchRunService.java index 1eaa14f94a..604f23f522 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/service/scenario/ApiScenarioBatchRunService.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/service/scenario/ApiScenarioBatchRunService.java @@ -351,7 +351,12 @@ public class ApiScenarioBatchRunService { taskRequest.getTaskInfo().setTaskId(queue.getTaskId()); taskRequest.getTaskInfo().setRerun(queue.getRerun()); - apiExecuteService.execute(taskRequest); + try { + apiExecuteService.execute(taskRequest); + } catch (Exception e) { + // 执行失败,删除队列 + apiExecutionQueueService.deleteQueue(queue.getQueueId()); + } } private TaskRequestDTO getTaskRequestDTO(String projectId, ApiRunModeConfigDTO runModeConfig) { diff --git a/backend/services/test-plan/src/main/java/io/metersphere/plan/service/PlanRunTestPlanApiCaseService.java b/backend/services/test-plan/src/main/java/io/metersphere/plan/service/PlanRunTestPlanApiCaseService.java index 7220eafe37..b37f95c501 100644 --- a/backend/services/test-plan/src/main/java/io/metersphere/plan/service/PlanRunTestPlanApiCaseService.java +++ b/backend/services/test-plan/src/main/java/io/metersphere/plan/service/PlanRunTestPlanApiCaseService.java @@ -116,12 +116,13 @@ public class PlanRunTestPlanApiCaseService { TestPlan testPlan = testPlanMapper.selectByPrimaryKey(testPlanId); TaskBatchRequestDTO taskRequest = apiTestCaseBatchRunService.getTaskBatchRequestDTO(testPlan.getProjectId(), runModeConfig); - taskRequest.getTaskInfo().setTaskId(taskId); - taskRequest.getTaskInfo().setParentQueueId(testPlanExecutionQueue.getQueueId()); - taskRequest.getTaskInfo().setSetId(execSetId); - taskRequest.getTaskInfo().setRerun(testPlanExecutionQueue.isRerun()); - taskRequest.getTaskInfo().setUserId(userId); - taskRequest.getTaskInfo().setResourceType(ApiExecuteResourceType.PLAN_RUN_API_CASE.name()); + TaskInfo taskInfo = taskRequest.getTaskInfo(); + taskInfo.setTaskId(taskId); + taskInfo.setParentQueueId(testPlanExecutionQueue.getQueueId()); + taskInfo.setSetId(execSetId); + taskInfo.setRerun(testPlanExecutionQueue.isRerun()); + taskInfo.setUserId(userId); + taskInfo.setResourceType(ApiExecuteResourceType.PLAN_RUN_API_CASE.name()); List execTaskItems = apiBatchRunBaseService.getExecTaskItemByTaskIdAndCollectionId(testPlanExecutionQueue.getTaskId(), collection.getId(), testPlanExecutionQueue.isRerun()); @@ -141,10 +142,17 @@ public class PlanRunTestPlanApiCaseService { }) .collect(Collectors.toList()); + List taskItemIds = taskItems.stream().map(TaskItem::getId).toList(); // 初始化执行集合,以便判断是否执行完毕 - apiExecutionSetService.initSet(execSetId, taskItems.stream().map(TaskItem::getId).toList()); + apiExecutionSetService.initSet(execSetId, taskItemIds); taskRequest.setTaskItems(taskItems); - apiExecuteService.batchExecute(taskRequest); + try { + apiExecuteService.batchExecute(taskRequest); + } catch (Exception e) { + // 执行失败,删除执行集合中的任务项 + apiExecutionSetService.removeItems(execSetId, taskItemIds); + LogUtils.error(e); + } taskRequest.setTaskItems(null); }); return false; @@ -180,7 +188,12 @@ public class PlanRunTestPlanApiCaseService { taskRequest.getTaskItem().setRequestCount(1L); taskRequest.getTaskItem().setId(queueDetail.getTaskItemId()); - apiExecuteService.execute(taskRequest); + try { + apiExecuteService.execute(taskRequest); + } catch (Exception e) { + // 执行失败,删除队列 + apiExecutionQueueService.deleteQueue(queue.getQueueId()); + } } /** diff --git a/backend/services/test-plan/src/main/java/io/metersphere/plan/service/PlanRunTestPlanApiScenarioService.java b/backend/services/test-plan/src/main/java/io/metersphere/plan/service/PlanRunTestPlanApiScenarioService.java index afde50656c..c62c35af45 100644 --- a/backend/services/test-plan/src/main/java/io/metersphere/plan/service/PlanRunTestPlanApiScenarioService.java +++ b/backend/services/test-plan/src/main/java/io/metersphere/plan/service/PlanRunTestPlanApiScenarioService.java @@ -135,10 +135,18 @@ public class PlanRunTestPlanApiScenarioService { }) .collect(Collectors.toList()); + List taskItemIds = taskItems.stream().map(TaskItem::getId).toList(); + // 初始化执行集合,以便判断是否执行完毕 - apiExecutionSetService.initSet(execSetId, taskItems.stream().map(TaskItem::getId).toList()); + apiExecutionSetService.initSet(execSetId, taskItemIds); taskRequest.setTaskItems(taskItems); - apiExecuteService.batchExecute(taskRequest); + try { + apiExecuteService.batchExecute(taskRequest); + } catch (Exception e) { + // 执行失败,删除执行集合中的任务项 + apiExecutionSetService.removeItems(execSetId, taskItemIds); + LogUtils.error(e); + } taskRequest.setTaskItems(null); }); return false; @@ -186,8 +194,12 @@ public class PlanRunTestPlanApiScenarioService { taskRequest.getTaskInfo().setRerun(queue.getRerun()); taskRequest.getTaskInfo().setParentQueueId(queue.getParentQueueId()); taskRequest.getTaskInfo().setResourceType(ApiExecuteResourceType.PLAN_RUN_API_SCENARIO.name()); - - apiExecuteService.execute(taskRequest); + try { + apiExecuteService.execute(taskRequest); + } catch (Exception e) { + // 执行失败,删除队列 + apiExecutionQueueService.deleteQueue(queue.getQueueId()); + } } public GetRunScriptResult getRunScript(GetRunScriptRequest request) { diff --git a/backend/services/test-plan/src/main/java/io/metersphere/plan/service/TestPlanApiCaseBatchRunService.java b/backend/services/test-plan/src/main/java/io/metersphere/plan/service/TestPlanApiCaseBatchRunService.java index 69329477e4..1ddc92105c 100644 --- a/backend/services/test-plan/src/main/java/io/metersphere/plan/service/TestPlanApiCaseBatchRunService.java +++ b/backend/services/test-plan/src/main/java/io/metersphere/plan/service/TestPlanApiCaseBatchRunService.java @@ -250,14 +250,24 @@ public class TestPlanApiCaseBatchRunService { }) .collect(Collectors.toList()); - List taskIds = taskItems.stream().map(TaskItem::getId).toList(); + List taskItemIds = taskItems.stream().map(TaskItem::getId).toList(); if (StringUtils.isBlank(parentSetId)) { // 如果有没有父集合,则初始化执行集合,以便判断是否执行完毕 - apiExecutionSetService.initSet(taskInfo.getSetId(), taskIds); + apiExecutionSetService.initSet(taskInfo.getSetId(), taskItemIds); } taskRequest.setTaskItems(taskItems); - apiExecuteService.batchExecute(taskRequest); + + try { + apiExecuteService.batchExecute(taskRequest); + } catch (Exception e) { + // 执行失败,删除执行集合中的任务项 + if (StringUtils.isBlank(parentSetId)) { + apiExecutionSetService.removeItems(taskInfo.getSetId(), taskItemIds); + } + LogUtils.error(e); + } + taskRequest.setTaskItems(null); }); } @@ -347,7 +357,13 @@ public class TestPlanApiCaseBatchRunService { taskRequest.getTaskItem().setId(queueDetail.getTaskItemId()); taskRequest.getTaskItem().setRequestCount(1L); - apiExecuteService.execute(taskRequest); + try { + apiExecuteService.execute(taskRequest); + } catch (Exception e) { + // 执行失败,删除队列 + apiExecutionQueueService.deleteQueue(queue.getQueueId()); + apiExecutionQueueService.deleteQueue(queue.getParentQueueId()); + } } public TaskRequestDTO getTaskRequestDTO(String resourceId, ApiTestCase apiTestCase, ApiRunModeConfigDTO runModeConfig) { diff --git a/backend/services/test-plan/src/main/java/io/metersphere/plan/service/TestPlanApiScenarioBatchRunService.java b/backend/services/test-plan/src/main/java/io/metersphere/plan/service/TestPlanApiScenarioBatchRunService.java index ff0e67f7db..7895744a7d 100644 --- a/backend/services/test-plan/src/main/java/io/metersphere/plan/service/TestPlanApiScenarioBatchRunService.java +++ b/backend/services/test-plan/src/main/java/io/metersphere/plan/service/TestPlanApiScenarioBatchRunService.java @@ -25,6 +25,7 @@ import io.metersphere.sdk.dto.api.task.*; import io.metersphere.sdk.dto.queue.ExecutionQueue; import io.metersphere.sdk.dto.queue.ExecutionQueueDetail; import io.metersphere.sdk.util.CommonBeanFactory; +import io.metersphere.sdk.util.LogUtils; import io.metersphere.sdk.util.SubListUtils; import io.metersphere.sdk.util.Translator; import io.metersphere.system.domain.ExecTask; @@ -258,14 +259,22 @@ public class TestPlanApiScenarioBatchRunService { }) .collect(Collectors.toList()); - List taskIds = taskItems.stream().map(TaskItem::getId).toList(); + List taskItemIds = taskItems.stream().map(TaskItem::getId).toList(); if (StringUtils.isBlank(parentSetId)) { // 如果有没有父集合,则初始化执行集合,以便判断是否执行完毕 - apiExecutionSetService.initSet(taskInfo.getSetId(), taskIds); + apiExecutionSetService.initSet(taskInfo.getSetId(), taskItemIds); } taskRequest.setTaskItems(taskItems); - apiExecuteService.batchExecute(taskRequest); + try { + apiExecuteService.batchExecute(taskRequest); + } catch (Exception e) { + // 执行失败,删除执行集合中的任务项 + if (StringUtils.isBlank(parentSetId)) { + apiExecutionSetService.removeItems(taskInfo.getSetId(), taskItemIds); + } + LogUtils.error(e); + } taskRequest.setTaskItems(null); }); } @@ -319,7 +328,13 @@ public class TestPlanApiScenarioBatchRunService { taskRequest.getTaskInfo().setParentQueueId(queue.getParentQueueId()); taskRequest.getTaskInfo().setParentSetId(queue.getParentSetId()); - apiExecuteService.execute(taskRequest); + try { + apiExecuteService.execute(taskRequest); + } catch (Exception e) { + // 执行失败,删除队列 + apiExecutionQueueService.deleteQueue(queue.getQueueId()); + apiExecutionQueueService.deleteQueue(queue.getParentQueueId()); + } } public TaskRequestDTO getTaskRequestDTO(String projectId, ApiRunModeConfigDTO runModeConfig) {