From 7fee554381c10798c65ae67574418adce10ba032 Mon Sep 17 00:00:00 2001 From: Priamo Germosen <6233424+pgermosen@users.noreply.github.com> Date: Tue, 15 Sep 2026 22:56:56 -0400 Subject: [PATCH] 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. --- .../floci/core/common/AwsCronDefinitions.java | 36 +++++++++++++++++++ .../eventbridge/ScheduleExpressionParser.java | 22 ++---------- .../scheduler/SchedulerExpressionParser.java | 22 ++---------- 3 files changed, 40 insertions(+), 40 deletions(-) create mode 100644 src/main/java/io/github/hectorvent/floci/core/common/AwsCronDefinitions.java diff --git a/src/main/java/io/github/hectorvent/floci/core/common/AwsCronDefinitions.java b/src/main/java/io/github/hectorvent/floci/core/common/AwsCronDefinitions.java new file mode 100644 index 0000000000..2b682b3b6b --- /dev/null +++ b/src/main/java/io/github/hectorvent/floci/core/common/AwsCronDefinitions.java @@ -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. + * + *
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); + } +} diff --git a/src/main/java/io/github/hectorvent/floci/services/eventbridge/ScheduleExpressionParser.java b/src/main/java/io/github/hectorvent/floci/services/eventbridge/ScheduleExpressionParser.java index 1f2f121d20..ad787594a0 100644 --- a/src/main/java/io/github/hectorvent/floci/services/eventbridge/ScheduleExpressionParser.java +++ b/src/main/java/io/github/hectorvent/floci/services/eventbridge/ScheduleExpressionParser.java @@ -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() {} diff --git a/src/main/java/io/github/hectorvent/floci/services/scheduler/SchedulerExpressionParser.java b/src/main/java/io/github/hectorvent/floci/services/scheduler/SchedulerExpressionParser.java index df6fc48af5..1c6f990ed3 100644 --- a/src/main/java/io/github/hectorvent/floci/services/scheduler/SchedulerExpressionParser.java +++ b/src/main/java/io/github/hectorvent/floci/services/scheduler/SchedulerExpressionParser.java @@ -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 }