mirror of
https://gitee.com/dolphinscheduler/DolphinScheduler.git
synced 2024-12-02 04:08:31 +08:00
[Bug-8053] Fix CronUtils.getMaxCycle return null (#8086)
This commit is contained in:
parent
b705a08794
commit
e0a99dd909
@ -173,6 +173,16 @@ public abstract class AbstractCycle {
|
|||||||
FieldExpression dayOfWeekFieldExpression = dayOfWeekField.getExpression();
|
FieldExpression dayOfWeekFieldExpression = dayOfWeekField.getExpression();
|
||||||
return (dayOfWeekFieldExpression instanceof Every || dayOfWeekFieldExpression instanceof Always);
|
return (dayOfWeekFieldExpression instanceof Every || dayOfWeekFieldExpression instanceof Always);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* whether the year field has a value of every or always
|
||||||
|
*
|
||||||
|
* @return if year field has a value of every or always return true,else return false
|
||||||
|
*/
|
||||||
|
protected boolean yearFieldIsEvery() {
|
||||||
|
FieldExpression yearFieldExpression = yearField.getExpression();
|
||||||
|
return (yearFieldExpression instanceof Every || yearFieldExpression instanceof Always);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* get cycle enum
|
* get cycle enum
|
||||||
|
@ -22,6 +22,7 @@ import static org.apache.dolphinscheduler.service.quartz.cron.CycleFactory.hour;
|
|||||||
import static org.apache.dolphinscheduler.service.quartz.cron.CycleFactory.min;
|
import static org.apache.dolphinscheduler.service.quartz.cron.CycleFactory.min;
|
||||||
import static org.apache.dolphinscheduler.service.quartz.cron.CycleFactory.month;
|
import static org.apache.dolphinscheduler.service.quartz.cron.CycleFactory.month;
|
||||||
import static org.apache.dolphinscheduler.service.quartz.cron.CycleFactory.week;
|
import static org.apache.dolphinscheduler.service.quartz.cron.CycleFactory.week;
|
||||||
|
import static org.apache.dolphinscheduler.service.quartz.cron.CycleFactory.year;
|
||||||
|
|
||||||
import static com.cronutils.model.CronType.QUARTZ;
|
import static com.cronutils.model.CronType.QUARTZ;
|
||||||
|
|
||||||
@ -90,7 +91,7 @@ public class CronUtils {
|
|||||||
* @return CycleEnum
|
* @return CycleEnum
|
||||||
*/
|
*/
|
||||||
public static CycleEnum getMaxCycle(Cron cron) {
|
public static CycleEnum getMaxCycle(Cron cron) {
|
||||||
return min(cron).addCycle(hour(cron)).addCycle(day(cron)).addCycle(week(cron)).addCycle(month(cron)).getCycle();
|
return min(cron).addCycle(hour(cron)).addCycle(day(cron)).addCycle(week(cron)).addCycle(month(cron)).addCycle(year(cron)).getCycle();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -100,7 +101,7 @@ public class CronUtils {
|
|||||||
* @return CycleEnum
|
* @return CycleEnum
|
||||||
*/
|
*/
|
||||||
public static CycleEnum getMiniCycle(Cron cron) {
|
public static CycleEnum getMiniCycle(Cron cron) {
|
||||||
return min(cron).addCycle(hour(cron)).addCycle(day(cron)).addCycle(week(cron)).addCycle(month(cron)).getMiniCycle();
|
return min(cron).addCycle(hour(cron)).addCycle(day(cron)).addCycle(week(cron)).addCycle(month(cron)).addCycle(year(cron)).getMiniCycle();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -186,7 +187,7 @@ public class CronUtils {
|
|||||||
*/
|
*/
|
||||||
public static List<Date> getSelfFireDateList(final Date startTime, final Date endTime, final List<Schedule> schedules) {
|
public static List<Date> getSelfFireDateList(final Date startTime, final Date endTime, final List<Schedule> schedules) {
|
||||||
List<Date> result = new ArrayList<>();
|
List<Date> result = new ArrayList<>();
|
||||||
if(startTime.equals(endTime)){
|
if (startTime.equals(endTime)) {
|
||||||
result.add(startTime);
|
result.add(startTime);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
@ -72,6 +72,15 @@ public class CycleFactory {
|
|||||||
public static AbstractCycle month(Cron cron) {
|
public static AbstractCycle month(Cron cron) {
|
||||||
return new MonthCycle(cron);
|
return new MonthCycle(cron);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* year
|
||||||
|
* @param cron cron
|
||||||
|
* @return AbstractCycle
|
||||||
|
*/
|
||||||
|
public static AbstractCycle year(Cron cron) {
|
||||||
|
return new YearCycle(cron);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* day cycle
|
* day cycle
|
||||||
@ -275,4 +284,52 @@ public class CycleFactory {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* year cycle
|
||||||
|
*/
|
||||||
|
public static class YearCycle extends AbstractCycle {
|
||||||
|
public YearCycle(Cron cron) {
|
||||||
|
super(cron);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* get cycle
|
||||||
|
* @return CycleEnum
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
protected CycleEnum getCycle() {
|
||||||
|
boolean flag = (minFiledIsSetAll()
|
||||||
|
&& hourFiledIsSetAll()
|
||||||
|
&& dayOfMonthFieldIsSetAll()
|
||||||
|
&& dayOfWeekField.getExpression() instanceof QuestionMark
|
||||||
|
&& monthFieldIsSetAll())
|
||||||
|
&& yearFieldIsEvery() ||
|
||||||
|
(minFiledIsSetAll()
|
||||||
|
&& hourFiledIsSetAll()
|
||||||
|
&& dayOfMonthField.getExpression() instanceof QuestionMark
|
||||||
|
&& dayofWeekFieldIsSetAll()
|
||||||
|
&& monthFieldIsSetAll()
|
||||||
|
&& yearFieldIsEvery());
|
||||||
|
|
||||||
|
if (flag) {
|
||||||
|
return CycleEnum.YEAR;
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* get mini cycle
|
||||||
|
* @return CycleEnum
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
protected CycleEnum getMiniCycle() {
|
||||||
|
if (yearFieldIsEvery()) {
|
||||||
|
return CycleEnum.YEAR;
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -95,6 +95,20 @@ public class CronUtilsTest {
|
|||||||
|
|
||||||
CycleEnum cycleEnum3 = CronUtils.getMiniCycle(CronUtils.parse2Cron("0 * * * * ? *"));
|
CycleEnum cycleEnum3 = CronUtils.getMiniCycle(CronUtils.parse2Cron("0 * * * * ? *"));
|
||||||
Assert.assertEquals("MINUTE", cycleEnum3.name());
|
Assert.assertEquals("MINUTE", cycleEnum3.name());
|
||||||
|
|
||||||
|
CycleEnum cycleEnum4 = CronUtils.getMaxCycle(CronUtils.parse2Cron("0 0 7 * 1 ? *"));
|
||||||
|
Assert.assertEquals("YEAR", cycleEnum4.name());
|
||||||
|
cycleEnum4 = CronUtils.getMiniCycle(CronUtils.parse2Cron("0 0 7 * 1 ? *"));
|
||||||
|
Assert.assertEquals("DAY", cycleEnum4.name());
|
||||||
|
|
||||||
|
CycleEnum cycleEnum5 = CronUtils.getMaxCycle(CronUtils.parse2Cron("0 0 7 * 1/1 ? *"));
|
||||||
|
Assert.assertEquals("MONTH", cycleEnum5.name());
|
||||||
|
|
||||||
|
CycleEnum cycleEnum6 = CronUtils.getMaxCycle(CronUtils.parse2Cron("0 0 7 * 1-2 ? *"));
|
||||||
|
Assert.assertEquals("YEAR", cycleEnum6.name());
|
||||||
|
|
||||||
|
CycleEnum cycleEnum7 = CronUtils.getMaxCycle(CronUtils.parse2Cron("0 0 7 * 1,2 ? *"));
|
||||||
|
Assert.assertEquals("YEAR", cycleEnum7.name());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -113,7 +127,7 @@ public class CronUtilsTest {
|
|||||||
.instance();
|
.instance();
|
||||||
// minute cycle
|
// minute cycle
|
||||||
String[] cronArayy = new String[]{"* * * * * ? *","* 0 * * * ? *",
|
String[] cronArayy = new String[]{"* * * * * ? *","* 0 * * * ? *",
|
||||||
"* 5 * * 3/5 ? *","0 0 * * * ? *"};
|
"* 5 * * 3/5 ? *","0 0 * * * ? *", "0 0 7 * 1 ? *", "0 0 7 * 1/1 ? *", "0 0 7 * 1-2 ? *" , "0 0 7 * 1,2 ? *"};
|
||||||
for(String minCrontab:cronArayy){
|
for(String minCrontab:cronArayy){
|
||||||
if (!org.quartz.CronExpression.isValidExpression(minCrontab)) {
|
if (!org.quartz.CronExpression.isValidExpression(minCrontab)) {
|
||||||
throw new RuntimeException(minCrontab+" verify failure, cron expression not valid");
|
throw new RuntimeException(minCrontab+" verify failure, cron expression not valid");
|
||||||
@ -155,6 +169,14 @@ public class CronUtilsTest {
|
|||||||
logger.info("dayOfWeekField instanceof On:"+(dayOfWeekField.getExpression() instanceof On));
|
logger.info("dayOfWeekField instanceof On:"+(dayOfWeekField.getExpression() instanceof On));
|
||||||
logger.info("dayOfWeekField instanceof And:"+(dayOfWeekField.getExpression() instanceof And));
|
logger.info("dayOfWeekField instanceof And:"+(dayOfWeekField.getExpression() instanceof And));
|
||||||
logger.info("dayOfWeekField instanceof QuestionMark:"+(dayOfWeekField.getExpression() instanceof QuestionMark));
|
logger.info("dayOfWeekField instanceof QuestionMark:"+(dayOfWeekField.getExpression() instanceof QuestionMark));
|
||||||
|
|
||||||
|
CronField yearField = cron.retrieve(CronFieldName.YEAR);
|
||||||
|
logger.info("yearField instanceof Between:"+(yearField.getExpression() instanceof Between));
|
||||||
|
logger.info("yearField instanceof Always:"+(yearField.getExpression() instanceof Always));
|
||||||
|
logger.info("yearField instanceof Every:"+(yearField.getExpression() instanceof Every));
|
||||||
|
logger.info("yearField instanceof On:"+(yearField.getExpression() instanceof On));
|
||||||
|
logger.info("yearField instanceof And:"+(yearField.getExpression() instanceof And));
|
||||||
|
logger.info("yearField instanceof QuestionMark:"+(yearField.getExpression() instanceof QuestionMark));
|
||||||
|
|
||||||
CycleEnum cycleEnum = CronUtils.getMaxCycle(minCrontab);
|
CycleEnum cycleEnum = CronUtils.getMaxCycle(minCrontab);
|
||||||
if(cycleEnum !=null){
|
if(cycleEnum !=null){
|
||||||
@ -204,4 +226,4 @@ public class CronUtilsTest {
|
|||||||
expirationTime = CronUtils.getExpirationTime(startTime, CycleEnum.YEAR);
|
expirationTime = CronUtils.getExpirationTime(startTime, CycleEnum.YEAR);
|
||||||
Assert.assertEquals("2020-02-07 18:30:00", DateUtils.dateToString(expirationTime));
|
Assert.assertEquals("2020-02-07 18:30:00", DateUtils.dateToString(expirationTime));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user