refactor(cron): dedupe AWS CronDefinition construction (#3707)

ScheduleExpressionParser (EventBridge) and SchedulerExpressionParser
(Scheduler) each independently built an identical cron-utils
CronDefinition, a 20-line block duplicated verbatim, found via CPD
(#3701). This included a non-obvious AWS-specific correction (AWS
numbers day-of-week 1-7 as SUN-SAT, not the Unix 0-6 that cron-utils
defaults to, so Monday is 2) that was only explained in one of the
two copies.

Extracted the construction into AwsCronDefinitions.newParser() in
core.common, and pointed both parsers at it. No behavior change: the
definition is identical, only its location changed. Both parsers
already have direct test coverage of the day-of-week quirk
(dayOfWeekOneIsSunday, dayOfWeekTwoIsMonday,
dayOfWeekSevenIsSaturdayAndIsAccepted), so no new tests were needed.
This commit is contained in:
Priamo Germosen authored and GitHub committed 2026-09-15 21:56:56 -05:00
1 parent 8a074fcddd
commit 7fee554381
3 files changed
+40 -40

No files matched your search

@@ -0,0 +1,36 @@
package io.github.hectorvent.floci.core.common;
import com.cronutils.model.definition.CronDefinition;
import com.cronutils.model.definition.CronDefinitionBuilder;
import com.cronutils.parser.CronParser;
/**
* Builds the cron-utils {@link CronParser} for AWS's six-field EventBridge cron dialect
* (minute hour day-of-month month day-of-week year), shared by EventBridge's own schedule
* expressions ({@code ScheduleExpressionParser}) and EventBridge Scheduler's {@code cron()}
* expressions ({@code SchedulerExpressionParser}), which independently built the identical
* definition before this class existed.
*
* <p>AWS numbers day-of-week 1-7 as SUN-SAT, not the Unix 0-6 SUN-SAT that cron-utils defaults
* to, so Monday is 2. Without this every numeric day fires one day late and 7 (Saturday) does
* not parse at all.
*/
public final class AwsCronDefinitions {
private AwsCronDefinitions() {
}
public static CronParser newParser() {
CronDefinition definition = CronDefinitionBuilder.defineCron()
.withSeconds().and()
.withMinutes().and()
.withHours().and()
.withDayOfMonth().supportsHash().supportsL().supportsW().supportsQuestionMark().and()
.withMonth().and()
.withDayOfWeek().withValidRange(1, 7).withMondayDoWValue(2)
.supportsHash().supportsL().supportsW().supportsQuestionMark().and()
.withYear().optional().and()
.instance();
return new CronParser(definition);
}
}
@@ -1,10 +1,9 @@
package io.github.hectorvent.floci.services.eventbridge;
import com.cronutils.model.Cron;
import com.cronutils.model.definition.CronDefinition;
import com.cronutils.model.definition.CronDefinitionBuilder;
import com.cronutils.model.time.ExecutionTime;
import com.cronutils.parser.CronParser;
import io.github.hectorvent.floci.core.common.AwsCronDefinitions;
import java.time.ZonedDateTime;
import java.util.regex.Matcher;
@@ -20,24 +19,7 @@ public final class ScheduleExpressionParser {
"^cron\\((.+)\\)$",
Pattern.CASE_INSENSITIVE);
private static final CronParser CRON_PARSER;
static {
CronDefinition definition = CronDefinitionBuilder.defineCron()
.withSeconds().and()
.withMinutes().and()
.withHours().and()
.withDayOfMonth().supportsHash().supportsL().supportsW().supportsQuestionMark().and()
.withMonth().and()
// AWS numbers day-of-week 1-7 as SUN-SAT, not the Unix 0-6 SUN-SAT that cron-utils
// defaults to, so Monday is 2. Without this every numeric day fires one day late and
// 7 (Saturday) does not parse at all.
.withDayOfWeek().withValidRange(1, 7).withMondayDoWValue(2)
.supportsHash().supportsL().supportsW().supportsQuestionMark().and()
.withYear().optional().and()
.instance();
CRON_PARSER = new CronParser(definition);
}
private static final CronParser CRON_PARSER = AwsCronDefinitions.newParser();
private ScheduleExpressionParser() {}
@@ -1,10 +1,9 @@
package io.github.hectorvent.floci.services.scheduler;
import com.cronutils.model.Cron;
import com.cronutils.model.definition.CronDefinition;
import com.cronutils.model.definition.CronDefinitionBuilder;
import com.cronutils.model.time.ExecutionTime;
import com.cronutils.parser.CronParser;
import io.github.hectorvent.floci.core.common.AwsCronDefinitions;
import java.time.Instant;
import java.time.LocalDateTime;
@@ -43,24 +42,7 @@ public final class SchedulerExpressionParser {
private static final DateTimeFormatter AT_FORMATTER =
DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss");
private static final CronParser CRON_PARSER;
static {
CronDefinition definition = CronDefinitionBuilder.defineCron()
.withSeconds().and()
.withMinutes().and()
.withHours().and()
.withDayOfMonth().supportsHash().supportsL().supportsW().supportsQuestionMark().and()
.withMonth().and()
// AWS numbers day-of-week 1-7 as SUN-SAT, not the Unix 0-6 SUN-SAT that cron-utils
// defaults to, so Monday is 2. Without this every numeric day fires one day late and
// 7 (Saturday) does not parse at all.
.withDayOfWeek().withValidRange(1, 7).withMondayDoWValue(2)
.supportsHash().supportsL().supportsW().supportsQuestionMark().and()
.withYear().optional().and()
.instance();
CRON_PARSER = new CronParser(definition);
}
private static final CronParser CRON_PARSER = AwsCronDefinitions.newParser();
public enum Kind { AT, RATE, CRON }