Skip to content

armgit / floci

Public Imported from GitHub
0

Light, fluffy, and always free - The AWS Local Emulator alternative

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

Priamo Germosen7fee554381c1
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.
+ *
+ * <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);
+ }
+}
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 }