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();