Skip to content

Commit

Permalink
Merge pull request #702 from jsonwan/github_fix/create_cron
Browse files Browse the repository at this point in the history
bugfix: 通过ESB V3接口创建一次性定时任务报错 #701
  • Loading branch information
jsonwan authored Mar 11, 2022
2 parents 37c395f + c77ff56 commit 8dcfa4b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import com.cronutils.model.Cron;
import com.cronutils.model.definition.CronDefinitionBuilder;
import com.cronutils.parser.CronParser;
import org.apache.commons.lang3.StringUtils;

import static com.cronutils.model.CronType.QUARTZ;
import static com.cronutils.model.CronType.UNIX;
Expand All @@ -41,16 +42,18 @@ public class CronExpressionUtil {
private static final CronParser UNIX_PARSER = new CronParser(CronDefinitionBuilder.instanceDefinitionFor(UNIX));

public static String fixExpressionForQuartz(String expression) {
if (StringUtils.isBlank(expression)) {
return expression;
}
Cron unixCron = UNIX_PARSER.parse(expression);
unixCron.validate();
return CronMapper.fromUnixToQuartz().map(unixCron).validate().asString();
}

public static String fixExpressionForUser(String expression) {
if (StringUtils.isBlank(expression)) {
return expression;
}
return CronMapper.fromQuartzToUnix().map(QUARTZ_PARSER.parse(expression)).asString();
}

public static void main(String[] args) {
System.out.println(CronExpressionUtil.fixExpressionForUser("0 30 10 8 * ? *"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.tencent.bk.job.crontab.util;

import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;

public class CronExpressionUtilTest {

@Test
public void testFixExpression() {
String fixedCron = CronExpressionUtil.fixExpressionForUser("0 30 10 8 * ? *");
assertThat("30 10 8 * *".equals(fixedCron)).isTrue();
assertThat(" ".equals(CronExpressionUtil.fixExpressionForUser(" "))).isTrue();
assertThat(CronExpressionUtil.fixExpressionForUser(null)).isNull();
}
}

0 comments on commit 8dcfa4b

Please sign in to comment.