Skip to content

Commit

Permalink
Scheduler - fix a regression in validation
Browse files Browse the repository at this point in the history
- if a scheduled method has a config-based value in every() or cron()
the rest of the values is not validated at build time
  • Loading branch information
mkouba committed Jun 22, 2021
1 parent fa018e6 commit c38991f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -331,33 +331,30 @@ private Throwable validateScheduled(CronParser parser, AnnotationInstance schedu
AnnotationValue everyValue = schedule.value("every");
if (cronValue != null && !cronValue.asString().trim().isEmpty()) {
String cron = cronValue.asString().trim();
if (SchedulerUtils.isConfigValue(cron)) {
// Don't validate config property
return null;
}
try {
parser.parse(cron).validate();
} catch (IllegalArgumentException e) {
return new IllegalStateException("Invalid cron() expression on: " + schedule, e);
}
if (everyValue != null && !everyValue.asString().trim().isEmpty()) {
LOGGER.warnf(
"%s declared on %s#%s() defines both cron() and every() - the cron expression takes precedence",
schedule, method.declaringClass().name(), method.name());
if (!SchedulerUtils.isConfigValue(cron)) {
try {
parser.parse(cron).validate();
} catch (IllegalArgumentException e) {
return new IllegalStateException("Invalid cron() expression on: " + schedule, e);
}
if (everyValue != null && !everyValue.asString().trim().isEmpty()) {
LOGGER.warnf(
"%s declared on %s#%s() defines both cron() and every() - the cron expression takes precedence",
schedule, method.declaringClass().name(), method.name());
}
}
} else {
if (everyValue != null && !everyValue.asString().trim().isEmpty()) {
String every = everyValue.asString().trim();
if (SchedulerUtils.isConfigValue(every)) {
return null;
}
if (Character.isDigit(every.charAt(0))) {
every = "PT" + every;
}
try {
Duration.parse(every);
} catch (Exception e) {
return new IllegalStateException("Invalid every() expression on: " + schedule, e);
if (!SchedulerUtils.isConfigValue(every)) {
if (Character.isDigit(every.charAt(0))) {
every = "PT" + every;
}
try {
Duration.parse(every);
} catch (Exception e) {
return new IllegalStateException("Invalid every() expression on: " + schedule, e);
}
}
} else {
return new IllegalStateException("@Scheduled must declare either cron() or every(): " + schedule);
Expand All @@ -368,16 +365,15 @@ private Throwable validateScheduled(CronParser parser, AnnotationInstance schedu
if (delay == null || delay.asLong() <= 0) {
if (delayedValue != null && !delayedValue.asString().trim().isEmpty()) {
String delayed = delayedValue.asString().trim();
if (SchedulerUtils.isConfigValue(delayed)) {
return null;
}
if (Character.isDigit(delayed.charAt(0))) {
delayed = "PT" + delayed;
}
try {
Duration.parse(delayed);
} catch (Exception e) {
return new IllegalStateException("Invalid delayed() expression on: " + schedule, e);
if (!SchedulerUtils.isConfigValue(delayed)) {
if (Character.isDigit(delayed.charAt(0))) {
delayed = "PT" + delayed;
}
try {
Duration.parse(delayed);
} catch (Exception e) {
return new IllegalStateException("Invalid delayed() expression on: " + schedule, e);
}
}
}
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ public class InvalidDelayedExpressionTest {
static final QuarkusUnitTest test = new QuarkusUnitTest()
.setExpectedException(DeploymentException.class)
.setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class)
.addClasses(InvalidDelayedExpressionTest.InvalidBean.class));
.addClasses(InvalidBean.class));

@Test
public void test() throws InterruptedException {
}

static class InvalidBean {

@Scheduled(delayed = "for 10 seconds")
@Scheduled(every = "${my.every.expr:off}", delayed = "for 10 seconds")
void wrong() {
}

Expand Down

0 comments on commit c38991f

Please sign in to comment.