[ server ] [ execution ]- optimize some controller - 1

This commit is contained in:
zeaslity
2023-01-16 17:11:43 +08:00
parent 9332ca5533
commit 41396e024c
13 changed files with 523 additions and 200 deletions

View File

@@ -8,7 +8,7 @@ import org.springframework.scheduling.quartz.QuartzJobBean;
import javax.annotation.Resource;
public class MonitorAllAgentStatusJob extends QuartzJobBean {
public class AgentStatusMonitorJob extends QuartzJobBean {
@Resource
MonitorAllAgentStatus monitorAllAgentStatus;

View File

@@ -2,7 +2,7 @@ package io.wdd.rpc.scheduler.service;
import io.wdd.rpc.scheduler.job.AgentRunMetricStatusJob;
import io.wdd.rpc.scheduler.job.MonitorAllAgentStatusJob;
import io.wdd.rpc.scheduler.job.AgentStatusMonitorJob;
import lombok.extern.slf4j.Slf4j;
import org.quartz.CronExpression;
import org.springframework.beans.factory.annotation.Value;
@@ -22,7 +22,7 @@ import static io.wdd.rpc.status.AgentRuntimeMetricStatus.METRIC_REPORT_TIME_PINC
public class BuildStatusScheduleTask {
@Resource
OctopusQuartzService octopusQuartzService;
QuartzSchedulerService octopusQuartzService;
@Value(value = "${octopus.status.healthy.cron}")
String healthyCronTimeExpress;
@@ -99,7 +99,7 @@ public class BuildStatusScheduleTask {
// build the Job
octopusQuartzService.addJob(
MonitorAllAgentStatusJob.class,
AgentStatusMonitorJob.class,
"monitorAllAgentStatusJob",
JOB_GROUP_NAME,
healthyCheckStartDelaySeconds,

View File

@@ -7,7 +7,8 @@ import java.util.List;
import java.util.Map;
public interface OctopusQuartzService {
public interface QuartzSchedulerService {
boolean addJob(OctopusQuartzJob quartzJob);
@@ -43,13 +44,14 @@ public interface OctopusQuartzService {
void updateJob(String jobName, String jobGroupName, String jobTime);
/**
* 删除一个任务job
*
* @param jobName
* @param jobGroupName
* @return
*/
void deleteJob(String jobName, String jobGroupName);
boolean deleteJob(String jobName, String jobGroupName);
/**
* 暂停一个任务job
@@ -85,6 +87,10 @@ public interface OctopusQuartzService {
List<Map<String, Object>> queryRunJob();
/**
* 获取所有的触发器
*
* */
List<Trigger> queryAllTrigger();

View File

@@ -24,7 +24,7 @@ import static org.quartz.TriggerBuilder.newTrigger;
*/
@Slf4j
@Service
public class OctopusQuartzServiceImpl implements OctopusQuartzService {
public class QuartzSchedulerServiceImpl implements QuartzSchedulerService {
@Autowired
private Scheduler scheduler;
@@ -48,12 +48,12 @@ public class OctopusQuartzServiceImpl implements OctopusQuartzService {
/**
* 增加一个job
*
* @param jobClass 任务实现类
* @param jobName 任务名称
* @param jobGroupName 任务组名
* @param jobRunTimePinch 时间表达式 (这是每隔多少秒为一次任务)
* @param jobRunRepeatTimes 运行的次数 <0:表示不限次数
* @param jobData 参数
* @param jobClass 任务实现类
* @param jobName 任务名称
* @param jobGroupName 任务组名
* @param jobRunTimePinch 时间表达式 (这是每隔多少秒为一次任务)
* @param jobRunRepeatTimes 运行的次数 <0:表示不限次数
* @param jobData 参数
*/
@Override
public void addJob(Class<? extends QuartzJobBean> jobClass, String jobName, String jobGroupName, int jobRunTimePinch, int jobRunRepeatTimes, Map jobData) {
@@ -61,17 +61,25 @@ public class OctopusQuartzServiceImpl implements OctopusQuartzService {
// 任务名称和组构成任务key
JobDetail jobDetail = JobBuilder
.newJob(jobClass)
.withIdentity(jobName, jobGroupName)
.withIdentity(
jobName,
jobGroupName
)
.build();
// 设置job参数
if (jobData != null && jobData.size() > 0) {
jobDetail.getJobDataMap().putAll(jobData);
jobDetail
.getJobDataMap()
.putAll(jobData);
}
// 使用simpleTrigger规则
Trigger trigger = newTrigger()
.withIdentity(jobName, jobGroupName)
.withIdentity(
jobName,
jobGroupName
)
.withSchedule(
SimpleScheduleBuilder
.repeatSecondlyForTotalCount(
@@ -82,9 +90,17 @@ public class OctopusQuartzServiceImpl implements OctopusQuartzService {
.startNow()
.build();
log.debug("jobDataMap: {}", jobDetail.getJobDataMap().getWrappedMap());
log.debug(
"jobDataMap: {}",
jobDetail
.getJobDataMap()
.getWrappedMap()
);
scheduler.scheduleJob(jobDetail, trigger);
scheduler.scheduleJob(
jobDetail,
trigger
);
} catch (SchedulerException e) {
e.printStackTrace();
@@ -108,10 +124,18 @@ public class OctopusQuartzServiceImpl implements OctopusQuartzService {
// 创建jobDetail实例绑定Job实现类
// 指明job的名称所在组的名称以及绑定job类
// 任务名称和组构成任务key
JobDetail jobDetail = JobBuilder.newJob(jobClass).withIdentity(jobName, jobGroupName).build();
JobDetail jobDetail = JobBuilder
.newJob(jobClass)
.withIdentity(
jobName,
jobGroupName
)
.build();
// 设置job参数
if (jobData != null && jobData.size() > 0) {
jobDetail.getJobDataMap().putAll(jobData);
jobDetail
.getJobDataMap()
.putAll(jobData);
}
// 定义调度触发规则
@@ -124,9 +148,15 @@ public class OctopusQuartzServiceImpl implements OctopusQuartzService {
}
Trigger trigger = newTrigger()
.withIdentity(jobName, jobGroupName)
.withIdentity(
jobName,
jobGroupName
)
.startAt(
DateBuilder.futureDate(startTime, IntervalUnit.SECOND)
DateBuilder.futureDate(
startTime,
IntervalUnit.SECOND
)
)
.withSchedule(
CronScheduleBuilder.cronSchedule(cronJobExpression)
@@ -135,8 +165,14 @@ public class OctopusQuartzServiceImpl implements OctopusQuartzService {
.build();
// 把作业和触发器注册到任务调度中
scheduler.scheduleJob(jobDetail, trigger);
log.info("jobDataMap: {}", jobDetail.getJobDataMap());
scheduler.scheduleJob(
jobDetail,
trigger
);
log.info(
"jobDataMap: {}",
jobDetail.getJobDataMap()
);
} catch (Exception e) {
e.printStackTrace();
@@ -154,12 +190,25 @@ public class OctopusQuartzServiceImpl implements OctopusQuartzService {
@Override
public void updateJob(String jobName, String jobGroupName, String jobTime) {
try {
TriggerKey triggerKey = TriggerKey.triggerKey(jobName, jobGroupName);
TriggerKey triggerKey = TriggerKey.triggerKey(
jobName,
jobGroupName
);
CronTrigger trigger = (CronTrigger) scheduler.getTrigger(triggerKey);
log.info("new jobTime: {}", jobTime);
trigger = trigger.getTriggerBuilder().withIdentity(triggerKey).withSchedule(CronScheduleBuilder.cronSchedule(jobTime)).build();
log.info(
"new jobTime: {}",
jobTime
);
trigger = trigger
.getTriggerBuilder()
.withIdentity(triggerKey)
.withSchedule(CronScheduleBuilder.cronSchedule(jobTime))
.build();
// 重启触发器
scheduler.rescheduleJob(triggerKey, trigger);
scheduler.rescheduleJob(
triggerKey,
trigger
);
} catch (SchedulerException e) {
e.printStackTrace();
throw new MyRuntimeException("update job error!");
@@ -171,15 +220,22 @@ public class OctopusQuartzServiceImpl implements OctopusQuartzService {
*
* @param jobName 任务名称
* @param jobGroupName 任务组名
* @return
*/
@Override
public void deleteJob(String jobName, String jobGroupName) {
public boolean deleteJob(String jobName, String jobGroupName) {
try {
scheduler.deleteJob(new JobKey(jobName, jobGroupName));
scheduler.deleteJob(new JobKey(
jobName,
jobGroupName
));
return true;
} catch (Exception e) {
e.printStackTrace();
throw new MyRuntimeException("delete job error!");
}
}
/**
@@ -191,7 +247,10 @@ public class OctopusQuartzServiceImpl implements OctopusQuartzService {
@Override
public void pauseJob(String jobName, String jobGroupName) {
try {
JobKey jobKey = JobKey.jobKey(jobName, jobGroupName);
JobKey jobKey = JobKey.jobKey(
jobName,
jobGroupName
);
scheduler.pauseJob(jobKey);
} catch (SchedulerException e) {
e.printStackTrace();
@@ -208,7 +267,10 @@ public class OctopusQuartzServiceImpl implements OctopusQuartzService {
@Override
public void resumeJob(String jobName, String jobGroupName) {
try {
JobKey jobKey = JobKey.jobKey(jobName, jobGroupName);
JobKey jobKey = JobKey.jobKey(
jobName,
jobGroupName
);
scheduler.resumeJob(jobKey);
} catch (SchedulerException e) {
e.printStackTrace();
@@ -225,7 +287,10 @@ public class OctopusQuartzServiceImpl implements OctopusQuartzService {
@Override
public void runAJobNow(String jobName, String jobGroupName) {
try {
JobKey jobKey = JobKey.jobKey(jobName, jobGroupName);
JobKey jobKey = JobKey.jobKey(
jobName,
jobGroupName
);
scheduler.triggerJob(jobKey);
} catch (SchedulerException e) {
e.printStackTrace();
@@ -246,19 +311,40 @@ public class OctopusQuartzServiceImpl implements OctopusQuartzService {
Set<JobKey> jobKeys = scheduler.getJobKeys(matcher);
jobList = new ArrayList<Map<String, Object>>();
for (JobKey jobKey : jobKeys) {
log.info("maps: {}", scheduler.getJobDetail(jobKey).getJobDataMap().getWrappedMap());
log.info(
"maps: {}",
scheduler
.getJobDetail(jobKey)
.getJobDataMap()
.getWrappedMap()
);
List<? extends Trigger> triggers = scheduler.getTriggersOfJob(jobKey);
for (Trigger trigger : triggers) {
Map<String, Object> map = new HashMap<>();
map.put("jobName", jobKey.getName());
map.put("jobGroupName", jobKey.getGroup());
map.put("description", "触发器:" + trigger.getKey());
map.put(
"jobName",
jobKey.getName()
);
map.put(
"jobGroupName",
jobKey.getGroup()
);
map.put(
"description",
"触发器:" + trigger.getKey()
);
Trigger.TriggerState triggerState = scheduler.getTriggerState(trigger.getKey());
map.put("jobStatus", triggerState.name());
map.put(
"jobStatus",
triggerState.name()
);
if (trigger instanceof CronTrigger) {
CronTrigger cronTrigger = (CronTrigger) trigger;
String cronExpression = cronTrigger.getCronExpression();
map.put("jobTime", cronExpression);
map.put(
"jobTime",
cronExpression
);
}
jobList.add(map);
}
@@ -286,15 +372,30 @@ public class OctopusQuartzServiceImpl implements OctopusQuartzService {
JobDetail jobDetail = executingJob.getJobDetail();
JobKey jobKey = jobDetail.getKey();
Trigger trigger = executingJob.getTrigger();
map.put("jobName", jobKey.getName());
map.put("jobGroupName", jobKey.getGroup());
map.put("description", "触发器:" + trigger.getKey());
map.put(
"jobName",
jobKey.getName()
);
map.put(
"jobGroupName",
jobKey.getGroup()
);
map.put(
"description",
"触发器:" + trigger.getKey()
);
Trigger.TriggerState triggerState = scheduler.getTriggerState(trigger.getKey());
map.put("jobStatus", triggerState.name());
map.put(
"jobStatus",
triggerState.name()
);
if (trigger instanceof CronTrigger) {
CronTrigger cronTrigger = (CronTrigger) trigger;
String cronExpression = cronTrigger.getCronExpression();
map.put("jobTime", cronExpression);
map.put(
"jobTime",
cronExpression
);
}
jobList.add(map);
}
@@ -310,17 +411,21 @@ public class OctopusQuartzServiceImpl implements OctopusQuartzService {
try {
return scheduler.getTriggerKeys(
GroupMatcher.groupEquals(JOB_GROUP_NAME)
).stream().map(
triggerKey -> {
try {
return scheduler.getTrigger(triggerKey);
} catch (SchedulerException e) {
throw new RuntimeException(e);
}
}
).collect(Collectors.toList());
return scheduler
.getTriggerKeys(
GroupMatcher.groupEquals(JOB_GROUP_NAME)
)
.stream()
.map(
triggerKey -> {
try {
return scheduler.getTrigger(triggerKey);
} catch (SchedulerException e) {
throw new RuntimeException(e);
}
}
)
.collect(Collectors.toList());
} catch (SchedulerException e) {
throw new RuntimeException(e);