[ 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 {
private static final ZoneId SYSTEM_TIME_ZONE_ID = ZoneId.of("UTC+8");
/**
* https://memorynotfound.com/calculate-relative-time-time-ago-java/
* <p>
@@ -22,34 +25,55 @@ public class TimeUtils {
private static final Map<String, Long> times = new LinkedHashMap<>();
static {
times.put("year",
TimeUnit.DAYS.toMillis(365));
times.put("month",
TimeUnit.DAYS.toMillis(30));
times.put("week",
TimeUnit.DAYS.toMillis(7));
times.put("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));
times.put(
"year",
TimeUnit.DAYS.toMillis(365)
);
times.put(
"month",
TimeUnit.DAYS.toMillis(30)
);
times.put(
"week",
TimeUnit.DAYS.toMillis(7)
);
times.put(
"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() {
byte[] timeBytes = LocalDateTime.now(ZoneId.of("UTC+8"))
.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))
.getBytes(StandardCharsets.UTF_8);
byte[] timeBytes = LocalDateTime
.now(SYSTEM_TIME_ZONE_ID)
.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))
.getBytes(StandardCharsets.UTF_8);
return ByteBuffer.wrap(timeBytes);
}
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,8 +81,9 @@ public class TimeUtils {
*/
public static String currentTimeString() {
return LocalDateTime.now(ZoneId.of("UTC+8"))
.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
return LocalDateTime
.now(SYSTEM_TIME_ZONE_ID)
.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
}
/**
@@ -66,8 +91,9 @@ public class TimeUtils {
*/
public static String currentTimeStringFullSplit() {
return LocalDateTime.now(ZoneId.of("UTC+8"))
.format(DateTimeFormatter.ofPattern("yyyy-MM-dd-HH-mm-ss"));
return LocalDateTime
.now(SYSTEM_TIME_ZONE_ID)
.format(DateTimeFormatter.ofPattern("yyyy-MM-dd-HH-mm-ss"));
}
@@ -82,11 +108,12 @@ public class TimeUtils {
for (Map.Entry<String, Long> time : times.entrySet()) {
long timeDelta = duration / time.getValue();
if (timeDelta > 0) {
res.append(timeDelta)
.append(" ")
.append(time.getKey())
.append(timeDelta > 1 ? "s" : "")
.append(", ");
res
.append(timeDelta)
.append(" ")
.append(time.getKey())
.append(timeDelta > 1 ? "s" : "")
.append(", ");
duration -= time.getValue() * timeDelta;
level++;
}
@@ -104,8 +131,10 @@ public class TimeUtils {
}
public static String toRelative(long duration) {
return toRelative(duration,
times.size());
return toRelative(
duration,
times.size()
);
}
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) {
assert start.after(end);
return toRelative(end.getTime() - start.getTime(),
level);
return toRelative(
end.getTime() - start.getTime(),
level
);
}
}

View File

@@ -26,18 +26,18 @@ public class SchedulerController {
/**
* --------------------------------------------------------------
* 页面定时任务部分
* 应该只有脚本功能才可以定时,目前一阶段的功能
* */
* 应该只有脚本功能才可以定时,目前一阶段的功能
*/
@ApiOperation(value = "新增一个定时脚本任务")
@PostMapping(value = "/script/create")
public R<String> createScriptScheduler(
public R<List<String>> createScriptScheduler(
@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();
// ScriptScheduleDTO
ScriptSchedulerDTO scriptSchedulerDTO = (ScriptSchedulerDTO) jobDataMap.get("scriptSchedulerPO");
ScriptSchedulerDTO scriptSchedulerDTO = (ScriptSchedulerDTO) jobDataMap.get("scriptSchedulerDTO");
// 调用实际任务执行器
agentApplyScheduledScript.apply(scriptSchedulerDTO);

View File

@@ -98,9 +98,11 @@ 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.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;
@@ -13,7 +14,6 @@ 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;
@@ -23,6 +23,7 @@ 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;
@@ -38,12 +39,10 @@ import static org.quartz.TriggerBuilder.newTrigger;
@Service
public class QuartzSchedulerServiceImpl implements QuartzSchedulerService {
@Autowired
private Scheduler scheduler;
@Resource
ScriptSchedulerService scriptSchedulerService;
@Autowired
private Scheduler scheduler;
/**
@@ -52,14 +51,11 @@ public class QuartzSchedulerServiceImpl implements QuartzSchedulerService {
* 应该只有脚本功能才可以定时,目前一阶段的功能
* */
/**
*
*
* */
* @return
*/
@Override
public void createScriptScheduledMission(ScriptSchedulerVO scriptSchedulerVO) {
public List<String> createScriptScheduledMission(ScriptSchedulerVO scriptSchedulerVO) {
// validate important value
ScriptSchedulerDTO scriptSchedulerDTO = validateAndConvertToScriptSchedulerDTO(scriptSchedulerVO);
@@ -68,10 +64,13 @@ public class QuartzSchedulerServiceImpl implements QuartzSchedulerService {
// build the trigger
// bind job and trigger
HashMap<String, Object> dataMap = new HashMap<>();
dataMap.put("scriptSchedulerDTO", scriptSchedulerDTO);
dataMap.put(
"scriptSchedulerDTO",
scriptSchedulerDTO
);
this.addMission(
AgentScriptSchedulerJob.class,
scriptSchedulerVO.getName(),
scriptSchedulerVO.getSchedulerUuid(),
SCHEDULE_MISSION_GROUP_NAME,
1,
scriptSchedulerVO.getScriptContent(),
@@ -79,32 +78,58 @@ public class QuartzSchedulerServiceImpl implements QuartzSchedulerService {
);
// persistent the script scheduled mission
// todo dto should store more info
// dto should store more info
ScriptSchedulerPO scriptSchedulerPO = convertToScriptSchedulerPO(scriptSchedulerDTO);
scriptSchedulerService.save(scriptSchedulerPO);
log.info(String.valueOf(scriptSchedulerPO));
// scriptSchedulerService.save(scriptSchedulerPO);
return scriptSchedulerDTO.getResultKeyList();
}
/**
* 转换 中间层 --> 持久层
* 转换 中间层 --> 持久层
*
* @param dto 定时脚本任务-中间转换状态-实体类
* @return 定时脚本任务-持久化-实体类
*/
private ScriptSchedulerPO convertToScriptSchedulerPO(ScriptSchedulerDTO dto) {
// todo should be a static method
return ScriptSchedulerPO
ScriptSchedulerPO schedulerPO = ScriptSchedulerPO
.builder()
.cronExpress(dto.getCronExpress())
.schedulerUuid(dto.getSchedulerUuid())
.scriptContent(String.valueOf(dto.getCommandList()))
.targetMachine(String.valueOf(dto.getTargetMachineList()))
.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 定时脚本任务-前端页面-实体类
* @return 定时脚本任务-中间转换状态-实体类
*/
@@ -130,13 +155,16 @@ public class QuartzSchedulerServiceImpl implements QuartzSchedulerService {
throw new MyRuntimeException("target machine wrong !");
}
ArrayList<String> targetMachineList = new ArrayList<>();
targetMachineList.stream().forEach(
targetMachineList::add
);
targetMachineList
.stream()
.forEach(
targetMachineList::add
);
// 生成DTO对象
ScriptSchedulerDTO dto = new ScriptSchedulerDTO();
BeanUtils.copyProperties(dto,scriptSchedulerVO);
BeanUtils.copyProperties(dto,
scriptSchedulerVO);
// 设置属性值
dto.setCommandList(null);
@@ -555,6 +583,4 @@ public class QuartzSchedulerServiceImpl implements QuartzSchedulerService {
}
}