[ server ] [ scheduler ]- script scheduler - 1
This commit is contained in:
@@ -5,6 +5,7 @@ import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import io.swagger.annotations.ApiParam;
|
||||
import io.wdd.common.beans.response.R;
|
||||
import io.wdd.rpc.scheduler.beans.ScriptSchedulerVO;
|
||||
import io.wdd.rpc.scheduler.service.QuartzSchedulerService;
|
||||
import org.quartz.Trigger;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@@ -22,38 +23,63 @@ public class SchedulerController {
|
||||
QuartzSchedulerService octopusQuartzService;
|
||||
|
||||
|
||||
@ApiOperation(value = "查询所有job")
|
||||
@GetMapping(value = "/queryAllJob")
|
||||
public R<List<Map<String, Object>>> queryAllQuartzJob() {
|
||||
/**
|
||||
* --------------------------------------------------------------
|
||||
* 页面定时任务部分
|
||||
* 应该只有脚本功能才可以定时,目前一阶段的功能
|
||||
* */
|
||||
|
||||
return R.ok(octopusQuartzService.queryAllJob());
|
||||
}
|
||||
|
||||
|
||||
@ApiOperation(value = "查询所有运行job")
|
||||
@PostMapping(value = "/queryRunJob")
|
||||
public R<List<Map<String, Object>>> queryRunQuartzJob() {
|
||||
|
||||
return R.ok(octopusQuartzService.queryRunJob());
|
||||
}
|
||||
|
||||
@ApiOperation(value = "删除一个job")
|
||||
@PostMapping(value = "/deleteJob/")
|
||||
public R<String> deleteJob(
|
||||
@ApiParam(name = "jobName") @RequestParam("jobName") String jobName
|
||||
@ApiOperation(value = "新增一个定时脚本任务")
|
||||
@PostMapping(value = "/script/create")
|
||||
public R<String> createScriptScheduler(
|
||||
@ApiParam(name = "scheduleScript") @RequestBody() ScriptSchedulerVO scriptSchedulerVO
|
||||
) {
|
||||
|
||||
boolean deleteJob = octopusQuartzService.deleteJob(
|
||||
jobName,
|
||||
jobName
|
||||
octopusQuartzService.createScriptScheduledMission(scriptSchedulerVO);
|
||||
|
||||
return R.ok("ok");
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* --------------------------------------------------------------
|
||||
* 普通的定时任务查询功能
|
||||
* */
|
||||
|
||||
@ApiOperation(value = "查询所有mission")
|
||||
@GetMapping(value = "/queryAllMission")
|
||||
public R<List<Map<String, Object>>> queryAllQuartzMission() {
|
||||
|
||||
return R.ok(octopusQuartzService.queryAllMission());
|
||||
}
|
||||
|
||||
|
||||
@ApiOperation(value = "查询所有运行mission")
|
||||
@PostMapping(value = "/queryRunMission")
|
||||
public R<List<Map<String, Object>>> queryRunQuartzMission() {
|
||||
|
||||
return R.ok(octopusQuartzService.queryRunMission());
|
||||
}
|
||||
|
||||
|
||||
|
||||
@ApiOperation(value = "删除一个mission")
|
||||
@PostMapping(value = "/deleteMission/")
|
||||
public R<String> deleteMission(
|
||||
@ApiParam(name = "missionName") @RequestParam("missionName") String missionName
|
||||
) {
|
||||
|
||||
boolean deleteMission = octopusQuartzService.deleteMission(
|
||||
missionName,
|
||||
missionName
|
||||
);
|
||||
String result = String.format(
|
||||
"删除任务[ %s ]结果为 [ %s ]",
|
||||
jobName,
|
||||
deleteJob
|
||||
missionName,
|
||||
deleteMission
|
||||
);
|
||||
|
||||
if (deleteJob) {
|
||||
if (deleteMission) {
|
||||
return R.ok(result);
|
||||
} else {
|
||||
return R.failed(result);
|
||||
@@ -61,17 +87,17 @@ public class SchedulerController {
|
||||
}
|
||||
|
||||
|
||||
@ApiOperation(value = "修改job的cron时间")
|
||||
@PostMapping(value = "/updateJob/{jobName}")
|
||||
public void deleteJob(
|
||||
@ApiParam(name = "jobName") @RequestParam("jobName") String jobName,
|
||||
@ApiParam(name = "jobCronTime") @RequestParam("jobCronTime") String jobCronTime
|
||||
@ApiOperation(value = "修改mission的cron时间")
|
||||
@PostMapping(value = "/updateMission/{missionName}")
|
||||
public void deleteMission(
|
||||
@ApiParam(name = "missionName") @RequestParam("missionName") String missionName,
|
||||
@ApiParam(name = "missionCronTime") @RequestParam("missionCronTime") String missionCronTime
|
||||
) {
|
||||
|
||||
octopusQuartzService.updateJob(
|
||||
jobName,
|
||||
jobName,
|
||||
jobCronTime
|
||||
octopusQuartzService.updateMission(
|
||||
missionName,
|
||||
missionName,
|
||||
missionCronTime
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
@@ -10,8 +10,8 @@ import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import static io.wdd.rpc.status.MonitorAllAgentStatus.ALL_AGENT_HEALTHY_STATUS_MAP;
|
||||
import static io.wdd.rpc.status.MonitorAllAgentStatus.HEALTHY_STATUS_AGENT_LIST_MAP;
|
||||
import static io.wdd.rpc.scheduler.service.status.MonitorAllAgentStatus.ALL_AGENT_HEALTHY_STATUS_MAP;
|
||||
import static io.wdd.rpc.scheduler.service.status.MonitorAllAgentStatus.HEALTHY_STATUS_AGENT_LIST_MAP;
|
||||
|
||||
@RestController
|
||||
@Api("Agent运行状态Controller")
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
package io.wdd.rpc.execute.result;
|
||||
|
||||
|
||||
import io.wdd.rpc.execute.config.CommandReaderConfig;
|
||||
import io.wdd.rpc.status.AgentStatusStreamReader;
|
||||
import io.wdd.rpc.scheduler.service.status.AgentStatusStreamReader;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
@@ -19,7 +19,7 @@ import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static io.wdd.rpc.execute.service.ExecutionResultDaemonHandler.WAIT_EXECUTION_RESULT_LIST;
|
||||
import static io.wdd.rpc.status.MonitorAllAgentStatus.ALL_AGENT_TOPIC_NAME_SET;
|
||||
import static io.wdd.rpc.scheduler.service.status.MonitorAllAgentStatus.ALL_AGENT_TOPIC_NAME_SET;
|
||||
|
||||
@Service
|
||||
@Slf4j
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
package io.wdd.rpc.scheduler.beans;
|
||||
|
||||
import io.swagger.annotations.ApiModel;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.experimental.SuperBuilder;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@ApiModel("定时脚本任务-中间转换状态-实体类")
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
@SuperBuilder(toBuilder = true)
|
||||
public class ScriptSchedulerDTO extends ScriptSchedulerVO{
|
||||
|
||||
List<List<String>> commandList;
|
||||
|
||||
List<String> targetMachineList;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,114 @@
|
||||
package io.wdd.rpc.scheduler.beans;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.TableField;
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import io.swagger.annotations.ApiModel;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.experimental.SuperBuilder;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
import javax.validation.constraints.NotNull;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
@SuperBuilder(toBuilder = true)
|
||||
@ApiModel("定时脚本任务的VO实体类")
|
||||
public class ScriptSchedulerVO {
|
||||
|
||||
public static final String SCHEDULE_MISSION_GROUP_NAME = "SCRIPT_SCHEDULER";
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
@TableId(value = "scheduler_uuid")
|
||||
@Nullable
|
||||
private String schedulerUuid;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
@TableField(value = "name")
|
||||
@NotNull
|
||||
private String name;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
@TableField(value = "cron_express")
|
||||
@NotNull
|
||||
private String cronExpress;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
@TableField(value = "description")
|
||||
@Nullable
|
||||
private String description;
|
||||
|
||||
/**
|
||||
* 脚本任务的内容
|
||||
*/
|
||||
@TableField(value = "script_content")
|
||||
@NotNull
|
||||
private String scriptContent;
|
||||
|
||||
/**
|
||||
* 执行目标机器agent_topic_name列表,使用, 分隔
|
||||
*/
|
||||
@TableField(value = "target_machine")
|
||||
@Nullable
|
||||
private String targetMachine;
|
||||
|
||||
/**
|
||||
* 与 execution_log表的主键对应,方便查询执行日志
|
||||
*/
|
||||
@TableField(value = "last_execution_id")
|
||||
@Nullable
|
||||
private Long lastExecutionId;
|
||||
|
||||
/**
|
||||
* 与 execution_log表的 result_key 对应,方便查询执行日志
|
||||
*/
|
||||
@TableField(value = "last_execution_result_key")
|
||||
@Nullable
|
||||
private String lastExecutionResultKey;
|
||||
|
||||
/**
|
||||
* 任务上次执行状态
|
||||
*/
|
||||
@TableField(value = "last_execution_status")
|
||||
@Nullable
|
||||
private Integer lastExecutionStatus;
|
||||
|
||||
/**
|
||||
* 定时脚本任务创建时间
|
||||
*/
|
||||
@TableField(value = "create_time")
|
||||
@NotNull
|
||||
private LocalDateTime createTime;
|
||||
|
||||
/**
|
||||
* 上次更新时间
|
||||
*/
|
||||
@TableField(value = "update_time")
|
||||
@Nullable
|
||||
private LocalDateTime updateTime;
|
||||
|
||||
/**
|
||||
* 任务下次计划执行时间
|
||||
*/
|
||||
@TableField(value = "next_schedule_time")
|
||||
@Nullable
|
||||
private LocalDateTime nextScheduleTime;
|
||||
|
||||
/**
|
||||
* 任务上次计划执行时间
|
||||
*/
|
||||
@TableField(value = "last_schedule_time")
|
||||
@Nullable
|
||||
private LocalDateTime lastScheduleTime;
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
package io.wdd.rpc.scheduler.job;
|
||||
|
||||
import io.wdd.rpc.status.AgentRuntimeMetricStatus;
|
||||
import io.wdd.rpc.scheduler.service.status.AgentRuntimeMetricStatus;
|
||||
import org.quartz.JobDataMap;
|
||||
import org.quartz.JobExecutionContext;
|
||||
import org.quartz.JobExecutionException;
|
||||
@@ -8,8 +8,8 @@ import org.springframework.scheduling.quartz.QuartzJobBean;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
import static io.wdd.rpc.status.AgentRuntimeMetricStatus.METRIC_REPORT_TIMES_COUNT;
|
||||
import static io.wdd.rpc.status.AgentRuntimeMetricStatus.METRIC_REPORT_TIME_PINCH;
|
||||
import static io.wdd.rpc.scheduler.service.status.AgentRuntimeMetricStatus.METRIC_REPORT_TIMES_COUNT;
|
||||
import static io.wdd.rpc.scheduler.service.status.AgentRuntimeMetricStatus.METRIC_REPORT_TIME_PINCH;
|
||||
|
||||
public class AgentRunMetricStatusJob extends QuartzJobBean {
|
||||
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
package io.wdd.rpc.scheduler.job;
|
||||
|
||||
|
||||
import io.wdd.rpc.scheduler.beans.ScriptSchedulerDTO;
|
||||
import io.wdd.rpc.scheduler.service.script.AgentApplyScheduledScript;
|
||||
import io.wdd.server.beans.po.ScriptSchedulerPO;
|
||||
import org.quartz.JobDataMap;
|
||||
import org.quartz.JobExecutionContext;
|
||||
import org.quartz.JobExecutionException;
|
||||
import org.springframework.scheduling.quartz.QuartzJobBean;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
public class AgentScriptSchedulerJob extends QuartzJobBean {
|
||||
|
||||
@Resource
|
||||
AgentApplyScheduledScript agentApplyScheduledScript;
|
||||
|
||||
@Override
|
||||
protected void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException {
|
||||
|
||||
// 解析 Scheduler 模块传递过来的参数
|
||||
JobDataMap jobDataMap = jobExecutionContext
|
||||
.getJobDetail()
|
||||
.getJobDataMap();
|
||||
|
||||
// ScriptScheduleDTO
|
||||
ScriptSchedulerDTO scriptSchedulerDTO = (ScriptSchedulerDTO) jobDataMap.get("scriptSchedulerPO");
|
||||
|
||||
// 调用实际任务执行器
|
||||
agentApplyScheduledScript.apply(scriptSchedulerDTO);
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
package io.wdd.rpc.scheduler.job;
|
||||
|
||||
import io.wdd.rpc.scheduler.config.QuartzLogOperator;
|
||||
import io.wdd.rpc.status.MonitorAllAgentStatus;
|
||||
import io.wdd.rpc.scheduler.service.status.MonitorAllAgentStatus;
|
||||
import org.quartz.JobExecutionContext;
|
||||
import org.quartz.JobExecutionException;
|
||||
import org.springframework.scheduling.quartz.QuartzJobBean;
|
||||
|
||||
@@ -14,8 +14,8 @@ import java.text.ParseException;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
|
||||
import static io.wdd.rpc.status.AgentRuntimeMetricStatus.METRIC_REPORT_TIMES_COUNT;
|
||||
import static io.wdd.rpc.status.AgentRuntimeMetricStatus.METRIC_REPORT_TIME_PINCH;
|
||||
import static io.wdd.rpc.scheduler.service.status.AgentRuntimeMetricStatus.METRIC_REPORT_TIMES_COUNT;
|
||||
import static io.wdd.rpc.scheduler.service.status.AgentRuntimeMetricStatus.METRIC_REPORT_TIME_PINCH;
|
||||
|
||||
@Component
|
||||
@Slf4j
|
||||
@@ -80,7 +80,7 @@ public class BuildStatusScheduleTask {
|
||||
|
||||
// build the Job 只发送一次消息,然后让Agent获取消息 (重复间隔,重复次数) 进行相应的处理!
|
||||
// todo 解决创建太多对象的问题,需要缓存相应的内容
|
||||
octopusQuartzService.addJob(
|
||||
octopusQuartzService.addMission(
|
||||
AgentRunMetricStatusJob.class,
|
||||
"agentRunMetricStatusJob",
|
||||
JOB_GROUP_NAME,
|
||||
@@ -101,7 +101,7 @@ public class BuildStatusScheduleTask {
|
||||
private void buildMonitorAllAgentStatusScheduleTask() {
|
||||
|
||||
// build the Job
|
||||
octopusQuartzService.addJob(
|
||||
octopusQuartzService.addMission(
|
||||
AgentStatusMonitorJob.class,
|
||||
"monitorAllAgentStatusJob",
|
||||
JOB_GROUP_NAME,
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package io.wdd.rpc.scheduler.service;
|
||||
|
||||
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.List;
|
||||
@@ -10,7 +11,7 @@ import java.util.Map;
|
||||
public interface QuartzSchedulerService {
|
||||
|
||||
|
||||
boolean addJob(OctopusQuartzJob quartzJob);
|
||||
boolean addMission(OctopusQuartzJob quartzJob);
|
||||
|
||||
/**
|
||||
* 增加一个任务job
|
||||
@@ -21,7 +22,7 @@ public interface QuartzSchedulerService {
|
||||
* @param jobRunRepeatTimes 任务运行次数(若<0,则不限次数)
|
||||
* @param jobData 任务参数
|
||||
*/
|
||||
void addJob(Class<? extends QuartzJobBean> jobClass, String jobName, String jobGroupName, int jobRunTimePinch, int jobRunRepeatTimes, Map jobData);
|
||||
void addMission(Class<? extends QuartzJobBean> jobClass, String jobName, String jobGroupName, int jobRunTimePinch, int jobRunRepeatTimes, Map jobData);
|
||||
|
||||
/**
|
||||
* 增加一个任务job
|
||||
@@ -33,7 +34,7 @@ public interface QuartzSchedulerService {
|
||||
* @param cronJobExpression 任务时间表达式
|
||||
* @param jobData 任务参数
|
||||
*/
|
||||
void addJob(Class<? extends QuartzJobBean> jobClass, String jobName, String jobGroupName, int startTime, String cronJobExpression, Map jobData);
|
||||
void addMission(Class<? extends QuartzJobBean> jobClass, String jobName, String jobGroupName, int startTime, String cronJobExpression, Map jobData);
|
||||
|
||||
/**
|
||||
* 修改一个任务job
|
||||
@@ -41,7 +42,7 @@ public interface QuartzSchedulerService {
|
||||
* @param jobGroupName 任务组名
|
||||
* @param jobTime cron时间表达式
|
||||
*/
|
||||
void updateJob(String jobName, String jobGroupName, String jobTime);
|
||||
void updateMission(String jobName, String jobGroupName, String jobTime);
|
||||
|
||||
|
||||
/**
|
||||
@@ -51,21 +52,21 @@ public interface QuartzSchedulerService {
|
||||
* @param jobGroupName
|
||||
* @return
|
||||
*/
|
||||
boolean deleteJob(String jobName, String jobGroupName);
|
||||
boolean deleteMission(String jobName, String jobGroupName);
|
||||
|
||||
/**
|
||||
* 暂停一个任务job
|
||||
* @param jobName
|
||||
* @param jobGroupName
|
||||
*/
|
||||
void pauseJob(String jobName, String jobGroupName);
|
||||
void pauseMission(String jobName, String jobGroupName);
|
||||
|
||||
/**
|
||||
* 恢复一个任务job
|
||||
* @param jobName
|
||||
* @param jobGroupName
|
||||
*/
|
||||
void resumeJob(String jobName, String jobGroupName);
|
||||
void resumeMission(String jobName, String jobGroupName);
|
||||
|
||||
/**
|
||||
* 立即执行一个任务job
|
||||
@@ -78,13 +79,13 @@ public interface QuartzSchedulerService {
|
||||
* 获取所有任务job
|
||||
* @return
|
||||
*/
|
||||
List<Map<String, Object>> queryAllJob();
|
||||
List<Map<String, Object>> queryAllMission();
|
||||
|
||||
/**
|
||||
* 获取正在运行的任务job
|
||||
* @return
|
||||
*/
|
||||
List<Map<String, Object>> queryRunJob();
|
||||
List<Map<String, Object>> queryRunMission();
|
||||
|
||||
|
||||
/**
|
||||
@@ -94,4 +95,12 @@ public interface QuartzSchedulerService {
|
||||
List<Trigger> queryAllTrigger();
|
||||
|
||||
|
||||
/**
|
||||
* --------------------------------------------------------------
|
||||
* 页面定时任务部分
|
||||
* 应该只有脚本功能才可以定时,目前一阶段的功能
|
||||
* */
|
||||
void createScriptScheduledMission(ScriptSchedulerVO scriptSchedulerVO);
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -1,9 +1,19 @@
|
||||
package io.wdd.rpc.scheduler.service;
|
||||
|
||||
import io.wdd.common.handler.MyRuntimeException;
|
||||
import io.wdd.common.utils.FunctionReader;
|
||||
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.job.AgentScriptSchedulerJob;
|
||||
import io.wdd.server.beans.po.ScriptSchedulerPO;
|
||||
import io.wdd.server.service.ScriptSchedulerService;
|
||||
import lombok.SneakyThrows;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.beanutils.BeanUtils;
|
||||
import org.apache.commons.lang3.ObjectUtils;
|
||||
import org.apache.commons.lang3.RandomStringUtils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.quartz.*;
|
||||
import org.quartz.DateBuilder.IntervalUnit;
|
||||
import org.quartz.impl.matchers.GroupMatcher;
|
||||
@@ -12,9 +22,11 @@ import org.springframework.scheduling.quartz.QuartzJobBean;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import javax.annotation.Resource;
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static io.wdd.rpc.scheduler.beans.ScriptSchedulerVO.SCHEDULE_MISSION_GROUP_NAME;
|
||||
import static io.wdd.rpc.scheduler.service.BuildStatusScheduleTask.JOB_GROUP_NAME;
|
||||
import static org.quartz.TriggerBuilder.newTrigger;
|
||||
|
||||
@@ -29,6 +41,113 @@ public class QuartzSchedulerServiceImpl implements QuartzSchedulerService {
|
||||
@Autowired
|
||||
private Scheduler scheduler;
|
||||
|
||||
@Resource
|
||||
ScriptSchedulerService scriptSchedulerService;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* --------------------------------------------------------------
|
||||
* 页面定时任务部分
|
||||
* 应该只有脚本功能才可以定时,目前一阶段的功能
|
||||
* */
|
||||
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
* */
|
||||
@Override
|
||||
public void createScriptScheduledMission(ScriptSchedulerVO scriptSchedulerVO) {
|
||||
|
||||
// validate important value
|
||||
ScriptSchedulerDTO scriptSchedulerDTO = validateAndConvertToScriptSchedulerDTO(scriptSchedulerVO);
|
||||
|
||||
// build the job
|
||||
// build the trigger
|
||||
// bind job and trigger
|
||||
HashMap<String, Object> dataMap = new HashMap<>();
|
||||
dataMap.put("scriptSchedulerDTO", scriptSchedulerDTO);
|
||||
this.addMission(
|
||||
AgentScriptSchedulerJob.class,
|
||||
scriptSchedulerVO.getName(),
|
||||
SCHEDULE_MISSION_GROUP_NAME,
|
||||
1,
|
||||
scriptSchedulerVO.getScriptContent(),
|
||||
dataMap
|
||||
);
|
||||
|
||||
// persistent the script scheduled mission
|
||||
// todo dto should store more info
|
||||
ScriptSchedulerPO scriptSchedulerPO = convertToScriptSchedulerPO(scriptSchedulerDTO);
|
||||
scriptSchedulerService.save(scriptSchedulerPO);
|
||||
}
|
||||
|
||||
/**
|
||||
* 转换 中间层 --> 持久层
|
||||
*
|
||||
* @param dto 定时脚本任务-中间转换状态-实体类
|
||||
* @return 定时脚本任务-持久化-实体类
|
||||
*/
|
||||
private ScriptSchedulerPO convertToScriptSchedulerPO(ScriptSchedulerDTO dto) {
|
||||
// todo should be a static method
|
||||
return ScriptSchedulerPO
|
||||
.builder()
|
||||
.cronExpress(dto.getCronExpress())
|
||||
.schedulerUuid(dto.getSchedulerUuid())
|
||||
.scriptContent(String.valueOf(dto.getCommandList()))
|
||||
.targetMachine(String.valueOf(dto.getTargetMachineList()))
|
||||
.build();
|
||||
}
|
||||
|
||||
/**
|
||||
* 转换 视图层 --> 中间层
|
||||
* @param scriptSchedulerVO 定时脚本任务-前端页面-实体类
|
||||
* @return 定时脚本任务-中间转换状态-实体类
|
||||
*/
|
||||
@SneakyThrows
|
||||
private ScriptSchedulerDTO validateAndConvertToScriptSchedulerDTO(ScriptSchedulerVO scriptSchedulerVO) {
|
||||
|
||||
// 验证cron表达式
|
||||
if (!CronExpression.isValidExpression(scriptSchedulerVO.getCronExpress())) {
|
||||
throw new MyRuntimeException("cron express wrong !");
|
||||
}
|
||||
|
||||
// 归一化 commandList
|
||||
List<List<String>> commandList = FunctionReader.ReadStringToCommandList(scriptSchedulerVO.getScriptContent());
|
||||
if (commandList.size() == 0) {
|
||||
throw new MyRuntimeException("commandList parse wrong !");
|
||||
}
|
||||
|
||||
// 执行机器目标归一化
|
||||
String[] targetMachineSplit = scriptSchedulerVO
|
||||
.getTargetMachine()
|
||||
.split(",");
|
||||
if (targetMachineSplit.length == 0) {
|
||||
throw new MyRuntimeException("target machine wrong !");
|
||||
}
|
||||
ArrayList<String> targetMachineList = new ArrayList<>();
|
||||
targetMachineList.stream().forEach(
|
||||
targetMachineList::add
|
||||
);
|
||||
|
||||
// 生成DTO对象
|
||||
ScriptSchedulerDTO dto = new ScriptSchedulerDTO();
|
||||
BeanUtils.copyProperties(dto,scriptSchedulerVO);
|
||||
|
||||
// 设置属性值
|
||||
dto.setCommandList(null);
|
||||
dto.setTargetMachineList(targetMachineList);
|
||||
|
||||
// 生成 scheduler uuid
|
||||
String uuid = RandomStringUtils.randomAlphabetic(32);
|
||||
dto.setSchedulerUuid(uuid);
|
||||
|
||||
return dto;
|
||||
}
|
||||
|
||||
|
||||
@PostConstruct
|
||||
public void startScheduler() {
|
||||
try {
|
||||
@@ -39,7 +158,7 @@ public class QuartzSchedulerServiceImpl implements QuartzSchedulerService {
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean addJob(OctopusQuartzJob quartzJob) {
|
||||
public boolean addMission(OctopusQuartzJob quartzJob) {
|
||||
|
||||
|
||||
return false;
|
||||
@@ -56,7 +175,7 @@ public class QuartzSchedulerServiceImpl implements QuartzSchedulerService {
|
||||
* @param jobData 参数
|
||||
*/
|
||||
@Override
|
||||
public void addJob(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 {
|
||||
// 任务名称和组构成任务key
|
||||
JobDetail jobDetail = JobBuilder
|
||||
@@ -119,7 +238,7 @@ public class QuartzSchedulerServiceImpl implements QuartzSchedulerService {
|
||||
* @param jobData 参数
|
||||
*/
|
||||
@Override
|
||||
public void addJob(Class<? extends QuartzJobBean> jobClass, String jobName, String jobGroupName, int startTime, String cronJobExpression, Map jobData) {
|
||||
public void addMission(Class<? extends QuartzJobBean> jobClass, String jobName, String jobGroupName, int startTime, String cronJobExpression, Map jobData) {
|
||||
try {
|
||||
// 创建jobDetail实例,绑定Job实现类
|
||||
// 指明job的名称,所在组的名称,以及绑定job类
|
||||
@@ -188,7 +307,7 @@ public class QuartzSchedulerServiceImpl implements QuartzSchedulerService {
|
||||
* @param jobTime
|
||||
*/
|
||||
@Override
|
||||
public void updateJob(String jobName, String jobGroupName, String jobTime) {
|
||||
public void updateMission(String jobName, String jobGroupName, String jobTime) {
|
||||
try {
|
||||
TriggerKey triggerKey = TriggerKey.triggerKey(
|
||||
jobName,
|
||||
@@ -223,7 +342,7 @@ public class QuartzSchedulerServiceImpl implements QuartzSchedulerService {
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public boolean deleteJob(String jobName, String jobGroupName) {
|
||||
public boolean deleteMission(String jobName, String jobGroupName) {
|
||||
|
||||
try {
|
||||
scheduler.deleteJob(new JobKey(
|
||||
@@ -245,7 +364,7 @@ public class QuartzSchedulerServiceImpl implements QuartzSchedulerService {
|
||||
* @param jobGroupName
|
||||
*/
|
||||
@Override
|
||||
public void pauseJob(String jobName, String jobGroupName) {
|
||||
public void pauseMission(String jobName, String jobGroupName) {
|
||||
try {
|
||||
JobKey jobKey = JobKey.jobKey(
|
||||
jobName,
|
||||
@@ -265,7 +384,7 @@ public class QuartzSchedulerServiceImpl implements QuartzSchedulerService {
|
||||
* @param jobGroupName
|
||||
*/
|
||||
@Override
|
||||
public void resumeJob(String jobName, String jobGroupName) {
|
||||
public void resumeMission(String jobName, String jobGroupName) {
|
||||
try {
|
||||
JobKey jobKey = JobKey.jobKey(
|
||||
jobName,
|
||||
@@ -304,7 +423,7 @@ public class QuartzSchedulerServiceImpl implements QuartzSchedulerService {
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public List<Map<String, Object>> queryAllJob() {
|
||||
public List<Map<String, Object>> queryAllMission() {
|
||||
List<Map<String, Object>> jobList = null;
|
||||
try {
|
||||
GroupMatcher<JobKey> matcher = GroupMatcher.anyJobGroup();
|
||||
@@ -362,7 +481,7 @@ public class QuartzSchedulerServiceImpl implements QuartzSchedulerService {
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public List<Map<String, Object>> queryRunJob() {
|
||||
public List<Map<String, Object>> queryRunMission() {
|
||||
List<Map<String, Object>> jobList = null;
|
||||
try {
|
||||
List<JobExecutionContext> executingJobs = scheduler.getCurrentlyExecutingJobs();
|
||||
@@ -432,4 +551,7 @@ public class QuartzSchedulerServiceImpl implements QuartzSchedulerService {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
package io.wdd.rpc.scheduler.service.script;
|
||||
|
||||
|
||||
import io.wdd.rpc.execute.service.CoreExecutionService;
|
||||
import io.wdd.rpc.scheduler.beans.ScriptSchedulerDTO;
|
||||
import io.wdd.server.beans.po.ScriptSchedulerPO;
|
||||
import lombok.extern.log4j.Log4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 1.
|
||||
*/
|
||||
@Service
|
||||
@Log4j
|
||||
public class AgentApplyScheduledScript {
|
||||
|
||||
@Resource
|
||||
CoreExecutionService coreExecutionService;
|
||||
|
||||
public void apply(ScriptSchedulerDTO scriptSchedulerDTO) {
|
||||
|
||||
List<List<String>> commandList = scriptSchedulerDTO.getCommandList();
|
||||
List<String> targetMachineList = scriptSchedulerDTO.getTargetMachineList();
|
||||
|
||||
targetMachineList
|
||||
.stream()
|
||||
.map(
|
||||
targetMachine -> {
|
||||
coreExecutionService.SendCommandToAgent()
|
||||
}
|
||||
)
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package io.wdd.rpc.status;
|
||||
package io.wdd.rpc.scheduler.service.status;
|
||||
|
||||
|
||||
import io.wdd.common.beans.status.OctopusStatusMessage;
|
||||
@@ -1,4 +1,4 @@
|
||||
package io.wdd.rpc.status;
|
||||
package io.wdd.rpc.scheduler.service.status;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
@@ -1,4 +1,4 @@
|
||||
package io.wdd.rpc.status;
|
||||
package io.wdd.rpc.scheduler.service.status;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
@@ -1,4 +1,4 @@
|
||||
package io.wdd.rpc.status;
|
||||
package io.wdd.rpc.scheduler.service.status;
|
||||
|
||||
import io.wdd.common.beans.status.AgentHealthyStatusEnum;
|
||||
import io.wdd.common.beans.status.OctopusStatusMessage;
|
||||
@@ -18,7 +18,7 @@ import java.util.stream.Collectors;
|
||||
|
||||
import static io.wdd.common.beans.status.OctopusStatusMessage.ALL_AGENT_STATUS_REDIS_KEY;
|
||||
import static io.wdd.common.beans.status.OctopusStatusMessage.HEALTHY_STATUS_MESSAGE_TYPE;
|
||||
import static io.wdd.rpc.status.AgentRuntimeMetricStatus.ALL_HEALTHY_AGENT_TOPIC_NAMES;
|
||||
import static io.wdd.rpc.scheduler.service.status.AgentRuntimeMetricStatus.ALL_HEALTHY_AGENT_TOPIC_NAMES;
|
||||
|
||||
/**
|
||||
* 更新频率被类 BuildStatusScheduleTask.class控制
|
||||
@@ -0,0 +1,107 @@
|
||||
package io.wdd.server.beans.po;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.IdType;
|
||||
import com.baomidou.mybatisplus.annotation.TableField;
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import java.io.Serializable;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
import io.swagger.annotations.ApiModel;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.experimental.SuperBuilder;
|
||||
|
||||
/**
|
||||
*
|
||||
* @TableName script_scheduler
|
||||
*/
|
||||
@TableName(value ="script_scheduler")
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
@SuperBuilder(toBuilder = true)
|
||||
@ApiModel("定时脚本任务-持久化-实体类")
|
||||
public class ScriptSchedulerPO implements Serializable {
|
||||
/**
|
||||
*
|
||||
*/
|
||||
@TableId(value = "scheduler_uuid")
|
||||
private String schedulerUuid;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
@TableField(value = "name")
|
||||
private String name;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
@TableField(value = "cron_express")
|
||||
private String cronExpress;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
@TableField(value = "description")
|
||||
private String description;
|
||||
|
||||
/**
|
||||
* 脚本任务的内容
|
||||
*/
|
||||
@TableField(value = "script_content")
|
||||
private String scriptContent;
|
||||
|
||||
/**
|
||||
* 执行目标机器agent_topic_name列表,使用, 分隔
|
||||
*/
|
||||
@TableField(value = "target_machine")
|
||||
private String targetMachine;
|
||||
|
||||
/**
|
||||
* 与 execution_log表的主键对应,方便查询执行日志
|
||||
*/
|
||||
@TableField(value = "last_execution_id")
|
||||
private Long lastExecutionId;
|
||||
|
||||
/**
|
||||
* 与 execution_log表的 result_key 对应,方便查询执行日志
|
||||
*/
|
||||
@TableField(value = "last_execution_result_key")
|
||||
private String lastExecutionResultKey;
|
||||
|
||||
/**
|
||||
* 任务上次执行状态
|
||||
*/
|
||||
@TableField(value = "last_execution_status")
|
||||
private Integer lastExecutionStatus;
|
||||
|
||||
/**
|
||||
* 定时脚本任务创建时间
|
||||
*/
|
||||
@TableField(value = "create_time")
|
||||
private LocalDateTime createTime;
|
||||
|
||||
/**
|
||||
* 上次更新时间
|
||||
*/
|
||||
@TableField(value = "update_time")
|
||||
private LocalDateTime updateTime;
|
||||
|
||||
/**
|
||||
* 任务下次计划执行时间
|
||||
*/
|
||||
@TableField(value = "next_schedule_time")
|
||||
private LocalDateTime nextScheduleTime;
|
||||
|
||||
/**
|
||||
* 任务上次计划执行时间
|
||||
*/
|
||||
@TableField(value = "last_schedule_time")
|
||||
private LocalDateTime lastScheduleTime;
|
||||
|
||||
@TableField(exist = false)
|
||||
private static final long serialVersionUID = 1L;
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package io.wdd.server.mapper;
|
||||
|
||||
import io.wdd.server.beans.po.ScriptSchedulerPO;
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
|
||||
/**
|
||||
* @author wdd
|
||||
* @description 针对表【script_scheduler】的数据库操作Mapper
|
||||
* @createDate 2023-01-17 10:29:22
|
||||
* @Entity io.wdd.server.beans.po.ScriptSchedulerPO
|
||||
*/
|
||||
public interface ScriptSchedulerMapper extends BaseMapper<ScriptSchedulerPO> {
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
package io.wdd.server.service;
|
||||
|
||||
import io.wdd.server.beans.po.ScriptSchedulerPO;
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
|
||||
/**
|
||||
* @author wdd
|
||||
* @description 针对表【script_scheduler】的数据库操作Service
|
||||
* @createDate 2023-01-17 10:29:22
|
||||
*/
|
||||
public interface ScriptSchedulerService extends IService<ScriptSchedulerPO> {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package io.wdd.server.service.impl;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import io.wdd.server.beans.po.ScriptSchedulerPO;
|
||||
import io.wdd.server.service.ScriptSchedulerService;
|
||||
import io.wdd.server.mapper.ScriptSchedulerMapper;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
/**
|
||||
* @author wdd
|
||||
* @description 针对表【script_scheduler】的数据库操作Service实现
|
||||
* @createDate 2023-01-17 10:29:22
|
||||
*/
|
||||
@Service
|
||||
public class ScriptSchedulerServiceImpl extends ServiceImpl<ScriptSchedulerMapper, ScriptSchedulerPO>
|
||||
implements ScriptSchedulerService{
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
30
server/src/main/resources/mapper/ScriptSchedulerMapper.xml
Normal file
30
server/src/main/resources/mapper/ScriptSchedulerMapper.xml
Normal file
@@ -0,0 +1,30 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE mapper
|
||||
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
||||
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||
<mapper namespace="io.wdd.server.mapper.ScriptSchedulerMapper">
|
||||
|
||||
<resultMap id="BaseResultMap" type="io.wdd.server.beans.po.ScriptSchedulerPO">
|
||||
<id property="schedulerUuid" column="scheduler_uuid" jdbcType="VARCHAR"/>
|
||||
<result property="name" column="name" jdbcType="VARCHAR"/>
|
||||
<result property="cronExpress" column="cron_express" jdbcType="VARCHAR"/>
|
||||
<result property="description" column="description" jdbcType="VARCHAR"/>
|
||||
<result property="scriptContent" column="script_content" jdbcType="VARCHAR"/>
|
||||
<result property="targetMachine" column="target_machine" jdbcType="VARCHAR"/>
|
||||
<result property="lastExecutionId" column="last_execution_id" jdbcType="BIGINT"/>
|
||||
<result property="lastExecutionResultKey" column="last_execution_result_key" jdbcType="VARCHAR"/>
|
||||
<result property="lastExecutionStatus" column="last_execution_status" jdbcType="TINYINT"/>
|
||||
<result property="createTime" column="create_time" jdbcType="TIMESTAMP"/>
|
||||
<result property="updateTime" column="update_time" jdbcType="TIMESTAMP"/>
|
||||
<result property="nextScheduleTime" column="next_schedule_time" jdbcType="TIMESTAMP"/>
|
||||
<result property="lastScheduleTime" column="last_schedule_time" jdbcType="TIMESTAMP"/>
|
||||
</resultMap>
|
||||
|
||||
<sql id="Base_Column_List">
|
||||
scheduler_uuid,name,cron_express,
|
||||
description,script_content,target_machine,
|
||||
last_execution_id,last_execution_result_key,last_execution_status,
|
||||
create_time,update_time,next_schedule_time,
|
||||
last_schedule_time
|
||||
</sql>
|
||||
</mapper>
|
||||
Reference in New Issue
Block a user