[ server ] [ scheduler ]- script scheduler accomplish - 1

This commit is contained in:
zeaslity
2023-01-18 17:57:09 +08:00
parent ac27f1c702
commit 07d7810087
7 changed files with 129 additions and 95 deletions

View File

@@ -11,6 +11,7 @@ import org.quartz.Trigger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@@ -33,15 +34,15 @@ public class SchedulerController {
@ApiOperation(value = "新增一个定时脚本任务")
@PostMapping(value = "/script/create")
public R<List<String>> createScriptScheduler(
public R<HashMap<String, String>> createScriptScheduler(
@ApiParam(name = "scheduleScript") @RequestBody() ScriptSchedulerVO scriptSchedulerVO
) {
ALL_AGENT_TOPIC_NAME_SET.add("Chengdu-amd64-98-98066f");
List<String> resultList = octopusQuartzService.createScriptScheduledMission(scriptSchedulerVO);
HashMap<String, String> map = octopusQuartzService.createScriptScheduledMission(scriptSchedulerVO);
return R.ok(resultList);
return R.ok(map);
}

View File

@@ -0,0 +1,92 @@
package io.wdd.rpc.scheduler.config;
import io.wdd.common.beans.executor.ExecutionMessage;
import io.wdd.common.utils.TimeUtils;
import io.wdd.rpc.scheduler.beans.ScriptSchedulerDTO;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.Trigger;
import org.quartz.TriggerKey;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import static io.wdd.rpc.scheduler.beans.ScriptSchedulerVO.SCHEDULE_MISSION_GROUP_NAME;
@Component
public class QuartzSchedulerUtils {
@Resource
private Scheduler scheduler;
/**
* @param scriptSchedulerDTO dto
* @return dto中存储的 未来ExecutionKey对象
*/
public HashMap<String, String> getFutureExecutionResultKey(ScriptSchedulerDTO scriptSchedulerDTO) {
ArrayList<LocalDateTime> time = getLastNextExecutionTime(
scriptSchedulerDTO.getSchedulerUuid(),
SCHEDULE_MISSION_GROUP_NAME
);
LocalDateTime nextExecutionTime = time.get(1);
HashMap<String, String> keyMap = scriptSchedulerDTO.getAgentTopicNameToFutureResultKeyMap();
// 为每一个目标Agent都要设置相应的 FutureKey
scriptSchedulerDTO
.getTargetMachineList()
.stream()
.forEach(
targetMachine -> {
keyMap.put(
targetMachine,
ExecutionMessage.GetFutureResultKey(
targetMachine,
nextExecutionTime
)
);
}
);
return keyMap;
}
/**
* 定时任务通常在将来执行,所以需要手动获取到下次的执行时间
*
* @param missionName 任务名称
* @param missionGroupName 任务Group组名
* @return 上次调度时间 下次执行时间
*/
private ArrayList<LocalDateTime> getLastNextExecutionTime(String missionName, String missionGroupName) {
// 获取JobDetail存储上次调度时间和下次执行时间
try {
Trigger schedulerTrigger = scheduler.getTrigger(
new TriggerKey(
missionName,
missionGroupName
)
);
ArrayList<LocalDateTime> result = new ArrayList<>();
LocalDateTime last_schedule_time = TimeUtils.cvFromDate(schedulerTrigger.getFinalFireTime());
LocalDateTime next_schedule_time = TimeUtils.cvFromDate(schedulerTrigger.getNextFireTime());
result.add(last_schedule_time);
result.add(next_schedule_time);
return result;
} catch (SchedulerException e) {
throw new RuntimeException(e);
}
}
}

View File

@@ -1,16 +0,0 @@
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();
}
}

View File

@@ -4,6 +4,8 @@ import io.wdd.rpc.scheduler.beans.OctopusQuartzJob;
import io.wdd.rpc.scheduler.beans.ScriptSchedulerVO;
import org.quartz.Trigger;
import org.springframework.scheduling.quartz.QuartzJobBean;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@@ -102,7 +104,7 @@ public interface QuartzSchedulerService {
*
* @return
*/
List<String> createScriptScheduledMission(ScriptSchedulerVO scriptSchedulerVO);
HashMap<String, String> createScriptScheduledMission(ScriptSchedulerVO scriptSchedulerVO);
}

View File

