[ server ] [ scheduler ]- script scheduler - 5

This commit is contained in:
zeaslity
2023-01-18 14:39:01 +08:00
parent 9211966c59
commit 96e805916b
5 changed files with 125 additions and 66 deletions

View File

@@ -12,6 +12,9 @@ import java.util.concurrent.TimeUnit;
public class TimeUtils { public class TimeUtils {
private static final ZoneId SYSTEM_TIME_ZONE_ID = ZoneId.of("UTC+8");
/** /**
* https://memorynotfound.com/calculate-relative-time-time-ago-java/ * https://memorynotfound.com/calculate-relative-time-time-ago-java/
* <p> * <p>
@@ -22,25 +25,40 @@ public class TimeUtils {
private static final Map<String, Long> times = new LinkedHashMap<>(); private static final Map<String, Long> times = new LinkedHashMap<>();
static { static {
times.put("year", times.put(
TimeUnit.DAYS.toMillis(365)); "year",
times.put("month", TimeUnit.DAYS.toMillis(365)
TimeUnit.DAYS.toMillis(30)); );
times.put("week", times.put(
TimeUnit.DAYS.toMillis(7)); "month",
times.put("day", TimeUnit.DAYS.toMillis(30)
TimeUnit.DAYS.toMillis(1)); );
times.put("hour", times.put(
TimeUnit.HOURS.toMillis(1)); "week",
times.put("minute", TimeUnit.DAYS.toMillis(7)
TimeUnit.MINUTES.toMillis(1)); );
times.put("second", times.put(
TimeUnit.SECONDS.toMillis(1)); "day",
TimeUnit.DAYS.toMillis(1)
);
times.put(
"hour",
TimeUnit.HOURS.toMillis(1)
);
times.put(
"minute",
TimeUnit.MINUTES.toMillis(1)
);
times.put(
"second",
TimeUnit.SECONDS.toMillis(1)
);
} }
public static ByteBuffer currentTimeByteBuffer() { public static ByteBuffer currentTimeByteBuffer() {
byte[] timeBytes = LocalDateTime.now(ZoneId.of("UTC+8")) byte[] timeBytes = LocalDateTime
.now(SYSTEM_TIME_ZONE_ID)
.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")) .format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))
.getBytes(StandardCharsets.UTF_8); .getBytes(StandardCharsets.UTF_8);
@@ -49,7 +67,13 @@ public class TimeUtils {
public static LocalDateTime currentTime() { public static LocalDateTime currentTime() {
return LocalDateTime.now(ZoneId.of("UTC+8")); return LocalDateTime.now(SYSTEM_TIME_ZONE_ID);
}
public static LocalDateTime cvFromDate(Date date) {
return LocalDateTime.ofInstant(date.toInstant(),
SYSTEM_TIME_ZONE_ID);
} }
/** /**
@@ -57,7 +81,8 @@ public class TimeUtils {
*/ */
public static String currentTimeString() { public static String currentTimeString() {
return LocalDateTime.now(ZoneId.of("UTC+8")) return LocalDateTime
.now(SYSTEM_TIME_ZONE_ID)
.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")); .format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
} }
@@ -66,7 +91,8 @@ public class TimeUtils {
*/ */
public static String currentTimeStringFullSplit() { public static String currentTimeStringFullSplit() {
return LocalDateTime.now(ZoneId.of("UTC+8")) return LocalDateTime
.now(SYSTEM_TIME_ZONE_ID)
.format(DateTimeFormatter.ofPattern("yyyy-MM-dd-HH-mm-ss")); .format(DateTimeFormatter.ofPattern("yyyy-MM-dd-HH-mm-ss"));
} }
@@ -82,7 +108,8 @@ public class TimeUtils {
for (Map.Entry<String, Long> time : times.entrySet()) { for (Map.Entry<String, Long> time : times.entrySet()) {
long timeDelta = duration / time.getValue(); long timeDelta = duration / time.getValue();
if (timeDelta > 0) { if (timeDelta > 0) {
res.append(timeDelta) res
.append(timeDelta)
.append(" ") .append(" ")
.append(time.getKey()) .append(time.getKey())
.append(timeDelta > 1 ? "s" : "") .append(timeDelta > 1 ? "s" : "")
@@ -104,8 +131,10 @@ public class TimeUtils {
} }
public static String toRelative(long duration) { public static String toRelative(long duration) {
return toRelative(duration, return toRelative(
times.size()); duration,
times.size()
);
} }
public static String toRelative(Date start, Date end) { public static String toRelative(Date start, Date end) {
@@ -115,7 +144,9 @@ public class TimeUtils {
public static String toRelative(Date start, Date end, int level) { public static String toRelative(Date start, Date end, int level) {
assert start.after(end); assert start.after(end);
return toRelative(end.getTime() - start.getTime(), return toRelative(
level); end.getTime() - start.getTime(),
level
);
} }
} }

View File

@@ -27,17 +27,17 @@ public class SchedulerController {
* -------------------------------------------------------------- * --------------------------------------------------------------
* 页面定时任务部分 * 页面定时任务部分
* 应该只有脚本功能才可以定时,目前一阶段的功能 * 应该只有脚本功能才可以定时,目前一阶段的功能
* */ */
@ApiOperation(value = "新增一个定时脚本任务") @ApiOperation(value = "新增一个定时脚本任务")
@PostMapping(value = "/script/create") @PostMapping(value = "/script/create")
public R<String> createScriptScheduler( public R<List<String>> createScriptScheduler(
@ApiParam(name = "scheduleScript") @RequestBody() ScriptSchedulerVO scriptSchedulerVO @ApiParam(name = "scheduleScript") @RequestBody() ScriptSchedulerVO scriptSchedulerVO
) { ) {
octopusQuartzService.createScriptScheduledMission(scriptSchedulerVO); List<String> resultList = octopusQuartzService.createScriptScheduledMission(scriptSchedulerVO);
return R.ok("ok"); return R.ok(resultList);
} }

View File

@@ -25,7 +25,7 @@ public class AgentScriptSchedulerJob extends QuartzJobBean {
.getJobDataMap(); .getJobDataMap();
// ScriptScheduleDTO // ScriptScheduleDTO
ScriptSchedulerDTO scriptSchedulerDTO = (ScriptSchedulerDTO) jobDataMap.get("scriptSchedulerPO"); ScriptSchedulerDTO scriptSchedulerDTO = (ScriptSchedulerDTO) jobDataMap.get("scriptSchedulerDTO");
// 调用实际任务执行器 // 调用实际任务执行器
agentApplyScheduledScript.apply(scriptSchedulerDTO); agentApplyScheduledScript.apply(scriptSchedulerDTO);

View File

@@ -99,8 +99,10 @@ public interface QuartzSchedulerService {
* -------------------------------------------------------------- * --------------------------------------------------------------
* 页面定时任务部分 * 页面定时任务部分
* 应该只有脚本功能才可以定时,目前一阶段的功能 * 应该只有脚本功能才可以定时,目前一阶段的功能
* */ *
void createScriptScheduledMission(ScriptSchedulerVO scriptSchedulerVO); * @return
*/
List<String> createScriptScheduledMission(ScriptSchedulerVO scriptSchedulerVO);
} }

View File

@@ -2,6 +2,7 @@ package io.wdd.rpc.scheduler.service;
import io.wdd.common.handler.MyRuntimeException; import io.wdd.common.handler.MyRuntimeException;
import io.wdd.common.utils.FunctionReader; 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.OctopusQuartzJob;
import io.wdd.rpc.scheduler.beans.ScriptSchedulerDTO; import io.wdd.rpc.scheduler.beans.ScriptSchedulerDTO;
import io.wdd.rpc.scheduler.beans.ScriptSchedulerVO; import io.wdd.rpc.scheduler.beans.ScriptSchedulerVO;
@@ -13,7 +14,6 @@ import lombok.extern.slf4j.Slf4j;
import org.apache.commons.beanutils.BeanUtils; import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang3.ObjectUtils; import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.RandomStringUtils; import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;
import org.quartz.*; import org.quartz.*;
import org.quartz.DateBuilder.IntervalUnit; import org.quartz.DateBuilder.IntervalUnit;
import org.quartz.impl.matchers.GroupMatcher; import org.quartz.impl.matchers.GroupMatcher;
@@ -23,6 +23,7 @@ import org.springframework.stereotype.Service;
import javax.annotation.PostConstruct; import javax.annotation.PostConstruct;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.*; import java.util.*;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@@ -38,12 +39,10 @@ import static org.quartz.TriggerBuilder.newTrigger;
@Service @Service
public class QuartzSchedulerServiceImpl implements QuartzSchedulerService { public class QuartzSchedulerServiceImpl implements QuartzSchedulerService {
@Autowired
private Scheduler scheduler;
@Resource @Resource
ScriptSchedulerService scriptSchedulerService; ScriptSchedulerService scriptSchedulerService;
@Autowired
private Scheduler scheduler;
/** /**
@@ -52,14 +51,11 @@ public class QuartzSchedulerServiceImpl implements QuartzSchedulerService {
* 应该只有脚本功能才可以定时,目前一阶段的功能 * 应该只有脚本功能才可以定时,目前一阶段的功能
* */ * */
/** /**
* * @return
* */
* */
@Override @Override
public void createScriptScheduledMission(ScriptSchedulerVO scriptSchedulerVO) { public List<String> createScriptScheduledMission(ScriptSchedulerVO scriptSchedulerVO) {
// validate important value // validate important value
ScriptSchedulerDTO scriptSchedulerDTO = validateAndConvertToScriptSchedulerDTO(scriptSchedulerVO); ScriptSchedulerDTO scriptSchedulerDTO = validateAndConvertToScriptSchedulerDTO(scriptSchedulerVO);
@@ -68,10 +64,13 @@ public class QuartzSchedulerServiceImpl implements QuartzSchedulerService {
// build the trigger // build the trigger
// bind job and trigger // bind job and trigger
HashMap<String, Object> dataMap = new HashMap<>(); HashMap<String, Object> dataMap = new HashMap<>();
dataMap.put("scriptSchedulerDTO", scriptSchedulerDTO); dataMap.put(
"scriptSchedulerDTO",
scriptSchedulerDTO
);
this.addMission( this.addMission(
AgentScriptSchedulerJob.class, AgentScriptSchedulerJob.class,
scriptSchedulerVO.getName(), scriptSchedulerVO.getSchedulerUuid(),
SCHEDULE_MISSION_GROUP_NAME, SCHEDULE_MISSION_GROUP_NAME,
1, 1,
scriptSchedulerVO.getScriptContent(), scriptSchedulerVO.getScriptContent(),
@@ -79,9 +78,13 @@ public class QuartzSchedulerServiceImpl implements QuartzSchedulerService {
); );
// persistent the script scheduled mission // persistent the script scheduled mission
// todo dto should store more info // dto should store more info
ScriptSchedulerPO scriptSchedulerPO = convertToScriptSchedulerPO(scriptSchedulerDTO); ScriptSchedulerPO scriptSchedulerPO = convertToScriptSchedulerPO(scriptSchedulerDTO);
scriptSchedulerService.save(scriptSchedulerPO); log.info(String.valueOf(scriptSchedulerPO));
// scriptSchedulerService.save(scriptSchedulerPO);
return scriptSchedulerDTO.getResultKeyList();
} }
/** /**
@@ -91,20 +94,42 @@ public class QuartzSchedulerServiceImpl implements QuartzSchedulerService {
* @return 定时脚本任务-持久化-实体类 * @return 定时脚本任务-持久化-实体类
*/ */
private ScriptSchedulerPO convertToScriptSchedulerPO(ScriptSchedulerDTO dto) { private ScriptSchedulerPO convertToScriptSchedulerPO(ScriptSchedulerDTO dto) {
// todo should be a static method
ScriptSchedulerPO schedulerPO = ScriptSchedulerPO
return ScriptSchedulerPO
.builder() .builder()
.cronExpress(dto.getCronExpress()) .cronExpress(dto.getCronExpress())
.schedulerUuid(dto.getSchedulerUuid()) .schedulerUuid(dto.getSchedulerUuid())
.scriptContent(String.valueOf(dto.getCommandList())) .scriptContent(String.valueOf(dto.getCommandList()))
.targetMachine(String.valueOf(dto.getTargetMachineList())) .targetMachine(String.valueOf(dto.getTargetMachineList()))
.build(); .build();
// 获取JobDetail存储上次调度时间和下次执行时间
try {
Trigger schedulerTrigger = scheduler.getTrigger(
new TriggerKey(
dto.getSchedulerUuid(),
SCHEDULE_MISSION_GROUP_NAME
)
);
LocalDateTime last_schedule_time = TimeUtils.cvFromDate(schedulerTrigger.getFinalFireTime());
LocalDateTime next_schedule_time = TimeUtils.cvFromDate(schedulerTrigger.getNextFireTime());
schedulerPO.setLastScheduleTime(last_schedule_time);
schedulerPO.setNextScheduleTime(next_schedule_time);
} catch (SchedulerException e) {
throw new RuntimeException(e);
}
return schedulerPO;
} }
/** /**
* 转换 视图层 --> 中间层 * 转换 视图层 --> 中间层
*
* @param scriptSchedulerVO 定时脚本任务-前端页面-实体类 * @param scriptSchedulerVO 定时脚本任务-前端页面-实体类
* @return 定时脚本任务-中间转换状态-实体类 * @return 定时脚本任务-中间转换状态-实体类
*/ */
@@ -130,13 +155,16 @@ public class QuartzSchedulerServiceImpl implements QuartzSchedulerService {
throw new MyRuntimeException("target machine wrong !"); throw new MyRuntimeException("target machine wrong !");
} }
ArrayList<String> targetMachineList = new ArrayList<>(); ArrayList<String> targetMachineList = new ArrayList<>();
targetMachineList.stream().forEach( targetMachineList
.stream()
.forEach(
targetMachineList::add targetMachineList::add
); );
// 生成DTO对象 // 生成DTO对象
ScriptSchedulerDTO dto = new ScriptSchedulerDTO(); ScriptSchedulerDTO dto = new ScriptSchedulerDTO();
BeanUtils.copyProperties(dto,scriptSchedulerVO); BeanUtils.copyProperties(dto,
scriptSchedulerVO);
// 设置属性值 // 设置属性值
dto.setCommandList(null); dto.setCommandList(null);
@@ -555,6 +583,4 @@ public class QuartzSchedulerServiceImpl implements QuartzSchedulerService {
} }
} }