cherry-pick [Bug] [Cron] Parse corn expression error#13841

This commit is contained in:
旺阳 2023-04-09 12:50:42 +08:00 committed by zhuangchong
parent daabcdaa01
commit 298091bf3b
4 changed files with 32 additions and 7 deletions

View File

@ -111,6 +111,7 @@ import org.apache.dolphinscheduler.plugin.task.api.enums.TaskTimeoutStrategy;
import org.apache.dolphinscheduler.plugin.task.api.model.Property; import org.apache.dolphinscheduler.plugin.task.api.model.Property;
import org.apache.dolphinscheduler.plugin.task.api.parameters.ParametersNode; import org.apache.dolphinscheduler.plugin.task.api.parameters.ParametersNode;
import org.apache.dolphinscheduler.plugin.task.api.parameters.SqlParameters; import org.apache.dolphinscheduler.plugin.task.api.parameters.SqlParameters;
import org.apache.dolphinscheduler.service.cron.CronUtils;
import org.apache.dolphinscheduler.service.model.TaskNode; import org.apache.dolphinscheduler.service.model.TaskNode;
import org.apache.dolphinscheduler.service.process.ProcessService; import org.apache.dolphinscheduler.service.process.ProcessService;
import org.apache.dolphinscheduler.service.task.TaskPluginManager; import org.apache.dolphinscheduler.service.task.TaskPluginManager;
@ -2368,8 +2369,8 @@ public class ProcessDefinitionServiceImpl extends BaseServiceImpl implements Pro
putMsg(result, Status.SCHEDULE_START_TIME_END_TIME_SAME); putMsg(result, Status.SCHEDULE_START_TIME_END_TIME_SAME);
return result; return result;
} }
if (!org.quartz.CronExpression.isValidExpression(scheduleObj.getCrontab())) { if (!CronUtils.isValidExpression(scheduleObj.getCrontab())) {
logger.error("{} verify failure", scheduleObj.getCrontab()); log.error("CronExpression verify failure, cron:{}.", scheduleObj.getCrontab());
putMsg(result, Status.REQUEST_PARAMS_NOT_VALID_ERROR, scheduleObj.getCrontab()); putMsg(result, Status.REQUEST_PARAMS_NOT_VALID_ERROR, scheduleObj.getCrontab());
return result; return result;
} }

View File

@ -177,9 +177,8 @@ public class SchedulerServiceImpl extends BaseServiceImpl implements SchedulerSe
scheduleObj.setStartTime(scheduleParam.getStartTime()); scheduleObj.setStartTime(scheduleParam.getStartTime());
scheduleObj.setEndTime(scheduleParam.getEndTime()); scheduleObj.setEndTime(scheduleParam.getEndTime());
if (!org.quartz.CronExpression.isValidExpression(scheduleParam.getCrontab())) { if (!CronUtils.isValidExpression(scheduleParam.getCrontab())) {
logger.error("{} verify failure", scheduleParam.getCrontab()); logger.error("Schedule crontab verify failure, crontab:{}.", scheduleParam.getCrontab());
putMsg(result, Status.REQUEST_PARAMS_NOT_VALID_ERROR, scheduleParam.getCrontab()); putMsg(result, Status.REQUEST_PARAMS_NOT_VALID_ERROR, scheduleParam.getCrontab());
return result; return result;
} }
@ -664,7 +663,8 @@ public class SchedulerServiceImpl extends BaseServiceImpl implements SchedulerSe
schedule.setStartTime(scheduleParam.getStartTime()); schedule.setStartTime(scheduleParam.getStartTime());
schedule.setEndTime(scheduleParam.getEndTime()); schedule.setEndTime(scheduleParam.getEndTime());
if (!org.quartz.CronExpression.isValidExpression(scheduleParam.getCrontab())) { if (!CronUtils.isValidExpression(scheduleParam.getCrontab())) {
logger.error("Schedule crontab verify failure, crontab:{}.", scheduleParam.getCrontab());
putMsg(result, Status.SCHEDULE_CRON_CHECK_FAILED, scheduleParam.getCrontab()); putMsg(result, Status.SCHEDULE_CRON_CHECK_FAILED, scheduleParam.getCrontab());
return; return;
} }

View File

@ -80,11 +80,29 @@ public class CronUtils {
public static Cron parse2Cron(String cronExpression) throws CronParseException { public static Cron parse2Cron(String cronExpression) throws CronParseException {
try { try {
return QUARTZ_CRON_PARSER.parse(cronExpression); return QUARTZ_CRON_PARSER.parse(cronExpression);
} catch (Exception ex) { } catch (IllegalArgumentException ex) {
throw new CronParseException(String.format("Parse corn expression: [%s] error", cronExpression), ex); throw new CronParseException(String.format("Parse corn expression: [%s] error", cronExpression), ex);
} }
} }
/**
* Indicates whether the specified cron expression can be parsed into a
* valid cron expression
*
* @param cronExpression the expression to evaluate
* @return a boolean indicating whether the given expression is a valid cron
* expression
*/
public static boolean isValidExpression(String cronExpression) {
try {
parse2Cron(cronExpression);
} catch (CronParseException e) {
return false;
}
return true;
}
/** /**
* get max cycle * get max cycle
* *

View File

@ -237,4 +237,10 @@ 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));
} }
@Test
public void testValid() {
Assertions.assertFalse(CronUtils.isValidExpression("0 0 13/0 * * ? *"));
Assertions.assertTrue(CronUtils.isValidExpression("0 0 13-0 * * ? *"));
}
} }