Browse Source

[Bug-8053] Fix CronUtils.getMaxCycle return null (#8086)

3.0.0/version-upgrade
springmonster 3 years ago committed by GitHub
parent
commit
e0a99dd909
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 10
      dolphinscheduler-service/src/main/java/org/apache/dolphinscheduler/service/quartz/cron/AbstractCycle.java
  2. 7
      dolphinscheduler-service/src/main/java/org/apache/dolphinscheduler/service/quartz/cron/CronUtils.java
  3. 57
      dolphinscheduler-service/src/main/java/org/apache/dolphinscheduler/service/quartz/cron/CycleFactory.java
  4. 24
      dolphinscheduler-service/src/test/java/org/apache/dolphinscheduler/service/quartz/cron/CronUtilsTest.java

10
dolphinscheduler-service/src/main/java/org/apache/dolphinscheduler/service/quartz/cron/AbstractCycle.java

@ -174,6 +174,16 @@ public abstract class AbstractCycle {
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 trueelse return false
*/
protected boolean yearFieldIsEvery() {
FieldExpression yearFieldExpression = yearField.getExpression();
return (yearFieldExpression instanceof Every || yearFieldExpression instanceof Always);
}
/**
* get cycle enum
*

7
dolphinscheduler-service/src/main/java/org/apache/dolphinscheduler/service/quartz/cron/CronUtils.java

@ -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.month;
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;
@ -90,7 +91,7 @@ public class CronUtils {
* @return CycleEnum
*/
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
*/
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) {
List<Date> result = new ArrayList<>();
if(startTime.equals(endTime)){
if (startTime.equals(endTime)) {
result.add(startTime);
return result;
}

57
dolphinscheduler-service/src/main/java/org/apache/dolphinscheduler/service/quartz/cron/CycleFactory.java

@ -73,6 +73,15 @@ public class CycleFactory {
return new MonthCycle(cron);
}
/**
* year
* @param cron cron
* @return AbstractCycle
*/
public static AbstractCycle year(Cron cron) {
return new YearCycle(cron);
}
/**
* day cycle
*/
@ -272,6 +281,54 @@ public class CycleFactory {
return CycleEnum.WEEK;
}
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;
}
}

24
dolphinscheduler-service/src/test/java/org/apache/dolphinscheduler/service/quartz/cron/CronUtilsTest.java

@ -95,6 +95,20 @@ public class CronUtilsTest {
CycleEnum cycleEnum3 = CronUtils.getMiniCycle(CronUtils.parse2Cron("0 * * * * ? *"));
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();
// minute cycle
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){
if (!org.quartz.CronExpression.isValidExpression(minCrontab)) {
throw new RuntimeException(minCrontab+" verify failure, cron expression not valid");
@ -156,6 +170,14 @@ public class CronUtilsTest {
logger.info("dayOfWeekField instanceof And:"+(dayOfWeekField.getExpression() instanceof And));
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);
if(cycleEnum !=null){
logger.info(cycleEnum.name());

Loading…
Cancel
Save