[ server ] - monitor all agent status

This commit is contained in:
zeaslity
2023-01-10 17:28:26 +08:00
parent 0c4531dccf
commit b7958f5c78
16 changed files with 702 additions and 64 deletions

View File

@@ -0,0 +1,43 @@
package io.wdd.rpc.scheduler.beans;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import java.io.Serializable;
@Data
public class OctopusQuartzJob implements Serializable {
public static final String JOB_KEY = "JOB_KEY";
@ApiModelProperty(value = "ID")
private Long id;
@ApiModelProperty(value = "用于子任务唯一标识", hidden = true)
private String uuid;
@ApiModelProperty(value = "任务名称")
private String jobName;
@ApiModelProperty(value = "Bean名称")
private String beanName;
@ApiModelProperty(value = "方法名称")
private String methodName;
@ApiModelProperty(value = "参数")
private String params;
@ApiModelProperty(value = "cron表达式")
private String cronExpression;
@ApiModelProperty(value = "状态,暂时或启动")
private Boolean isPause = false;
@ApiModelProperty(value = "子任务")
private String subTask;
@ApiModelProperty(value = "失败后暂停")
private Boolean pauseAfterFailure;
}

View File

@@ -0,0 +1,47 @@
package io.wdd.rpc.scheduler.beans;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import java.io.Serializable;
import java.time.LocalDateTime;
@Data
public class OctopusQuartzLog implements Serializable {
@ApiModelProperty(value = "ID", hidden = true)
@TableId(type = IdType.ASSIGN_ID)
private Long id;
@ApiModelProperty(value = "任务名称", hidden = true)
private String jobName;
@ApiModelProperty(value = "bean名称", hidden = true)
private String beanName;
@ApiModelProperty(value = "方法名称", hidden = true)
private String methodName;
@ApiModelProperty(value = "参数", hidden = true)
private String params;
@ApiModelProperty(value = "cron表达式", hidden = true)
private String cronExpression;
@ApiModelProperty(value = "状态", hidden = true)
private Boolean isSuccess;
@ApiModelProperty(value = "异常详情", hidden = true)
private String exceptionDetail;
@ApiModelProperty(value = "执行耗时", hidden = true)
private Long time;
@ApiModelProperty(value = "创建时间", hidden = true)
private LocalDateTime createTime;
}

View File

@@ -0,0 +1,18 @@
package io.wdd.rpc.scheduler.call;
import io.wdd.common.beans.status.OctopusStatusMessage;
import io.wdd.rpc.status.CollectAgentStatus;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
@Service
public class CallAgentQuartzService {
@Resource
CollectAgentStatus collectAgentStatus;
}

View File

@@ -0,0 +1,61 @@
package io.wdd.rpc.scheduler.config;
import io.wdd.common.handler.MyRuntimeException;
import io.wdd.rpc.scheduler.beans.OctopusQuartzJob;
import io.wdd.server.utils.SpringUtils;
import org.apache.commons.lang3.StringUtils;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.quartz.QuartzJobBean;
import org.springframework.util.ReflectionUtils;
import java.lang.reflect.Method;
@Async
public class ExecutionJob extends QuartzJobBean {
@Override
protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
//通过JobExecutionContext对象得到OctopusQuartzJob实例。
OctopusQuartzJob octopusQuartzJob = (OctopusQuartzJob)
context.getMergedJobDataMap().get(
io.wdd.rpc.scheduler.beans.OctopusQuartzJob.JOB_KEY);
//反射获取到方法,并执行。
runMethod(octopusQuartzJob.getBeanName(), octopusQuartzJob.getMethodName(), octopusQuartzJob.getParams());
}
/***
* description:反射执行方法
*
* @author: zeaslity
*/
public void runMethod(String beanName, String methodName, String params) {
Object target = SpringUtils.getBean(beanName);
Method method = null;
try {
//执行的方法只能有两种有String参数或者无参数毕竟前端只能传字符串参数给后端。
if (StringUtils.isNotBlank(params)) {
//反射获取到方法 两个参数 分别是方法名和参数类型
method = target.getClass().getDeclaredMethod(methodName, String.class);
} else {
method = target.getClass().getDeclaredMethod(methodName);
}
//反射执行方法
ReflectionUtils.makeAccessible(method);
if (StringUtils.isNotBlank(params)) {
method.invoke(target, params);
} else {
method.invoke(target);
}
} catch (Exception e) {
throw new MyRuntimeException("定时任务执行失败");
}
}
}

View File

@@ -0,0 +1,49 @@
package io.wdd.rpc.scheduler.config;
import io.wdd.server.utils.SpringUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.ReflectionUtils;
import java.lang.reflect.Method;
import java.util.concurrent.Callable;
public class QuartzRunnable implements Callable<Object> {
private final Object target;
private final Method method;
private final String params;
QuartzRunnable(String beanName, String methodName, String params) throws NoSuchMethodException, ClassNotFoundException {
//获取到bean对象
this.target = SpringUtils.getBean(beanName);
//获取到参数
this.params = params;
//如果参数不为空
if (StringUtils.isNotBlank(params)) {
//反射获取到方法 两个参数 分别是方法名和参数类型
this.method = target.getClass().getDeclaredMethod(methodName, String.class);
} else {
this.method = target.getClass().getDeclaredMethod(methodName);
}
}
/***
* description: 线程回调函数 反射执行方法
*
* @author: lixiangxiang
*/
@Override
public Object call() throws Exception {
ReflectionUtils.makeAccessible(method);
if (StringUtils.isNotBlank(params)) {
method.invoke(target, params);
} else {
method.invoke(target);
}
return null;
}
}

