[server] [ scheduler.script ] - 优化定时脚本任务部分的代码

This commit is contained in:
zeaslity
2023-02-16 14:21:17 +08:00
parent 864d290fe7
commit 2f8f586b0e
13 changed files with 133 additions and 93 deletions

View File

@@ -34,7 +34,9 @@
141.147.106.62 London-amd64-02 london-2 l2 141.147.106.62 London-amd64-02 london-2 l2
132.145.12.135 London-arm64-01 london-3 l3
132.145.28.48 London-arm64-02 london-4 l4 132.145.28.48 London-arm64-02 london-4 l4
# 所有的主机 # 所有的主机
Phoenix-amd64-02,Phoenix-arm64-01,Phoenix-arm64-02,London-amd64-01,London-amd64-02,London-arm64-02 Phoenix-amd64-02,Phoenix-arm64-01,Phoenix-arm64-02,London-amd64-01,London-amd64-02,London-arm64-01,London-arm64-02

View File

@@ -40,7 +40,7 @@ public class XrayController {
) )
); );
/*ArrayList<ProxyNode> pathB = new ArrayList<>( ArrayList<ProxyNode> pathB = new ArrayList<>(
Arrays.asList( Arrays.asList(
shanghai, shanghai,
seoul2, seoul2,
@@ -53,9 +53,16 @@ public class XrayController {
seoul2, seoul2,
phoenix2 phoenix2
) )
);*/ );
allNetworkPathList.add(pathA); ArrayList<ProxyNode> pathD = new ArrayList<>(
Arrays.asList(
chengduAgent
)
);
// allNetworkPathList.add(pathA);
allNetworkPathList.add(pathD);
// allNetworkPathList.add(pathB); // allNetworkPathList.add(pathB);
// allNetworkPathList.add(pathC); // allNetworkPathList.add(pathC);
@@ -63,6 +70,8 @@ public class XrayController {
xrayConfigDistribute.uploadXrayConfigToOSS(allNetworkPathList.get(0)); xrayConfigDistribute.uploadXrayConfigToOSS(allNetworkPathList.get(0));
xrayConfigDistribute.buildXrayUpdateResult(allNetworkPathList.get(0));
System.out.println("结束!"); System.out.println("结束!");
} }

View File

@@ -7,8 +7,6 @@ import lombok.Data;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
import lombok.experimental.SuperBuilder; import lombok.experimental.SuperBuilder;
import java.io.File;
@Data @Data
@AllArgsConstructor @AllArgsConstructor
@NoArgsConstructor @NoArgsConstructor
@@ -26,6 +24,9 @@ public class ProxyNode {
*/ */
int num; int num;
String agentName;
String agentTopicName; String agentTopicName;
String location; String location;
@@ -41,6 +42,6 @@ public class ProxyNode {
@Override @Override
public String toString() { public String toString() {
return "ProxyNode: [ " + agentTopicName + " ]"; return "ProxyNode: [ " + agentName + " ]";
} }
} }

View File

