fix(测试计划定时任务): 每次保存定时任务时重置定时任务所需要的参数

每次保存定时任务时重置定时任务所需要的参数
This commit is contained in:
song-tianyang 2021-12-01 13:22:42 +08:00 committed by song-tianyang
parent 40a000ac8a
commit dde976d69b
3 changed files with 42 additions and 36 deletions

View File

@ -125,14 +125,6 @@ public class ScheduleManager {
trigger = (CronTrigger) triggerBuilder.build();// 创建Trigger对象
scheduler.rescheduleJob(triggerKey, trigger);// 修改一个任务的触发时间
/** 方式一 :调用 rescheduleJob 结束 */
/** 方式二先删除然后在创建一个新的Job */
// JobDetail jobDetail = sched.getJobDetail(JobKey.jobKey(jobName, jobGroupName));
// Class<? extends Job> jobClass = jobDetail.getJobClass();
// removeJob(jobName, jobGroupName, triggerName, triggerGroupName);
// addJob(jobName, jobGroupName, triggerName, triggerGroupName, jobClass, cron);
/** 方式二 先删除然后在创建一个新的Job */
}
} catch (Exception e) {
throw new RuntimeException(e);

View File

@ -67,7 +67,7 @@ public class ScheduleService {
swaggerUrlProjectMapper.updateByPrimaryKeySelective(swaggerUrlProject);
}
public ApiSwaggerUrlDTO selectApiSwaggerUrlDTO(String id){
public ApiSwaggerUrlDTO selectApiSwaggerUrlDTO(String id) {
return extScheduleMapper.select(id);
}
@ -120,13 +120,13 @@ public class ScheduleService {
}
private void removeJob(String resourceId, String group) {
if(StringUtils.equals(ScheduleGroup.API_SCENARIO_TEST.name(), 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)){
} 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)){
} else if (StringUtils.equals(ScheduleGroup.SWAGGER_IMPORT.name(), group)) {
scheduleManager.removeJob(SwaggerUrlImportJob.getJobKey(resourceId), SwaggerUrlImportJob.getTriggerKey(resourceId));
} else if(StringUtils.equals(ScheduleGroup.PERFORMANCE_TEST.name(), group)){
} else if (StringUtils.equals(ScheduleGroup.PERFORMANCE_TEST.name(), group)) {
scheduleManager.removeJob(PerformanceTestJob.getJobKey(resourceId), PerformanceTestJob.getTriggerKey(resourceId));
} else {
scheduleManager.removeJob(ApiTestJob.getJobKey(resourceId), ApiTestJob.getTriggerKey(resourceId));
@ -174,6 +174,22 @@ public class ScheduleService {
return schedule;
}
public void resetJob(Schedule request, JobKey jobKey, TriggerKey triggerKey, Class clazz) {
try {
scheduleManager.removeJob(jobKey, triggerKey);
} catch (Exception e) {
LogUtil.error(e);
MSException.throwException("重置定时任务-删除时异常");
}
try {
scheduleManager.addCronJob(jobKey, triggerKey, clazz, request.getValue(),
scheduleManager.getDefaultJobDataMap(request, request.getValue(), SessionUtils.getUser().getId()));
} catch (Exception e) {
LogUtil.error(e);
MSException.throwException("重置定时任务-删除时异常");
}
}
public void addOrUpdateCronJob(Schedule request, JobKey jobKey, TriggerKey triggerKey, Class clazz) {
Boolean enable = request.getEnable();
String cronExpression = request.getValue();
@ -214,7 +230,7 @@ public class ScheduleService {
}
public long countTaskByProjectId(String projectId) {
return extScheduleMapper.countTaskByProjectId(projectId);
return extScheduleMapper.countTaskByProjectId(projectId);
}
public long countTaskByProjectIdInThisWeek(String projectId) {
@ -223,16 +239,16 @@ public class ScheduleService {
Date firstTime = startAndEndDateInWeek.get("firstTime");
Date lastTime = startAndEndDateInWeek.get("lastTime");
if(firstTime==null || lastTime == null){
return 0;
}else {
return extScheduleMapper.countTaskByProjectIdAndCreateTimeRange(projectId,firstTime.getTime(),lastTime.getTime());
if (firstTime == null || lastTime == null) {
return 0;
} else {
return extScheduleMapper.countTaskByProjectIdAndCreateTimeRange(projectId, firstTime.getTime(), lastTime.getTime());
}
}
public List<TaskInfoResult> findRunningTaskInfoByProjectID(String projectID, BaseQueryRequest request) {
List<TaskInfoResult> runningTaskInfoList = extScheduleMapper.findRunningTaskInfoByProjectID(projectID, request);
return runningTaskInfoList;
return runningTaskInfoList;
}
public void createSchedule(ScheduleRequest request) {
@ -242,7 +258,7 @@ public class ScheduleService {
JobKey jobKey = null;
TriggerKey triggerKey = null;
Class clazz = null;
if("testPlan".equals(request.getScheduleFrom())){
if ("testPlan".equals(request.getScheduleFrom())) {
TestPlan testPlan = testPlanMapper.selectByPrimaryKey(request.getResourceId());
schedule.setName(testPlan.getName());
schedule.setProjectId(testPlan.getProjectId());
@ -252,7 +268,7 @@ public class ScheduleService {
triggerKey = TestPlanTestJob.getTriggerKey(request.getResourceId());
clazz = TestPlanTestJob.class;
schedule.setJob(TestPlanTestJob.class.getName());
}else {
} else {
//默认为情景
ApiScenarioWithBLOBs apiScene = apiScenarioMapper.selectByPrimaryKey(request.getResourceId());
schedule.setName(apiScene.getName());
@ -266,21 +282,23 @@ public class ScheduleService {
}
this.addSchedule(schedule);
this.addOrUpdateCronJob(request,jobKey ,triggerKey , clazz);
this.addOrUpdateCronJob(request, jobKey, triggerKey, clazz);
}
public void updateSchedule(Schedule request) {
JobKey jobKey = null;
TriggerKey triggerKey = null;
Class clazz = null;
if(ScheduleGroup.TEST_PLAN_TEST.name().equals(request.getGroup())){
//测试计划的定时任务修改会修改任务的配置信息并不只是单纯的修改定时任务时间需要重新配置这个定时任务
boolean needResetJob = false;
if (ScheduleGroup.TEST_PLAN_TEST.name().equals(request.getGroup())) {
jobKey = TestPlanTestJob.getJobKey(request.getResourceId());
triggerKey = TestPlanTestJob.getTriggerKey(request.getResourceId());
clazz = TestPlanTestJob.class;
request.setJob(TestPlanTestJob.class.getName());
}else {
needResetJob = true;
} else {
//默认为情景
jobKey = ApiScenarioTestJob.getJobKey(request.getResourceId());
triggerKey = ApiScenarioTestJob.getTriggerKey(request.getResourceId());
@ -289,7 +307,12 @@ public class ScheduleService {
}
this.editSchedule(request);
this.addOrUpdateCronJob(request, jobKey, triggerKey, clazz);
if (needResetJob) {
this.resetJob(request, jobKey, triggerKey, clazz);
} else {
this.addOrUpdateCronJob(request, jobKey, triggerKey, clazz);
}
}
public Object getCurrentlyExecutingJobs() {

View File

@ -1086,15 +1086,6 @@ public class TestPlanService {
runModeConfig.setEnvMap(new HashMap<>());
runModeConfig.setOnSampleError(false);
} else {
try {
JSONObject runModeObj = JSONObject.parseObject(apiRunConfig);
if (runModeObj.containsKey("runWithinResourcePool") && !runModeObj.getBoolean("runWithinResourcePool")) {
runModeConfig.setResourcePoolId(null);
}
} catch (Exception e) {
LogUtil.error(e.getMessage());
}
if (runModeConfig.getEnvMap() == null) {
runModeConfig.setEnvMap(new HashMap<>());
}