View File

@@ -1,5 +1,6 @@
package io.wdd.rpc.scheduler.service;
import io.wdd.rpc.scheduler.beans.OctopusQuartzJob;
import org.springframework.scheduling.quartz.QuartzJobBean;
import java.util.List;
import java.util.Map;
@@ -7,6 +8,8 @@ import java.util.Map;
public interface OctopusQuartzService {
boolean addJob(OctopusQuartzJob quartzJob);
/**
* 增加一个任务job
* @param jobClass 任务job实现类
@@ -16,8 +19,7 @@ public interface OctopusQuartzService {
* @param jobTimes 任务运行次数(若<0则不限次数
* @param jobData 任务参数
*/
void addJob(Class<? extends QuartzJobBean> jobClass, String jobName, String jobGroupName, int jobTime,
int jobTimes, Map jobData);
void addJob(Class<? extends QuartzJobBean> jobClass, String jobName, String jobGroupName, int jobTime, int jobTimes, Map jobData);
/**
* 增加一个任务job
@@ -37,6 +39,8 @@ public interface OctopusQuartzService {
*/
void updateJob(String jobName, String jobGroupName, String jobTime);
/**
* 删除一个任务job
* @param jobName

View File

@@ -1,10 +1,12 @@
package io.wdd.rpc.scheduler.service;
import io.wdd.common.handler.MyRuntimeException;
import io.wdd.rpc.scheduler.beans.OctopusQuartzJob;
import lombok.extern.slf4j.Slf4j;
import org.quartz.*;
import org.quartz.DateBuilder.IntervalUnit;
import org.quartz.impl.matchers.GroupMatcher;
import org.quartz.impl.triggers.CronTriggerImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.QuartzJobBean;
import org.springframework.stereotype.Service;
@@ -12,6 +14,8 @@ import org.springframework.stereotype.Service;
import javax.annotation.PostConstruct;
import java.util.*;
import static org.quartz.TriggerBuilder.newTrigger;
/**
* @author Andya
* @date 2021/4/01
@@ -32,6 +36,39 @@ public class OctopusQuartzServiceImpl implements OctopusQuartzService {
}
}
@Override
public boolean addJob(OctopusQuartzJob quartzJob) {
try {
// 构建jobDetail,并与PrintHelloJob类绑定(Job执行内容)
JobDetail jobDetail = JobBuilder
.newJob(PrintHelloJob.class)
.withIdentity(quartzJob.getUuid())
.build();
//通过触发器名和cron表达式创建Trigger
Trigger cronTrigger = newTrigger()
.withIdentity(quartzJob.getUuid())
.startNow()
.withSchedule(CronScheduleBuilder.cronSchedule(quartzJob.getCronExpression()))
.build();
//把job信息放入jobDataMap中 job_key为标识
cronTrigger.getJobDataMap().put(OctopusQuartzJob.JOB_KEY, quartzJob);
//重置启动时间
((CronTriggerImpl)cronTrigger).setStartTime(new Date());
//执行定时任务
scheduler.scheduleJob(jobDetail, cronTrigger);
} catch (Exception e) {
log.error("【创建定时任务失败】 定时任务id{}", quartzJob.getId());
return false;
}
return true;
}
/**
* 增加一个job
*
@@ -62,12 +99,11 @@ public class OctopusQuartzServiceImpl implements OctopusQuartzService {
// 使用simpleTrigger规则
Trigger trigger = null;
if (jobTimes < 0) {
trigger = TriggerBuilder.newTrigger().withIdentity(jobName, jobGroupName)
trigger = newTrigger().withIdentity(jobName, jobGroupName)
.withSchedule(SimpleScheduleBuilder.repeatSecondlyForever(1).withIntervalInSeconds(jobTime))
.startNow().build();
} else {
trigger = TriggerBuilder
.newTrigger().withIdentity(jobName, jobGroupName).withSchedule(SimpleScheduleBuilder
trigger = newTrigger().withIdentity(jobName, jobGroupName).withSchedule(SimpleScheduleBuilder
.repeatSecondlyForever(1).withIntervalInSeconds(jobTime).withRepeatCount(jobTimes))
.startNow().build();
}
@@ -108,7 +144,7 @@ public class OctopusQuartzServiceImpl implements OctopusQuartzService {
// 定义调度触发规则
// 使用cornTrigger规则
// 触发器key
Trigger trigger = TriggerBuilder.newTrigger().withIdentity(jobName, jobGroupName)
Trigger trigger = newTrigger().withIdentity(jobName, jobGroupName)
.startAt(DateBuilder.futureDate(1, IntervalUnit.SECOND))
.withSchedule(CronScheduleBuilder.cronSchedule(jobTime)).startNow().build();
// 把作业和触发器注册到任务调度中

View File

@@ -0,0 +1,16 @@
package io.wdd.rpc.scheduler.service;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
public class PrintHelloJob implements Job {
@Override
public void execute(JobExecutionContext context) throws JobExecutionException {
System.out.println();
System.out.println("PrintHelloJob被执行了");
System.out.println("context = " + context);
System.out.println();
}
}