This is an automated email from the ASF dual-hosted git repository. heneveld pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/brooklyn-server.git
commit 930309ccd1cecd03d9ef2349b28e374d704ce0a8 Author: Alex Heneveld <[email protected]> AuthorDate: Wed Jul 17 14:59:28 2024 +0100 add basic attr when ready options --- .../core/sensor/DependentConfiguration.java | 36 ++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/core/src/main/java/org/apache/brooklyn/core/sensor/DependentConfiguration.java b/core/src/main/java/org/apache/brooklyn/core/sensor/DependentConfiguration.java index eb4ee249fa..eef904bb22 100644 --- a/core/src/main/java/org/apache/brooklyn/core/sensor/DependentConfiguration.java +++ b/core/src/main/java/org/apache/brooklyn/core/sensor/DependentConfiguration.java @@ -148,7 +148,6 @@ public class DependentConfiguration { Builder<T, T> builder = builder().attributeWhenReady(source, sensor); if (ready != null) builder.readiness(ready); return builder.build(); - } /** as {@link #attributeWhenReady(Entity, AttributeSensor, Predicate)} but with no timeout and not aborting if the entity goes on fire. */ @@ -159,6 +158,23 @@ public class DependentConfiguration { } + public static class AttributeWhenReadyOptions { + Duration timeoutIfOnFire; + Duration timeoutIfDown; + Duration timeout; + + public static AttributeWhenReadyOptions defaultOptions() { + AttributeWhenReadyOptions result = new AttributeWhenReadyOptions(); + result.timeoutIfOnFire = Duration.ZERO; + result.timeoutIfDown = Duration.ONE_MINUTE; + return result; + } + } + + public static <T> Task<T> attributeWhenReady(final Entity source, final AttributeSensor<T> sensor, AttributeWhenReadyOptions options) { + return builder().attributeWhenReady(source, sensor).options(options).build(); + } + /** * @deprecated since 0.11.0; explicit groovy utilities/support will be deleted. */ @@ -871,7 +887,7 @@ public class DependentConfiguration { * then it will timeout after 1 minute. */ public <T2> Builder<T2,T2> attributeWhenReady(Entity source, AttributeSensor<T2> sensor) { - return new Builder<T2,T2>(source, sensor).abortIfOnFire().timeoutIfStoppingOrDestroyed(Duration.ONE_MINUTE); + return new Builder<T2,T2>(source, sensor).options(AttributeWhenReadyOptions.defaultOptions()); } /** @@ -961,6 +977,20 @@ public class DependentConfiguration { timeoutIf(source, Attributes.SERVICE_STATE_ACTUAL, Predicates.equalTo(Lifecycle.DESTROYED), time); return this; } + public Builder<T,V> options(AttributeWhenReadyOptions options) { + if (options!=null) { + if (options.timeout!=null) { + timeout(options.timeout); + } + if (options.timeoutIfOnFire!=null) { + if (Duration.ZERO.equals(options.timeoutIfOnFire)) abortIfOnFire(); + else timeoutIf(source, Attributes.SERVICE_STATE_ACTUAL, Predicates.equalTo(Lifecycle.ON_FIRE), options.timeoutIfOnFire); + } + if (options.timeoutIfDown!=null) timeoutIfStoppingOrDestroyed(options.timeoutIfDown); + } + return this; + } + public Builder<T,V> blockingDetails(String val) { blockingDetails = val; return this; @@ -972,6 +1002,8 @@ public class DependentConfiguration { } /** specifies the supplied timeout if the condition is met */ public <T2> Builder<T,V> timeoutIf(Entity source, AttributeSensor<T2> sensor, Predicate<? super T2> predicate, Duration val) { + // TODO these only apply to the target entity's state at initialization time; + // it would be nice to store these and recheck periodically in case state changes subsequently (either from down to up or up to down) if (predicate.apply(source.sensors().get(sensor))) timeout(val); return this; }
