feat: valid TimeExpression #116

This commit is contained in:
tjq 2020-11-28 20:59:04 +08:00
parent 8d369ffd21
commit ce97a87039
13 changed files with 767 additions and 584 deletions

View File

@ -0,0 +1,71 @@
package com.github.kfcfans.powerjob.server.service;
import com.github.kfcfans.powerjob.common.OmsConstant;
import com.github.kfcfans.powerjob.common.TimeExpressionType;
import com.github.kfcfans.powerjob.server.common.utils.CronExpression;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.time.DateFormatUtils;
import java.text.ParseException;
import java.util.Collections;
import java.util.Date;
import java.util.List;
/**
* 校验服务
*
* @author tjq
* @since 2020/11/28
*/
public class ValidateService {
private static final int NEXT_N_TIMES = 5;
/**
* 计算指定时间表达式接下来的运行状况
* @param timeExpressionType 时间表达式类型
* @param timeExpression 时间表达式
* @return 最近 N 次运行的时间
* @throws Exception 异常
*/
public static List<String> calculateNextTriggerTime(TimeExpressionType timeExpressionType, String timeExpression) throws Exception {
switch (timeExpressionType) {
case API: return Lists.newArrayList(OmsConstant.NONE);
case WORKFLOW: return Lists.newArrayList("VALID: depends on workflow");
case CRON: return calculateCronExpression(timeExpression);
case FIX_RATE: return calculateFixRate(timeExpression);
case FIX_DELAY: return Lists.newArrayList("VALID: depends on execution cost time");
}
// impossible
return Collections.emptyList();
}
private static List<String> calculateFixRate(String timeExpression) {
List<String> result = Lists.newArrayList();
long delay = Long.parseLong(timeExpression);
for (int i = 0; i < NEXT_N_TIMES; i++) {
long nextTime = System.currentTimeMillis() + i * delay;
result.add(DateFormatUtils.format(nextTime, OmsConstant.TIME_PATTERN));
}
return result;
}
private static List<String> calculateCronExpression(String expression) throws ParseException {
CronExpression cronExpression = new CronExpression(expression);
List<String> result = Lists.newArrayList();
Date time = new Date();
for (int i = 0; i < NEXT_N_TIMES; i++) {
Date nextValidTime = cronExpression.getNextValidTimeAfter(time);
if (nextValidTime == null) {
break;
}
result.add(DateFormatUtils.format(nextValidTime.getTime(), OmsConstant.TIME_PATTERN));
time = nextValidTime;
}
if (result.isEmpty()) {
result.add("INVALID: no next validate schedule time");
}
return result;
}
}

View File

@ -1,42 +0,0 @@
package com.github.kfcfans.powerjob.server.web.controller;
import com.github.kfcfans.powerjob.common.OmsConstant;
import com.github.kfcfans.powerjob.common.response.ResultDTO;
import com.github.kfcfans.powerjob.server.common.utils.CronExpression;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Date;
import java.util.List;
/**
* 工具 Controller
*
* @author tjq
* @since 2020/11/28
*/
@Slf4j
@RestController
@RequestMapping("/tool")
public class ToolController {
@GetMapping("/validateCron")
public ResultDTO<List<String>> calculateNextCronTriggerTime(String expression) throws Exception {
CronExpression cronExpression = new CronExpression(expression);
List<String> result = Lists.newArrayList();
Date time = new Date();
for (int i = 0; i < 10; i++) {
Date nextValidTime = cronExpression.getNextValidTimeAfter(time);
if (nextValidTime == null) {
break;
}
result.add(DateFormatUtils.format(nextValidTime.getTime(), OmsConstant.TIME_PATTERN));
time = nextValidTime;
}
return ResultDTO.success(result);
}
}

View File

@ -0,0 +1,32 @@
package com.github.kfcfans.powerjob.server.web.controller;
import com.github.kfcfans.powerjob.common.TimeExpressionType;
import com.github.kfcfans.powerjob.common.response.ResultDTO;
import com.github.kfcfans.powerjob.server.service.ValidateService;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
/**
* 校验控制器
*
* @author tjq
* @since 2020/11/28
*/
@RestController
@RequestMapping("/validate")
public class ValidateController {
@GetMapping("/timeExpression")
public ResultDTO<List<String>> checkTimeExpression(TimeExpressionType timeExpressionType, String timeExpression) {
try {
return ResultDTO.success(ValidateService.calculateNextTriggerTime(timeExpressionType, timeExpression));
} catch (Exception e) {
return ResultDTO.success(Lists.newArrayList(ExceptionUtils.getMessage(e)));
}
}
}

View File

@ -51,7 +51,9 @@ public class WorkflowInstanceInfoVO {
vo.setWorkflowId(String.valueOf(wfInstanceDO.getWorkflowId()));
// 格式化时间
vo.setExpectedTriggerTime(DateFormatUtils.format(wfInstanceDO.getExpectedTriggerTime(), OmsConstant.TIME_PATTERN));
if (wfInstanceDO.getExpectedTriggerTime() != null) {
vo.setExpectedTriggerTime(DateFormatUtils.format(wfInstanceDO.getExpectedTriggerTime(), OmsConstant.TIME_PATTERN));
}
vo.setActualTriggerTime(DateFormatUtils.format(wfInstanceDO.getActualTriggerTime(), OmsConstant.TIME_PATTERN));
if (wfInstanceDO.getFinishedTime() == null) {
vo.setFinishedTime(OmsConstant.NONE);

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long