@@ -1,12 +1,11 @@
package io.wdd.rpc.scheduler.service;
import io.wdd.common.beans.executor.ExecutionMessage;
import io.wdd.common.handler.MyRuntimeException;
import io.wdd.common.utils.FunctionReader;
import io.wdd.common.utils.TimeUtils;
import io.wdd.rpc.scheduler.beans.OctopusQuartzJob;
import io.wdd.rpc.scheduler.beans.ScriptSchedulerDTO;
import io.wdd.rpc.scheduler.beans.ScriptSchedulerVO;
import io.wdd.rpc.scheduler.config.QuartzSchedulerUtils;
import io.wdd.rpc.scheduler.job.AgentScriptSchedulerJob;
import io.wdd.server.beans.po.ScriptSchedulerPO;
import io.wdd.server.service.ScriptSchedulerService;
@@ -24,7 +23,6 @@ import org.springframework.stereotype.Service;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;
@@ -42,10 +40,13 @@ public class QuartzSchedulerServiceImpl implements QuartzSchedulerService {
@Resource
ScriptSchedulerService scriptSchedulerService;
@Resource
QuartzSchedulerUtils quartzSchedulerUtils;
@Autowired
private Scheduler scheduler;
/**
* --------------------------------------------------------------
* 页面定时任务部分
@@ -56,7 +57,7 @@ public class QuartzSchedulerServiceImpl implements QuartzSchedulerService {
* @return
*/
@Override
public List<String> createScriptScheduledMission(ScriptSchedulerVO scriptSchedulerVO) {
public HashMap<String, String> createScriptScheduledMission(ScriptSchedulerVO scriptSchedulerVO) {
// validate important value
ScriptSchedulerDTO scriptSchedulerDTO = validateAndConvertToScriptSchedulerDTO(scriptSchedulerVO);
@@ -79,7 +80,7 @@ public class QuartzSchedulerServiceImpl implements QuartzSchedulerService {
dataMap
);
HashMap<String, String> futureExecutionResultKey = getFutureExecutionResultKey(scriptSchedulerDTO);
HashMap<String, String> futureExecutionResultKey = quartzSchedulerUtils.getFutureExecutionResultKey(scriptSchedulerDTO);
log.info("futureExecutionResultKey is => {}", futureExecutionResultKey);
@@ -90,74 +91,7 @@ public class QuartzSchedulerServiceImpl implements QuartzSchedulerService {
// scriptSchedulerService.save(scriptSchedulerPO);
return scriptSchedulerDTO.getResultKeyList();
}
/**
* @param scriptSchedulerDTO dto
* @return dto中存储的 未来ExecutionKey对象
*/
private HashMap<String, String> getFutureExecutionResultKey(ScriptSchedulerDTO scriptSchedulerDTO) {
ArrayList<LocalDateTime> time = getLastNextExecutionTime(
scriptSchedulerDTO.getSchedulerUuid(),
SCHEDULE_MISSION_GROUP_NAME
);
LocalDateTime nextExecutionTime = time.get(1);
HashMap<String, String> keyMap = scriptSchedulerDTO.getAgentTopicNameToFutureResultKeyMap();
// 为每一个目标Agent都要设置相应的 FutureKey
scriptSchedulerDTO
.getTargetMachineList()
.stream()
.forEach(
targetMachine -> {
keyMap.put(
targetMachine,
ExecutionMessage.GetFutureResultKey(
targetMachine,
nextExecutionTime
)
);
}
);
return keyMap;
}
/**
* 定时任务通常在将来执行,所以需要手动获取到下次的执行时间
*
* @param missionName 任务名称
* @param missionGroupName 任务Group组名
* @return 上次调度时间 下次执行时间
*/
public ArrayList<LocalDateTime> getLastNextExecutionTime(String missionName, String missionGroupName) {
// 获取JobDetail存储上次调度时间和下次执行时间
try {
Trigger schedulerTrigger = scheduler.getTrigger(
new TriggerKey(
missionName,
missionGroupName
)
);
ArrayList<LocalDateTime> result = new ArrayList<>();
LocalDateTime last_schedule_time = TimeUtils.cvFromDate(schedulerTrigger.getFinalFireTime());
LocalDateTime next_schedule_time = TimeUtils.cvFromDate(schedulerTrigger.getNextFireTime());
result.add(last_schedule_time);
result.add(next_schedule_time);
return result;
} catch (SchedulerException e) {
throw new RuntimeException(e);
}
return futureExecutionResultKey;
}
/**

View File

@@ -3,10 +3,12 @@ package io.wdd.rpc.scheduler.service.script;
import io.wdd.rpc.execute.service.CoreExecutionService;
import io.wdd.rpc.scheduler.beans.ScriptSchedulerDTO;
import io.wdd.rpc.scheduler.config.QuartzSchedulerUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
/**
@@ -19,20 +21,34 @@ public class AgentApplyScheduledScript {
@Resource
CoreExecutionService coreExecutionService;
@Resource
QuartzSchedulerUtils quartzSchedulerUtils;
public void apply(ScriptSchedulerDTO scriptSchedulerDTO) {
List<List<String>> completeCommandList = scriptSchedulerDTO.getCompleteCommandList();
List<String> targetMachineList = scriptSchedulerDTO.getTargetMachineList();
HashMap<String, String> futureResultKeyMap = scriptSchedulerDTO.getAgentTopicNameToFutureResultKeyMap();
List<String> resultKeyList = coreExecutionService.SendCommandToAgentComplete(
targetMachineList,
"Scheduled Script",
completeCommandList
completeCommandList,
futureResultKeyMap
);
// 将 resultKeyList 放入这个DTO中
scriptSchedulerDTO.setResultKeyList(resultKeyList);
// 更新DTO中的下次执行时间 否则会出错
HashMap<String, String> futureExecutionResultKey = quartzSchedulerUtils.getFutureExecutionResultKey(scriptSchedulerDTO);
log.info(
"futureExecutionResultKey in scheduled work is => {}",
futureExecutionResultKey
);
// 需要更新数据库
// 关联性数据库