From 01b5441d1604517b103dffd647249d19e080f2bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Mathieu?= Date: Tue, 16 Dec 2025 11:14:17 +0100 Subject: [PATCH] feat(trigger): refactor Schedule to not use the application context Part-of: https://github.com/kestra-io/kestra-ee/issues/4228 --- .../core/services/ConditionService.java | 11 --------- .../kestra/plugin/core/trigger/Schedule.java | 23 +++++++++++-------- 2 files changed, 14 insertions(+), 20 deletions(-) diff --git a/core/src/main/java/io/kestra/core/services/ConditionService.java b/core/src/main/java/io/kestra/core/services/ConditionService.java index ad1b453e85..5e2f4e7b6a 100644 --- a/core/src/main/java/io/kestra/core/services/ConditionService.java +++ b/core/src/main/java/io/kestra/core/services/ConditionService.java @@ -4,7 +4,6 @@ import com.cronutils.utils.VisibleForTesting; import io.kestra.core.exceptions.InternalException; import io.kestra.core.models.conditions.Condition; import io.kestra.core.models.conditions.ConditionContext; -import io.kestra.core.models.conditions.ScheduleCondition; import io.kestra.core.models.executions.Execution; import io.kestra.core.models.flows.Flow; import io.kestra.core.models.flows.FlowInterface; @@ -65,16 +64,6 @@ public class ConditionService { return this.valid(flow, conditions, conditionContext); } - /** - * Check that all conditions are valid. - * Warning, this method throws if a condition cannot be evaluated. - */ - public boolean isValid(List conditions, ConditionContext conditionContext) throws InternalException { - return conditions - .stream() - .allMatch(throwPredicate(condition -> condition.test(conditionContext))); - } - /** * Check that all conditions are valid. * Warning, this method throws if a condition cannot be evaluated. diff --git a/core/src/main/java/io/kestra/plugin/core/trigger/Schedule.java b/core/src/main/java/io/kestra/plugin/core/trigger/Schedule.java index f35087ead2..e878e9b1ed 100644 --- a/core/src/main/java/io/kestra/plugin/core/trigger/Schedule.java +++ b/core/src/main/java/io/kestra/plugin/core/trigger/Schedule.java @@ -18,9 +18,7 @@ import io.kestra.core.models.conditions.ScheduleCondition; import io.kestra.core.models.executions.Execution; import io.kestra.core.models.flows.State; import io.kestra.core.models.triggers.*; -import io.kestra.core.runners.DefaultRunContext; import io.kestra.core.runners.RunContext; -import io.kestra.core.services.ConditionService; import io.kestra.core.services.LabelService; import io.kestra.core.utils.ListUtils; import io.kestra.core.validations.ScheduleValidation; @@ -40,6 +38,8 @@ import java.time.temporal.ChronoUnit; import java.util.*; import java.util.stream.Stream; +import static io.kestra.core.utils.Rethrow.throwPredicate; + @Slf4j @SuperBuilder @ToString @@ -549,9 +549,9 @@ public class Schedule extends AbstractTrigger implements Schedulable, TriggerOut Optional truePreviousNextDateWithCondition(ExecutionTime executionTime, ConditionContext conditionContext, ZonedDateTime toTestDate, boolean next) throws InternalException { int upperYearBound = ZonedDateTime.now().getYear() + 10; int lowerYearBound = ZonedDateTime.now().getYear() - 10; - + while ((next && toTestDate.getYear() < upperYearBound) || (!next && toTestDate.getYear() > lowerYearBound)) { - + Optional currentDate = next ? executionTime.nextExecution(toTestDate) : executionTime.lastExecution(toTestDate); @@ -607,16 +607,21 @@ public class Schedule extends AbstractTrigger implements Schedulable, TriggerOut private boolean validateScheduleCondition(ConditionContext conditionContext) throws InternalException { if (conditions != null) { - ConditionService conditionService = ((DefaultRunContext)conditionContext.getRunContext()).getApplicationContext().getBean(ConditionService.class); - return conditionService.isValid( - conditions.stream().filter(c -> c instanceof ScheduleCondition).map(c -> (ScheduleCondition) c).toList(), - conditionContext - ); + return conditions.stream() + .filter(c -> c instanceof ScheduleCondition) + .map(c -> (ScheduleCondition) c) + .allMatch(throwPredicate(condition -> condition.test(conditionContext))); } return true; } + private boolean isValid(List conditions, ConditionContext conditionContext) throws InternalException { + return conditions + .stream() + .allMatch(throwPredicate(condition -> condition.test(conditionContext))); + } + @SuperBuilder @ToString @EqualsAndHashCode