From 2f637979e4f8ebb5706f7b5a8d954da76e067960 Mon Sep 17 00:00:00 2001 From: "song.tianyang" Date: Thu, 28 Jan 2021 13:22:00 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E5=9C=BA=E6=99=AF?= =?UTF-8?q?=E5=88=A0=E9=99=A4=E5=90=8E=E6=B2=A1=E6=9C=89=E5=81=9C=E5=88=B0?= =?UTF-8?q?=E5=AE=9A=E6=97=B6=E4=BB=BB=E5=8A=A1=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修复场景删除后没有停到定时任务的问题 --- .../api/service/ApiAutomationService.java | 6 ++++- .../base/mapper/ScheduleMapper.java | 1 - .../metersphere/service/ScheduleService.java | 24 +++++++++++++++---- .../track/service/TestPlanService.java | 7 ++++++ 4 files changed, 32 insertions(+), 6 deletions(-) diff --git a/backend/src/main/java/io/metersphere/api/service/ApiAutomationService.java b/backend/src/main/java/io/metersphere/api/service/ApiAutomationService.java index c015d4c19b..7f15aff6e6 100644 --- a/backend/src/main/java/io/metersphere/api/service/ApiAutomationService.java +++ b/backend/src/main/java/io/metersphere/api/service/ApiAutomationService.java @@ -217,7 +217,7 @@ public class ApiAutomationService { ids.add(scenarioId); deleteApiScenarioReport(ids); - scheduleService.deleteByResourceId(scenarioId); + scheduleService.deleteScheduleAndJobByResourceId(scenarioId,ScheduleGroup.API_SCENARIO_TEST.name()); TestPlanApiScenarioExample example = new TestPlanApiScenarioExample(); example.createCriteria().andApiScenarioIdEqualTo(scenarioId); List testPlanApiScenarioList = testPlanApiScenarioMapper.selectByExample(example); @@ -282,6 +282,10 @@ public class ApiAutomationService { public void removeToGc(List apiIds) { extApiScenarioMapper.removeToGc(apiIds); + //将这些场景的定时任务删除掉 + for (String id : apiIds) { + scheduleService.deleteScheduleAndJobByResourceId(id,ScheduleGroup.API_SCENARIO_TEST.name()); + } } public void reduction(List requests) { diff --git a/backend/src/main/java/io/metersphere/base/mapper/ScheduleMapper.java b/backend/src/main/java/io/metersphere/base/mapper/ScheduleMapper.java index 8d1d4b68ec..264e2f2e58 100644 --- a/backend/src/main/java/io/metersphere/base/mapper/ScheduleMapper.java +++ b/backend/src/main/java/io/metersphere/base/mapper/ScheduleMapper.java @@ -33,5 +33,4 @@ public interface ScheduleMapper { int updateByPrimaryKeyWithBLOBs(Schedule record); int updateByPrimaryKey(Schedule record); - } \ No newline at end of file diff --git a/backend/src/main/java/io/metersphere/service/ScheduleService.java b/backend/src/main/java/io/metersphere/service/ScheduleService.java index d7d002c4e4..cc30924d46 100644 --- a/backend/src/main/java/io/metersphere/service/ScheduleService.java +++ b/backend/src/main/java/io/metersphere/service/ScheduleService.java @@ -18,10 +18,7 @@ import io.metersphere.commons.utils.SessionUtils; import io.metersphere.controller.request.OrderRequest; import io.metersphere.controller.request.QueryScheduleRequest; import io.metersphere.dto.ScheduleDao; -import io.metersphere.job.sechedule.ApiScenarioTestJob; -import io.metersphere.job.sechedule.ApiTestJob; -import io.metersphere.job.sechedule.ScheduleManager; -import io.metersphere.job.sechedule.TestPlanTestJob; +import io.metersphere.job.sechedule.*; import org.apache.commons.lang3.StringUtils; import org.quartz.JobKey; import org.quartz.SchedulerException; @@ -98,6 +95,25 @@ public class ScheduleService { return scheduleMapper.deleteByExample(scheduleExample); } + public int deleteScheduleAndJobByResourceId(String resourceId,String group) { + ScheduleExample scheduleExample = new ScheduleExample(); + scheduleExample.createCriteria().andResourceIdEqualTo(resourceId); + removeJob(resourceId,group); + return scheduleMapper.deleteByExample(scheduleExample); + } + + public void removeJob(String resourceId,String group) { + if(StringUtils.equals(ScheduleGroup.API_SCENARIO_TEST.name(),group)){ + scheduleManager.removeJob(ApiScenarioTestJob.getJobKey(resourceId), ApiScenarioTestJob.getTriggerKey(resourceId)); + }else if(StringUtils.equals(ScheduleGroup.TEST_PLAN_TEST.name(),group)){ + scheduleManager.removeJob(TestPlanTestJob.getJobKey(resourceId), TestPlanTestJob.getTriggerKey(resourceId)); + }else if(StringUtils.equals(ScheduleGroup.SWAGGER_IMPORT.name(),group)){ + scheduleManager.removeJob(SwaggerUrlImportJob.getJobKey(resourceId), SwaggerUrlImportJob.getTriggerKey(resourceId)); + }else{ + scheduleManager.removeJob(ApiTestJob.getJobKey(resourceId), ApiTestJob.getTriggerKey(resourceId)); + } + } + public List listSchedule() { ScheduleExample example = new ScheduleExample(); return scheduleMapper.selectByExample(example); diff --git a/backend/src/main/java/io/metersphere/track/service/TestPlanService.java b/backend/src/main/java/io/metersphere/track/service/TestPlanService.java index 2c2a9626f0..f245d46f98 100644 --- a/backend/src/main/java/io/metersphere/track/service/TestPlanService.java +++ b/backend/src/main/java/io/metersphere/track/service/TestPlanService.java @@ -28,6 +28,7 @@ import io.metersphere.dto.BaseSystemConfigDTO; import io.metersphere.i18n.Translator; import io.metersphere.notice.sender.NoticeModel; import io.metersphere.notice.service.NoticeSendService; +import io.metersphere.service.ScheduleService; import io.metersphere.service.SystemParameterService; import io.metersphere.track.Factory.ReportComponentFactory; import io.metersphere.track.domain.ReportComponent; @@ -62,6 +63,8 @@ public class TestPlanService { @Resource ExtTestPlanMapper extTestPlanMapper; @Resource + ScheduleService scheduleService; + @Resource ExtTestPlanTestCaseMapper extTestPlanTestCaseMapper; @Resource TestCaseMapper testCaseMapper; @@ -315,6 +318,10 @@ public class TestPlanService { testPlanProjectService.deleteTestPlanProjectByPlanId(planId); testPlanApiCaseService.deleteByPlanId(planId); testPlanScenarioCaseService.deleteByPlanId(planId); + + //删除定时任务 + scheduleService.deleteScheduleAndJobByResourceId(planId,ScheduleGroup.TEST_PLAN_TEST.name()); + int num = testPlanMapper.deleteByPrimaryKey(planId); List relatedUsers = new ArrayList<>(); AddTestPlanRequest testPlans = new AddTestPlanRequest();