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 f1aa3fed1a..e5a0091e7c 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 @@ -209,24 +209,31 @@ public class ApiBatchRunBaseService { return runModeConfig.getEnvironmentId(); } - public void updateTaskStatus(String taskId) { - // 更新任务状态 - ExecTask execTask = new ExecTask(); - execTask.setEndTime(System.currentTimeMillis()); - execTask.setId(taskId); - execTask.setStatus(ExecStatus.COMPLETED.name()); - if (extExecTaskItemMapper.hasErrorItem(taskId)) { - execTask.setResult(ResultStatus.ERROR.name()); - } else if (extExecTaskItemMapper.hasFakeErrorItem(taskId)) { - execTask.setResult(ResultStatus.FAKE_ERROR.name()); - } else { - execTask.setResult(ResultStatus.SUCCESS.name()); + public void updateTaskCompletedStatus(String taskId) { + // 删除执行缓存 + removeRunningTaskCache(taskId); + ExecTask originExecTask = execTaskMapper.selectByPrimaryKey(taskId); + // 出现异常,导致任务没有开始,则不更新任务状态 + if (!StringUtils.equals(originExecTask.getStatus(), ExecStatus.PENDING.name())) { + // 更新任务状态 + ExecTask execTask = new ExecTask(); + execTask.setEndTime(System.currentTimeMillis()); + execTask.setId(taskId); + execTask.setStatus(ExecStatus.COMPLETED.name()); + if (extExecTaskItemMapper.hasErrorItem(taskId)) { + execTask.setResult(ResultStatus.ERROR.name()); + } else if (extExecTaskItemMapper.hasFakeErrorItem(taskId)) { + execTask.setResult(ResultStatus.FAKE_ERROR.name()); + } else { + execTask.setResult(ResultStatus.SUCCESS.name()); + } + execTaskMapper.updateByPrimaryKeySelective(execTask); } - execTaskMapper.updateByPrimaryKeySelective(execTask); } /** * 清理正在运行的任务缓存 + * * @param taskId */ public void removeRunningTaskCache(String taskId) { 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 77a90a7d3d..1082519195 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 @@ -175,8 +175,7 @@ public class TestPlanApiCaseBatchRunService { ExecutionQueue collectionQueue = apiExecutionQueueService.getQueue(collectionQueueId); if (collectionQueue == null) { // 失败停止,或者执行完成,更新任务状态 - apiBatchRunBaseService.updateTaskStatus(collectionQueueId); - apiBatchRunBaseService.removeRunningTaskCache(collectionQueueId); + apiBatchRunBaseService.updateTaskCompletedStatus(collectionQueueId); return; } String userId = collectionQueue.getUserId(); 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 4398debf15..b2adc89321 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 @@ -200,8 +200,7 @@ public class TestPlanApiScenarioBatchRunService { ExecutionQueue collectionQueue = apiExecutionQueueService.getQueue(collectionQueueId); if (collectionQueue == null) { // 失败停止,或者执行完成,更新任务状态 - apiBatchRunBaseService.updateTaskStatus(collectionQueueId); - apiBatchRunBaseService.removeRunningTaskCache(collectionQueueId); + apiBatchRunBaseService.updateTaskCompletedStatus(collectionQueueId); return; } String userId = collectionQueue.getUserId(); diff --git a/backend/services/test-plan/src/main/java/io/metersphere/plan/service/TestPlanExecuteService.java b/backend/services/test-plan/src/main/java/io/metersphere/plan/service/TestPlanExecuteService.java index 3868636753..543f079fc6 100644 --- a/backend/services/test-plan/src/main/java/io/metersphere/plan/service/TestPlanExecuteService.java +++ b/backend/services/test-plan/src/main/java/io/metersphere/plan/service/TestPlanExecuteService.java @@ -687,8 +687,7 @@ public class TestPlanExecuteService { TestPlanReport testPlanReport = testPlanReportMapper.selectByPrimaryKey(reportId); if (StringUtils.equals(testPlanReport.getParentId(), reportId) && StringUtils.isNotBlank(taskId)) { // 执行完成,更新任务状态 - apiBatchRunBaseService.updateTaskStatus(taskId); - apiBatchRunBaseService.removeRunningTaskCache(taskId); + apiBatchRunBaseService.updateTaskCompletedStatus(taskId); } testPlanExecuteSupportService.summaryTestPlanReport(reportId, isGroupReport, isStop); }