[server] [ scheduler.script ] - 优化定时脚本任务部分的代码
This commit is contained in:
@@ -12,7 +12,7 @@ public class QuartzLogOperator {
|
||||
|
||||
public boolean save(){
|
||||
|
||||
log.info("QuartzLogOperator pretend to have saved the log !");
|
||||
log.debug("QuartzLogOperator pretend to have saved the log !");
|
||||
|
||||
return true;
|
||||
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
package io.wdd.rpc.scheduler.job;
|
||||
|
||||
|
||||
import io.wdd.common.utils.TimeUtils;
|
||||
import io.wdd.rpc.scheduler.beans.ScriptSchedulerDTO;
|
||||
import io.wdd.rpc.scheduler.service.script.AgentApplyScheduledScript;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.quartz.JobDataMap;
|
||||
import org.quartz.JobExecutionContext;
|
||||
import org.quartz.JobExecutionException;
|
||||
@@ -12,6 +14,10 @@ import javax.annotation.Resource;
|
||||
|
||||
import static io.wdd.rpc.scheduler.service.QuartzSchedulerServiceImpl.SCRIPT_SCHEDULER_MISSION_KEY;
|
||||
|
||||
/**
|
||||
* 定时脚本任务核心类,Quartz框架定时调用该类
|
||||
*/
|
||||
@Slf4j
|
||||
public class AgentScriptSchedulerJob extends QuartzJobBean {
|
||||
|
||||
@Resource
|
||||
@@ -20,6 +26,11 @@ public class AgentScriptSchedulerJob extends QuartzJobBean {
|
||||
@Override
|
||||
protected void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException {
|
||||
|
||||
|
||||
log.info("[ 定时脚本任务 ]被调用了,调用时间为: {}",
|
||||
TimeUtils.currentTimeString()
|
||||
);
|
||||
|
||||
// 解析 Scheduler 模块传递过来的参数
|
||||
JobDataMap jobDataMap = jobExecutionContext
|
||||
.getJobDetail()
|
||||
@@ -28,7 +39,7 @@ public class AgentScriptSchedulerJob extends QuartzJobBean {
|
||||
// ScriptScheduleDTO
|
||||
ScriptSchedulerDTO scriptSchedulerDTO = (ScriptSchedulerDTO) jobDataMap.get(SCRIPT_SCHEDULER_MISSION_KEY);
|
||||
|
||||
// 调用实际任务执行器
|
||||
// 调用实际任务执行器, 实际执行定时任务的核心业务内容
|
||||
agentApplyScheduledScript.apply(scriptSchedulerDTO);
|
||||
|
||||
}
|
||||
|
||||
@@ -17,7 +17,7 @@ public interface QuartzSchedulerService {
|
||||
boolean addMission(OctopusQuartzJob quartzJob);
|
||||
|
||||
/**
|
||||
* 增加一个任务job
|
||||
* 增加一个任务job, 重复特定的次数
|
||||
*
|
||||
* @param jobClass 任务job实现类
|
||||
* @param jobName 任务job名称(保证唯一性)
|
||||
@@ -37,8 +37,9 @@ public interface QuartzSchedulerService {
|
||||
* @param startTime
|
||||
* @param cronJobExpression 任务时间表达式
|
||||
* @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
|
||||
@@ -46,11 +47,12 @@ public interface QuartzSchedulerService {
|
||||
* @param jobClass 任务job实现类
|
||||
* @param jobName 任务job名称(保证唯一性)
|
||||
* @param jobGroupName 任务job组名
|
||||
* @param startTime
|
||||
* @param startTime 任务开始时间,单位间隔为秒
|
||||
* @param cronJobExpression 任务时间表达式
|
||||
* @param jobData 任务参数
|
||||
* @return
|
||||
*/
|
||||
void addMission(
|
||||
JobDetail addMission(
|
||||
Class<? extends QuartzJobBean> jobClass,
|
||||
String jobName,
|
||||
String jobGroupName,
|
||||
@@ -125,7 +127,6 @@ public interface QuartzSchedulerService {
|
||||
|
||||
|
||||
/**
|
||||
* --------------------------------------------------------------
|
||||
* 页面定时任务部分
|
||||
* 应该只有脚本功能才可以定时,目前一阶段的功能
|
||||
*
|
||||
|
||||
@@ -28,13 +28,8 @@ import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
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;
|
||||
|
||||
/**
|
||||
* @author Andya
|
||||
* @date 2021/4/01
|
||||
*/
|
||||
@Slf4j
|
||||
@Service
|
||||
public class QuartzSchedulerServiceImpl implements QuartzSchedulerService {
|
||||
@@ -52,15 +47,8 @@ public class QuartzSchedulerServiceImpl implements QuartzSchedulerService {
|
||||
* 保存 定时脚本任务的中间DTO,在Quartz框架中JobDataMap的Key名称
|
||||
*/
|
||||
public static final String SCRIPT_SCHEDULER_MISSION_KEY = "scriptSchedulerDTO";
|
||||
/**
|
||||
* --------------------------------------------------------------
|
||||
* 页面定时任务部分
|
||||
* 应该只有脚本功能才可以定时,目前一阶段的功能
|
||||
* */
|
||||
|
||||
/**
|
||||
* @return
|
||||
*/
|
||||
|
||||
@Override
|
||||
public HashMap<String, String> createScriptScheduledMission(ScriptSchedulerVO scriptSchedulerVO) {
|
||||
|
||||
@@ -75,7 +63,9 @@ public class QuartzSchedulerServiceImpl implements QuartzSchedulerService {
|
||||
SCRIPT_SCHEDULER_MISSION_KEY,
|
||||
scriptSchedulerDTO
|
||||
);
|
||||
this.addMission(
|
||||
|
||||
// 核心部分,使用Quartz框架,新增定时任务
|
||||
JobDetail jobDetail = this.addMission(
|
||||
AgentScriptSchedulerJob.class,
|
||||
// 方便删除任务,比较好定位
|
||||
scriptSchedulerDTO.getSchedulerUuid(),
|
||||
@@ -89,20 +79,23 @@ public class QuartzSchedulerServiceImpl implements QuartzSchedulerService {
|
||||
|
||||
HashMap<String, String> futureExecutionResultKey = quartzSchedulerUtils.generateAndSetFutureExecutionResultKey(scriptSchedulerDTO);
|
||||
|
||||
log.info(
|
||||
log.debug(
|
||||
"futureExecutionResultKey is => {}",
|
||||
futureExecutionResultKey
|
||||
);
|
||||
|
||||
log.info("[ 定时脚本任务 ] - 新建任务成功, 船舰任务DTO为 => {}", scriptSchedulerDTO);
|
||||
log.info("[ 定时脚本任务 ] - 新建任务成功, 任务内容为 => {}", jobDetail);
|
||||
|
||||
// persistent the script scheduled mission
|
||||
// dto should store more info
|
||||
ScriptSchedulerPO scriptSchedulerPO = convertToScriptSchedulerPO(scriptSchedulerDTO);
|
||||
log.info(
|
||||
log.debug(
|
||||
"scriptSchedulerPO is => {}",
|
||||
scriptSchedulerPO
|
||||
);
|
||||
|
||||
// scriptSchedulerService.save(scriptSchedulerPO);
|
||||
// 持久化保存定时脚本任务至数据库中
|
||||
scriptSchedulerService.save(scriptSchedulerPO);
|
||||
|
||||
return futureExecutionResultKey;
|
||||
}
|
||||
@@ -205,16 +198,7 @@ public class QuartzSchedulerServiceImpl implements QuartzSchedulerService {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 增加一个job
|
||||
*
|
||||
* @param jobClass 任务实现类
|
||||
* @param jobName 任务名称
|
||||
* @param jobGroupName 任务组名
|
||||
* @param jobRunTimePinch 时间表达式 (这是每隔多少秒为一次任务)
|
||||
* @param jobRunRepeatTimes 运行的次数 (<0:表示不限次数)
|
||||
* @param jobData 参数
|
||||
*/
|
||||
|
||||
@Override
|
||||
public void addMission(Class<? extends QuartzJobBean> jobClass, String jobName, String jobGroupName, int jobRunTimePinch, int jobRunRepeatTimes, Map jobData) {
|
||||
try {
|
||||
@@ -278,10 +262,11 @@ public class QuartzSchedulerServiceImpl implements QuartzSchedulerService {
|
||||
* 表示立即开始
|
||||
* @param cronJobExpression 时间表达式 (如:0/5 * * * * ? )
|
||||
* @param jobData 参数
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public void addMission(Class<? extends QuartzJobBean> jobClass, String jobName, String jobGroupName, int startTime, String cronJobExpression, Map jobData) {
|
||||
addMission(
|
||||
public JobDetail addMission(Class<? extends QuartzJobBean> jobClass, String jobName, String jobGroupName, int startTime, String cronJobExpression, Map jobData) {
|
||||
return addMission(
|
||||
jobClass,
|
||||
jobName,
|
||||
jobGroupName,
|
||||
@@ -293,7 +278,7 @@ public class QuartzSchedulerServiceImpl implements QuartzSchedulerService {
|
||||
}
|
||||
|
||||
@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 {
|
||||
// 创建jobDetail实例,绑定Job实现类
|
||||
// 指明job的名称,所在组的名称,以及绑定job类
|
||||
@@ -306,6 +291,7 @@ public class QuartzSchedulerServiceImpl implements QuartzSchedulerService {
|
||||
)
|
||||
.withDescription(jobDescription)
|
||||
.build();
|
||||
|
||||
// 设置job参数
|
||||
if (jobData != null && jobData.size() > 0) {
|
||||
jobDetail
|
||||
@@ -316,7 +302,6 @@ public class QuartzSchedulerServiceImpl implements QuartzSchedulerService {
|
||||
// 定义调度触发规则
|
||||
// 使用cornTrigger规则
|
||||
// 触发器key
|
||||
|
||||
// uniform the start time
|
||||
if (ObjectUtils.isEmpty(startTime)) {
|
||||
startTime = 1;
|
||||
@@ -330,13 +315,12 @@ public class QuartzSchedulerServiceImpl implements QuartzSchedulerService {
|
||||
.startAt(
|
||||
DateBuilder.futureDate(
|
||||
startTime,
|
||||
IntervalUnit.MILLISECOND
|
||||
IntervalUnit.SECOND
|
||||
)
|
||||
)
|
||||
.withSchedule(
|
||||
CronScheduleBuilder.cronSchedule(cronJobExpression)
|
||||
)
|
||||
.startNow()
|
||||
.build();
|
||||
|
||||
// 把作业和触发器注册到任务调度中
|
||||
@@ -345,10 +329,7 @@ public class QuartzSchedulerServiceImpl implements QuartzSchedulerService {
|
||||
trigger
|
||||
);
|
||||
|
||||
log.debug(
|
||||
"jobDataMap: {}",
|
||||
jobDetail.getJobDataMap()
|
||||
);
|
||||
return jobDetail;
|
||||
|
||||
|
||||
} catch (Exception e) {
|
||||
|
||||
@@ -25,11 +25,17 @@ public class AgentApplyScheduledScript {
|
||||
@Resource
|
||||
QuartzSchedulerUtils quartzSchedulerUtils;
|
||||
|
||||
/**
|
||||
* 实际执行定时脚本任务的核心方法
|
||||
*
|
||||
* @param scriptSchedulerDTO
|
||||
*/
|
||||
public void apply(ScriptSchedulerDTO scriptSchedulerDTO) {
|
||||
|
||||
List<List<String>> completeCommandList = scriptSchedulerDTO.getCompleteCommandList();
|
||||
List<String> targetMachineList = scriptSchedulerDTO.getTargetMachineList();
|
||||
|
||||
// 拿到执行任务的 resultKey
|
||||
HashMap<String, String> futureResultKeyMap = scriptSchedulerDTO.getAgentTopicNameToFutureResultKeyMap();
|
||||
|
||||
// 新增-定时调用Nacos中预编辑脚本的内容
|
||||
@@ -38,12 +44,14 @@ public class AgentApplyScheduledScript {
|
||||
scriptType = "Scheduled Script";
|
||||
}
|
||||
|
||||
List<String> resultKeyList = coreExecutionService.SendCommandToAgentComplete(
|
||||
targetMachineList,
|
||||
scriptType,
|
||||
completeCommandList,
|
||||
futureResultKeyMap
|
||||
);
|
||||
// 发送命令到Agent中
|
||||
List<String> resultKeyList = coreExecutionService
|
||||
.SendCommandToAgentComplete(
|
||||
targetMachineList,
|
||||
scriptType,
|
||||
completeCommandList,
|
||||
futureResultKeyMap
|
||||
);
|
||||
|
||||
// 将 resultKeyList 放入这个DTO中
|
||||
scriptSchedulerDTO.setResultKeyList(resultKeyList);
|
||||
@@ -51,7 +59,7 @@ public class AgentApplyScheduledScript {
|
||||
// 更新DTO中的下次执行时间 否则会出错
|
||||
HashMap<String, String> futureExecutionResultKey = quartzSchedulerUtils.generateAndSetFutureExecutionResultKey(scriptSchedulerDTO);
|
||||
|
||||
log.info(
|
||||
log.debug(
|
||||
"futureExecutionResultKey in scheduled work is => {}",
|
||||
futureExecutionResultKey
|
||||
);
|
||||
@@ -59,7 +67,7 @@ public class AgentApplyScheduledScript {
|
||||
// 需要更新数据库
|
||||
// 关联性数据库
|
||||
|
||||
|
||||
log.info("[定时脚本任务]-[业务类]-实际执行完成!");
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user