@@ -14,6 +14,8 @@ public class ProxyNodeSet {
public static ProxyNode phoenix2; public static ProxyNode phoenix2;
public static ProxyNode london2; public static ProxyNode london2;
public static ProxyNode chengduAgent;
static { static {
@@ -24,7 +26,7 @@ public class ProxyNodeSet {
.publicIPv4("43.136.177.228") .publicIPv4("43.136.177.228")
.proxyNodeType(ProxyNodeType.INTERFACE) .proxyNodeType(ProxyNodeType.INTERFACE)
.name("cd-agent") .name("cd-agent")
.agentTopicName("Chengdu-amd-01") .agentName("Chengdu-amd-01")
.build(); .build();
@@ -35,7 +37,7 @@ public class ProxyNodeSet {
.publicIPv4("42.192.52.227") .publicIPv4("42.192.52.227")
.proxyNodeType(ProxyNodeType.RELAY) .proxyNodeType(ProxyNodeType.RELAY)
.name("tc-sh") .name("tc-sh")
.agentTopicName("Shanghai-amd64-01") .agentName("Shanghai-amd64-01")
.build(); .build();
hongkong = ProxyNode hongkong = ProxyNode
@@ -45,7 +47,7 @@ public class ProxyNodeSet {
.publicIPv4("43.154.83.213") .publicIPv4("43.154.83.213")
.proxyNodeType(ProxyNodeType.EXTERNAL) .proxyNodeType(ProxyNodeType.EXTERNAL)
.name("tc-hk") .name("tc-hk")
.agentTopicName("Hongkong-amd64-01") .agentName("Hongkong-amd64-01")
.build(); .build();
@@ -56,7 +58,7 @@ public class ProxyNodeSet {
.publicIPv4("140.238.30.110") .publicIPv4("140.238.30.110")
.proxyNodeType(ProxyNodeType.EXTERNAL) .proxyNodeType(ProxyNodeType.EXTERNAL)
.name("seoul-2") .name("seoul-2")
.agentTopicName("Seoul-amd64-02") .agentName("Seoul-amd64-02")
.build(); .build();
@@ -67,7 +69,7 @@ public class ProxyNodeSet {
.publicIPv4("140.238.52.228") .publicIPv4("140.238.52.228")
.proxyNodeType(ProxyNodeType.EXTERNAL) .proxyNodeType(ProxyNodeType.EXTERNAL)
.name("tokyo-2") .name("tokyo-2")
.agentTopicName("Tokyo-amd64-02") .agentName("Tokyo-amd64-02")
.build(); .build();
phoenix2 = ProxyNode phoenix2 = ProxyNode
@@ -77,7 +79,7 @@ public class ProxyNodeSet {
.publicIPv4("129.146.171.163") .publicIPv4("129.146.171.163")
.proxyNodeType(ProxyNodeType.EXTERNAL) .proxyNodeType(ProxyNodeType.EXTERNAL)
.name("phoenix-2") .name("phoenix-2")
.agentTopicName("Phoenix-amd64-02") .agentName("Phoenix-amd64-02")
.build(); .build();
london2 = ProxyNode london2 = ProxyNode
@@ -87,7 +89,18 @@ public class ProxyNodeSet {
.publicIPv4("141.147.106.62") .publicIPv4("141.147.106.62")
.proxyNodeType(ProxyNodeType.EXTERNAL) .proxyNodeType(ProxyNodeType.EXTERNAL)
.name("london-2") .name("london-2")
.agentTopicName("London-amd64-02") .agentName("London-amd64-02")
.build();
chengduAgent = ProxyNode
.builder()
.location("Chengdu")
.num(99)
.publicIPv4("183.220.112.113")
.proxyNodeType(ProxyNodeType.EXTERNAL)
.name("chengdu-agent")
.agentName("Chengdu-amd64-77")
.agentTopicName("Chengdu-amd64-77-remote")
.build(); .build();
ProxyNodeMap.put(chengdu.getNum(), chengdu); ProxyNodeMap.put(chengdu.getNum(), chengdu);
@@ -97,7 +110,7 @@ public class ProxyNodeSet {
ProxyNodeMap.put(tokyo2.getNum(), tokyo2); ProxyNodeMap.put(tokyo2.getNum(), tokyo2);
ProxyNodeMap.put(phoenix2.getNum(), phoenix2); ProxyNodeMap.put(phoenix2.getNum(), phoenix2);
ProxyNodeMap.put(london2.getNum(), london2); ProxyNodeMap.put(london2.getNum(), london2);
ProxyNodeMap.put(chengduAgent.getNum(), chengduAgent);
} }

View File

@@ -1,5 +1,6 @@
package io.wdd.func.xray.service; package io.wdd.func.xray.service;
import io.wdd.common.utils.TimeUtils;
import io.wdd.func.oss.config.OctopusObjectSummary; import io.wdd.func.oss.config.OctopusObjectSummary;
import io.wdd.func.oss.config.OssConfig; import io.wdd.func.oss.config.OssConfig;
import io.wdd.func.oss.service.OSSCoreService; import io.wdd.func.oss.service.OSSCoreService;
@@ -41,10 +42,9 @@ public class XrayConfigDistribute {
static { static {
ArrayList<String> first = new ArrayList<>( ArrayList<String> first = new ArrayList<>(
List.of( List.of(
"cp", "mv",
"/usr/local/etc/xray/config.json", "/usr/local/etc/xray/config.json",
"/usr/local/etc/xray/config-bak-$(date +%Y-%m-%d-%H-%M-%S)" "/usr/local/etc/xray/config-bak-TIME"
) )
); );
@@ -126,6 +126,7 @@ public class XrayConfigDistribute {
public void buildXrayUpdateResult(ArrayList<ProxyNode> networkPathList) { public void buildXrayUpdateResult(ArrayList<ProxyNode> networkPathList) {
String formatTimeString = TimeUtils.currentFormatTimeString();
List<String> resultKeyList = networkPathList List<String> resultKeyList = networkPathList
.stream() .stream()
@@ -136,13 +137,25 @@ public class XrayConfigDistribute {
.getObjectSummary() .getObjectSummary()
.getAccess(); .getAccess();
String realUrl = access String realUrl = String.valueOf(access
.getOriginUrl() .getOriginUrl());
.getPath();
if (StringUtils.isNotEmpty(access.getCloudflareUrl())) { if (StringUtils.isNotEmpty(access.getCloudflareUrl())) {
// 优先使用 被cloudflare包装过的的下载网址 // 优先使用 被cloudflare包装过的的下载网址
realUrl = access.getCloudflareUrl(); realUrl = access.getCloudflareUrl();
} }
// 修改命令中的时间
String s = updateXrayCommandList
.get(0)
.get(2);
updateXrayCommandList
.get(0)
.set(
2,
s.replace("TIME",
formatTimeString)
);
// 修改命令中的下载url // 修改命令中的下载url
updateXrayCommandList updateXrayCommandList
.get(1) .get(1)
@@ -165,7 +178,10 @@ public class XrayConfigDistribute {
) )
.collect(Collectors.toList()); .collect(Collectors.toList());
log.debug("发送所有的配置到各个Agent成功 结果查看为 => {}", resultKeyList); log.info(
"发送所有的配置到各个Agent成功 结果查看为 => {}",
resultKeyList
);
} }

View File

@@ -23,7 +23,6 @@ import javax.annotation.Resource;
import java.io.File; import java.io.File;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.UUID; import java.util.UUID;
@@ -81,18 +80,10 @@ public class XrayCoreServiceImpl implements XrayCoreService {
private void generateXrayJsonSinglePath(ArrayList<ProxyNode> networkPathList) { private void generateXrayJsonSinglePath(ArrayList<ProxyNode> networkPathList) {
int pathLength = networkPathList.size(); int pathLength = networkPathList.size();
if (pathLength == 1) {
log.error("网络路径节点仅为一个");
return;
}
// 忽略掉 null的字段 // 忽略掉 null的字段
objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
ArrayList<XrayConfig> tmpXrayConfigList = new ArrayList<>();
// 采用 VMESS + websocket的形式形成 链状代理 // 采用 VMESS + websocket的形式形成 链状代理
// 由于 Vlss+XTLS的形式形成 链状结构 // 由于 Vlss+XTLS的形式形成 链状结构
String tag = generatePathTag(networkPathList); String tag = generatePathTag(networkPathList);
@@ -119,7 +110,10 @@ public class XrayCoreServiceImpl implements XrayCoreService {
ProxyNode proxyNode = networkPathList.get(pos); ProxyNode proxyNode = networkPathList.get(pos);
// 最后一个节点需要进行 FreeOutbound的构造
boolean isOutBoundFree = pos == pathLength - 1; boolean isOutBoundFree = pos == pathLength - 1;
// 构造 xrayConfig
XrayConfig xrayConfig = doBuildXrayConfig( XrayConfig xrayConfig = doBuildXrayConfig(
isOutBoundFree, isOutBoundFree,
tag, tag,
@@ -129,9 +123,6 @@ public class XrayCoreServiceImpl implements XrayCoreService {
pos pos
); );
// 添加到临时缓存中
tmpXrayConfigList.add(xrayConfig);
// 持久化 // 持久化
try { try {
String resultContent = objectMapper String resultContent = objectMapper
@@ -169,7 +160,7 @@ public class XrayCoreServiceImpl implements XrayCoreService {
private String buildXrayConfigFileName(ProxyNode proxyNode, String timeString) { private String buildXrayConfigFileName(ProxyNode proxyNode, String timeString) {
return proxyNode.getNum() + "-" + proxyNode.getAgentTopicName() + "-" + timeString + ".json"; return proxyNode.getNum() + "-" + proxyNode.getAgentName() + "-" + timeString + ".json";
} }

View File

@@ -84,8 +84,8 @@ public class CoreExecutionServiceImpl implements CoreExecutionService {
// 检查agentTopicName是否存在 // 检查agentTopicName是否存在
if (!ALL_AGENT_TOPIC_NAME_SET.contains(agentTopicName)) { if (!ALL_AGENT_TOPIC_NAME_SET.contains(agentTopicName)) {
log.error("agentTopicName异常!"); log.error("agentTopicName异常! 输入为 => {}", agentTopicName);
throw new MyRuntimeException("agentTopicName异常!"); throw new MyRuntimeException("agentTopicName异常!" + agentTopicName);
} }
// 归一化type类型 不行 // 归一化type类型 不行

View File

@@ -99,6 +99,12 @@ public class ServerBootUpEnvironment {
} }
/**
*
* 更新Agent状态信息的全局缓存
*
* 由定时任务或者初始化服务触发
*/
public void updateAgentStatusMapCache() { public void updateAgentStatusMapCache() {
if (CollectionUtils.isEmpty(ALL_AGENT_TOPIC_NAME_LIST)) { if (CollectionUtils.isEmpty(ALL_AGENT_TOPIC_NAME_LIST)) {
@@ -128,12 +134,13 @@ public class ServerBootUpEnvironment {
); );
} }
String currentTimeString = TimeUtils.currentTimeString(); // 由于太多了,所以关闭此处log输出
log.info( /* String currentTimeString = TimeUtils.currentTimeString();
log.debug(
"[ AGENT HEALTHY CHECK ] time is {} , result are => {}", "[ AGENT HEALTHY CHECK ] time is {} , result are => {}",
currentTimeString, currentTimeString,
agentStatusMap agentStatusMap
); );*/
// 2023-01-16 // 2023-01-16
ALL_AGENT_STATUS_MAP.clear(); ALL_AGENT_STATUS_MAP.clear();

View File

@@ -12,7 +12,7 @@ public class QuartzLogOperator {
public boolean save(){ public boolean save(){
log.info("QuartzLogOperator pretend to have saved the log !"); log.debug("QuartzLogOperator pretend to have saved the log !");
return true; return true;

View File

@@ -1,8 +1,10 @@
package io.wdd.rpc.scheduler.job; package io.wdd.rpc.scheduler.job;
import io.wdd.common.utils.TimeUtils;
import io.wdd.rpc.scheduler.beans.ScriptSchedulerDTO; import io.wdd.rpc.scheduler.beans.ScriptSchedulerDTO;
import io.wdd.rpc.scheduler.service.script.AgentApplyScheduledScript; import io.wdd.rpc.scheduler.service.script.AgentApplyScheduledScript;
import lombok.extern.slf4j.Slf4j;
import org.quartz.JobDataMap; import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext; import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException; import org.quartz.JobExecutionException;
@@ -12,6 +14,10 @@ import javax.annotation.Resource;
import static io.wdd.rpc.scheduler.service.QuartzSchedulerServiceImpl.SCRIPT_SCHEDULER_MISSION_KEY; import static io.wdd.rpc.scheduler.service.QuartzSchedulerServiceImpl.SCRIPT_SCHEDULER_MISSION_KEY;
/**
* 定时脚本任务核心类Quartz框架定时调用该类
*/
@Slf4j
public class AgentScriptSchedulerJob extends QuartzJobBean { public class AgentScriptSchedulerJob extends QuartzJobBean {
@Resource @Resource
@@ -20,6 +26,11 @@ public class AgentScriptSchedulerJob extends QuartzJobBean {
@Override @Override
protected void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException { protected void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException {
log.info("[ 定时脚本任务 ]被调用了,调用时间为: {}",
TimeUtils.currentTimeString()
);
// 解析 Scheduler 模块传递过来的参数 // 解析 Scheduler 模块传递过来的参数
JobDataMap jobDataMap = jobExecutionContext JobDataMap jobDataMap = jobExecutionContext
.getJobDetail() .getJobDetail()
@@ -28,7 +39,7 @@ public class AgentScriptSchedulerJob extends QuartzJobBean {
// ScriptScheduleDTO // ScriptScheduleDTO
ScriptSchedulerDTO scriptSchedulerDTO = (ScriptSchedulerDTO) jobDataMap.get(SCRIPT_SCHEDULER_MISSION_KEY); ScriptSchedulerDTO scriptSchedulerDTO = (ScriptSchedulerDTO) jobDataMap.get(SCRIPT_SCHEDULER_MISSION_KEY);
// 调用实际任务执行器 // 调用实际任务执行器, 实际执行定时任务的核心业务内容
agentApplyScheduledScript.apply(scriptSchedulerDTO); agentApplyScheduledScript.apply(scriptSchedulerDTO);
} }

View File

@@ -17,7 +17,7 @@ public interface QuartzSchedulerService {
boolean addMission(OctopusQuartzJob quartzJob); boolean addMission(OctopusQuartzJob quartzJob);
/** /**
* 增加一个任务job * 增加一个任务job, 重复特定的次数
* *
* @param jobClass 任务job实现类 * @param jobClass 任务job实现类
* @param jobName 任务job名称保证唯一性 * @param jobName 任务job名称保证唯一性
@@ -37,8 +37,9 @@ public interface QuartzSchedulerService {
* @param startTime * @param startTime
* @param cronJobExpression 任务时间表达式 * @param cronJobExpression 任务时间表达式
* @param jobData 任务参数 * @param jobData 任务参数
* @return
*/ */
void addMission(Class<? extends QuartzJobBean> jobClass, String jobName, String jobGroupName, int startTime, String cronJobExpression, Map jobData); JobDetail addMission(Class<? extends QuartzJobBean> jobClass, String jobName, String jobGroupName, int startTime, String cronJobExpression, Map jobData);
/** /**
* 增加一个任务job * 增加一个任务job
@@ -46,11 +47,12 @@ public interface QuartzSchedulerService {
* @param jobClass 任务job实现类 * @param jobClass 任务job实现类
* @param jobName 任务job名称保证唯一性 * @param jobName 任务job名称保证唯一性
* @param jobGroupName 任务job组名 * @param jobGroupName 任务job组名
* @param startTime * @param startTime 任务开始时间,单位间隔为秒
* @param cronJobExpression 任务时间表达式 * @param cronJobExpression 任务时间表达式
* @param jobData 任务参数 * @param jobData 任务参数
* @return
*/ */
void addMission( JobDetail addMission(
Class<? extends QuartzJobBean> jobClass, Class<? extends QuartzJobBean> jobClass,
String jobName, String jobName,
String jobGroupName, String jobGroupName,
@@ -125,7 +127,6 @@ public interface QuartzSchedulerService {
/** /**
* --------------------------------------------------------------
* 页面定时任务部分 * 页面定时任务部分
* 应该只有脚本功能才可以定时,目前一阶段的功能 * 应该只有脚本功能才可以定时,目前一阶段的功能
* *

View File

@@ -28,13 +28,8 @@ import java.util.*;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import static io.wdd.rpc.scheduler.beans.ScriptSchedulerVO.SCRIPT_SCHEDULE_MISSION_GROUP_NAME; import static io.wdd.rpc.scheduler.beans.ScriptSchedulerVO.SCRIPT_SCHEDULE_MISSION_GROUP_NAME;
import static io.wdd.rpc.scheduler.service.BuildStatusScheduleTask.JOB_GROUP_NAME;
import static org.quartz.TriggerBuilder.newTrigger; import static org.quartz.TriggerBuilder.newTrigger;
/**
* @author Andya
* @date 2021/4/01
*/
@Slf4j @Slf4j
@Service @Service
public class QuartzSchedulerServiceImpl implements QuartzSchedulerService { public class QuartzSchedulerServiceImpl implements QuartzSchedulerService {
@@ -52,15 +47,8 @@ public class QuartzSchedulerServiceImpl implements QuartzSchedulerService {
* 保存 定时脚本任务的中间DTO在Quartz框架中JobDataMap的Key名称 * 保存 定时脚本任务的中间DTO在Quartz框架中JobDataMap的Key名称
*/ */
public static final String SCRIPT_SCHEDULER_MISSION_KEY = "scriptSchedulerDTO"; public static final String SCRIPT_SCHEDULER_MISSION_KEY = "scriptSchedulerDTO";
/**
* --------------------------------------------------------------
* 页面定时任务部分
* 应该只有脚本功能才可以定时,目前一阶段的功能
* */
/**
* @return
*/
@Override @Override
public HashMap<String, String> createScriptScheduledMission(ScriptSchedulerVO scriptSchedulerVO) { public HashMap<String, String> createScriptScheduledMission(ScriptSchedulerVO scriptSchedulerVO) {
@@ -75,7 +63,9 @@ public class QuartzSchedulerServiceImpl implements QuartzSchedulerService {
SCRIPT_SCHEDULER_MISSION_KEY, SCRIPT_SCHEDULER_MISSION_KEY,
scriptSchedulerDTO scriptSchedulerDTO
); );
this.addMission(
// 核心部分使用Quartz框架新增定时任务
JobDetail jobDetail = this.addMission(
AgentScriptSchedulerJob.class, AgentScriptSchedulerJob.class,
// 方便删除任务,比较好定位 // 方便删除任务,比较好定位
scriptSchedulerDTO.getSchedulerUuid(), scriptSchedulerDTO.getSchedulerUuid(),
@@ -89,20 +79,23 @@ public class QuartzSchedulerServiceImpl implements QuartzSchedulerService {
HashMap<String, String> futureExecutionResultKey = quartzSchedulerUtils.generateAndSetFutureExecutionResultKey(scriptSchedulerDTO); HashMap<String, String> futureExecutionResultKey = quartzSchedulerUtils.generateAndSetFutureExecutionResultKey(scriptSchedulerDTO);
log.info( log.debug(
"futureExecutionResultKey is => {}", "futureExecutionResultKey is => {}",
futureExecutionResultKey futureExecutionResultKey
); );
log.info("[ 定时脚本任务 ] - 新建任务成功, 船舰任务DTO为 => {}", scriptSchedulerDTO);
log.info("[ 定时脚本任务 ] - 新建任务成功, 任务内容为 => {}", jobDetail);
// persistent the script scheduled mission // persistent the script scheduled mission
// dto should store more info // dto should store more info
ScriptSchedulerPO scriptSchedulerPO = convertToScriptSchedulerPO(scriptSchedulerDTO); ScriptSchedulerPO scriptSchedulerPO = convertToScriptSchedulerPO(scriptSchedulerDTO);
log.info( log.debug(
"scriptSchedulerPO is => {}", "scriptSchedulerPO is => {}",
scriptSchedulerPO scriptSchedulerPO
); );
// 持久化保存定时脚本任务至数据库中
// scriptSchedulerService.save(scriptSchedulerPO); scriptSchedulerService.save(scriptSchedulerPO);
return futureExecutionResultKey; return futureExecutionResultKey;
} }
@@ -205,16 +198,7 @@ public class QuartzSchedulerServiceImpl implements QuartzSchedulerService {
return false; return false;
} }
/**
* 增加一个job
*
* @param jobClass 任务实现类
* @param jobName 任务名称
* @param jobGroupName 任务组名
* @param jobRunTimePinch 时间表达式 (这是每隔多少秒为一次任务)
* @param jobRunRepeatTimes 运行的次数 <0:表示不限次数)
* @param jobData 参数
*/
@Override @Override
public void addMission(Class<? extends QuartzJobBean> jobClass, String jobName, String jobGroupName, int jobRunTimePinch, int jobRunRepeatTimes, Map jobData) { public void addMission(Class<? extends QuartzJobBean> jobClass, String jobName, String jobGroupName, int jobRunTimePinch, int jobRunRepeatTimes, Map jobData) {
try { try {
@@ -278,10 +262,11 @@ public class QuartzSchedulerServiceImpl implements QuartzSchedulerService {
* 表示立即开始 * 表示立即开始
* @param cronJobExpression 时间表达式 0/5 * * * * ? * @param cronJobExpression 时间表达式 0/5 * * * * ?
* @param jobData 参数 * @param jobData 参数
* @return
*/ */
@Override @Override
public void addMission(Class<? extends QuartzJobBean> jobClass, String jobName, String jobGroupName, int startTime, String cronJobExpression, Map jobData) { public JobDetail addMission(Class<? extends QuartzJobBean> jobClass, String jobName, String jobGroupName, int startTime, String cronJobExpression, Map jobData) {
addMission( return addMission(
jobClass, jobClass,
jobName, jobName,
jobGroupName, jobGroupName,
@@ -293,7 +278,7 @@ public class QuartzSchedulerServiceImpl implements QuartzSchedulerService {
} }
@Override @Override
public void addMission(Class<? extends QuartzJobBean> jobClass, String jobName, String jobGroupName, String jobDescription, int startTime, String cronJobExpression, Map jobData) { public JobDetail addMission(Class<? extends QuartzJobBean> jobClass, String jobName, String jobGroupName, String jobDescription, int startTime, String cronJobExpression, Map jobData) {
try { try {
// 创建jobDetail实例绑定Job实现类 // 创建jobDetail实例绑定Job实现类
// 指明job的名称所在组的名称以及绑定job类 // 指明job的名称所在组的名称以及绑定job类
@@ -306,6 +291,7 @@ public class QuartzSchedulerServiceImpl implements QuartzSchedulerService {
) )
.withDescription(jobDescription) .withDescription(jobDescription)
.build(); .build();
// 设置job参数 // 设置job参数
if (jobData != null && jobData.size() > 0) { if (jobData != null && jobData.size() > 0) {
jobDetail jobDetail
@@ -316,7 +302,6 @@ public class QuartzSchedulerServiceImpl implements QuartzSchedulerService {
// 定义调度触发规则 // 定义调度触发规则
// 使用cornTrigger规则 // 使用cornTrigger规则
// 触发器key // 触发器key
// uniform the start time // uniform the start time
if (ObjectUtils.isEmpty(startTime)) { if (ObjectUtils.isEmpty(startTime)) {
startTime = 1; startTime = 1;
@@ -330,13 +315,12 @@ public class QuartzSchedulerServiceImpl implements QuartzSchedulerService {
.startAt( .startAt(
DateBuilder.futureDate( DateBuilder.futureDate(
startTime, startTime,
IntervalUnit.MILLISECOND IntervalUnit.SECOND
) )
) )
.withSchedule( .withSchedule(
CronScheduleBuilder.cronSchedule(cronJobExpression) CronScheduleBuilder.cronSchedule(cronJobExpression)
) )
.startNow()
.build(); .build();
// 把作业和触发器注册到任务调度中 // 把作业和触发器注册到任务调度中
@@ -345,10 +329,7 @@ public class QuartzSchedulerServiceImpl implements QuartzSchedulerService {
trigger trigger
); );
log.debug( return jobDetail;
"jobDataMap: {}",
jobDetail.getJobDataMap()
);
} catch (Exception e) { } catch (Exception e) {

View File

@@ -25,11 +25,17 @@ public class AgentApplyScheduledScript {
@Resource @Resource
QuartzSchedulerUtils quartzSchedulerUtils; QuartzSchedulerUtils quartzSchedulerUtils;
/**
* 实际执行定时脚本任务的核心方法
*
* @param scriptSchedulerDTO
*/
public void apply(ScriptSchedulerDTO scriptSchedulerDTO) { public void apply(ScriptSchedulerDTO scriptSchedulerDTO) {
List<List<String>> completeCommandList = scriptSchedulerDTO.getCompleteCommandList(); List<List<String>> completeCommandList = scriptSchedulerDTO.getCompleteCommandList();
List<String> targetMachineList = scriptSchedulerDTO.getTargetMachineList(); List<String> targetMachineList = scriptSchedulerDTO.getTargetMachineList();
// 拿到执行任务的 resultKey
HashMap<String, String> futureResultKeyMap = scriptSchedulerDTO.getAgentTopicNameToFutureResultKeyMap(); HashMap<String, String> futureResultKeyMap = scriptSchedulerDTO.getAgentTopicNameToFutureResultKeyMap();
// 新增-定时调用Nacos中预编辑脚本的内容 // 新增-定时调用Nacos中预编辑脚本的内容
@@ -38,7 +44,9 @@ public class AgentApplyScheduledScript {
scriptType = "Scheduled Script"; scriptType = "Scheduled Script";
} }
List<String> resultKeyList = coreExecutionService.SendCommandToAgentComplete( // 发送命令到Agent中
List<String> resultKeyList = coreExecutionService
.SendCommandToAgentComplete(
targetMachineList, targetMachineList,
scriptType, scriptType,
completeCommandList, completeCommandList,
@@ -51,7 +59,7 @@ public class AgentApplyScheduledScript {
// 更新DTO中的下次执行时间 否则会出错 // 更新DTO中的下次执行时间 否则会出错
HashMap<String, String> futureExecutionResultKey = quartzSchedulerUtils.generateAndSetFutureExecutionResultKey(scriptSchedulerDTO); HashMap<String, String> futureExecutionResultKey = quartzSchedulerUtils.generateAndSetFutureExecutionResultKey(scriptSchedulerDTO);
log.info( log.debug(
"futureExecutionResultKey in scheduled work is => {}", "futureExecutionResultKey in scheduled work is => {}",
futureExecutionResultKey futureExecutionResultKey
); );
@@ -59,7 +67,7 @@ public class AgentApplyScheduledScript {
// 需要更新数据库 // 需要更新数据库
// 关联性数据库 // 关联性数据库
log.info("[定时脚本任务]-[业务类]-实际执行完成!");
} }