Skip to content

armgit / core

Public Imported from GitHub
0

100% open-source IoT Platform - Integrate your devices, add intelligence, build custom apps

Bug fix for RulesEngine OR_RULE_EVENT_EXPIRES parsing to prevent startup failure

Rich Turner9fbfd2a4fb51
diff --git a/manager/Dockerfile b/manager/Dockerfile
index 6816679ec..b5d6c47de 100644
--- a/manager/Dockerfile
+++ b/manager/Dockerfile
@@ -51,7 +51,7 @@ ENV OR_MAP_TILESERVER_HOST ${OR_MAP_TILESERVER_HOST}
ENV OR_MAP_TILESERVER_PORT ${OR_MAP_TILESERVER_PORT:-8082}
ENV OR_MAP_TILESERVER_REQUEST_TIMEOUT ${OR_MAP_TILESERVER_REQUEST_TIMEOUT:-10000}
ENV OR_SCHEDULED_TASKS_THREADS_MAX ${OR_SCHEDULED_TASKS_THREADS_MAX:-4}
-ENV OR_RULE_EVENT_EXPIRES ${OR_RULE_EVENT_EXPIRES:-1h}
+ENV OR_RULE_EVENT_EXPIRES ${OR_RULE_EVENT_EXPIRES:-PT1H}
ENV OR_IDENTITY_PROVIDER ${OR_IDENTITY_PROVIDER:-keycloak}
ENV OR_IDENTITY_SESSION_MAX_MINUTES ${OR_IDENTITY_SESSION_MAX_MINUTES:-1440}
ENV OR_IDENTITY_SESSION_OFFLINE_TIMEOUT_MINUTES ${OR_IDENTITY_SESSION_OFFLINE_TIMEOUT_MINUTES:-2628000}
diff --git a/manager/src/main/java/org/openremote/manager/rules/RulesEngine.java b/manager/src/main/java/org/openremote/manager/rules/RulesEngine.java
index 1853fcc6c..483c6657b 100644
--- a/manager/src/main/java/org/openremote/manager/rules/RulesEngine.java
+++ b/manager/src/main/java/org/openremote/manager/rules/RulesEngine.java
@@ -37,6 +37,7 @@ import org.openremote.model.query.filter.LocationAttributePredicate;
import org.openremote.model.rules.*;
import org.openremote.model.syslog.SyslogCategory;
import org.openremote.model.util.TextUtil;
+import org.openremote.model.util.TimeUtil;
import java.util.*;
import java.util.concurrent.ScheduledExecutorService;
@@ -560,8 +561,8 @@ public class RulesEngine<T extends Ruleset> {
}
}
- public void insertAssetEvent(String expires, AssetState<?> assetState) {
- facts.insertAssetEvent(expires, assetState);
+ public void insertAssetEvent(long expiresMillis, AssetState<?> assetState) {
+ facts.insertAssetEvent(expiresMillis, assetState);
if (running) {
scheduleFire();
}
diff --git a/manager/src/main/java/org/openremote/manager/rules/RulesFacts.java b/manager/src/main/java/org/openremote/manager/rules/RulesFacts.java
index 3ad02769c..58b0dd13b 100644
--- a/manager/src/main/java/org/openremote/manager/rules/RulesFacts.java
+++ b/manager/src/main/java/org/openremote/manager/rules/RulesFacts.java
@@ -247,10 +247,6 @@ public class RulesFacts extends Facts implements RuleListener {
return this;
}
- public RulesFacts insertAssetEvent(String expires, AssetState<?> assetState) {
- return insertAssetEvent(TimeUtil.parseTimeDuration(expires), assetState);
- }
-
public RulesFacts insertAssetEvent(long expiresMilliSeconds, AssetState<?> assetState) {
TemporaryFact<AssetState<?>> fact = new TemporaryFact<>(assetState.getTimestamp(), expiresMilliSeconds, assetState);
if (LOG.isLoggable(Level.FINEST)) {
diff --git a/manager/src/main/java/org/openremote/manager/rules/RulesService.java b/manager/src/main/java/org/openremote/manager/rules/RulesService.java
index 33de6d151..db43e52b7 100644
--- a/manager/src/main/java/org/openremote/manager/rules/RulesService.java
+++ b/manager/src/main/java/org/openremote/manager/rules/RulesService.java
@@ -51,6 +51,8 @@ import org.openremote.model.rules.geofence.GeofenceDefinition;
import org.openremote.model.security.ClientRole;
import org.openremote.model.security.Tenant;
import org.openremote.model.util.Pair;
+import org.openremote.model.util.TextUtil;
+import org.openremote.model.util.TimeUtil;
import org.openremote.model.util.ValueUtil;
import org.openremote.model.value.MetaItemType;
@@ -58,12 +60,12 @@ import javax.persistence.EntityManager;
import java.util.*;
import java.util.concurrent.ScheduledExecutorService;
import java.util.function.BiFunction;
+import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;
import java.util.stream.Stream;
-import static java.util.logging.Level.FINEST;
-import static java.util.logging.Level.SEVERE;
+import static java.util.logging.Level.*;
import static java.util.stream.Collectors.toList;
import static org.openremote.container.concurrent.GlobalLock.withLock;
import static org.openremote.container.concurrent.GlobalLock.withLockReturning;
@@ -85,7 +87,7 @@ import static org.openremote.model.attribute.Attribute.getAddedOrModifiedAttribu
* If an updated attribute's {@link MetaItemType#RULE_EVENT} is true, another temporary {@link AssetState} fact is
* inserted in the rules engines in scope. This fact expires automatically if the lifetime set in {@link
* RulesService#OR_RULE_EVENT_EXPIRES} is reached, or if the lifetime set in the attribute {@link
- * MetaItemType#OR_RULE_EVENT_EXPIRES} is reached.
+ * MetaItemType#RULE_EVENT_EXPIRES} is reached.
* <p>
* Each asset attribute update is processed in the following order:
* <ol>
@@ -126,7 +128,7 @@ public class RulesService extends RouteBuilder implements ContainerService, Asse
// here means we can quickly insert facts into newly started engines
protected Set<AssetState<?>> assetStates = new HashSet<>();
protected Set<AssetState<?>> preInitassetStates = new HashSet<>();
- protected String configEventExpires;
+ protected long defaultEventExpiresMillis = 1000*60*60;
protected boolean initDone;
protected boolean startDone;
@@ -187,7 +189,15 @@ public class RulesService extends RouteBuilder implements ContainerService, Asse
geofenceAssetAdapters.addAll(container.getServices(GeofenceAssetAdapter.class));
geofenceAssetAdapters.sort(Comparator.comparingInt(GeofenceAssetAdapter::getPriority));
container.getService(MessageBrokerService.class).getContext().addRoutes(this);
- configEventExpires = getString(container.getConfig(), OR_RULE_EVENT_EXPIRES, OR_RULE_EVENT_EXPIRES_DEFAULT);
+ String defaultEventExpires = getString(container.getConfig(), OR_RULE_EVENT_EXPIRES, OR_RULE_EVENT_EXPIRES_DEFAULT);
+
+ if (!TextUtil.isNullOrEmpty(defaultEventExpires)) {
+ try {
+ defaultEventExpiresMillis = TimeUtil.parseTimeDuration(defaultEventExpires);
+ } catch (RuntimeException exception) {
+ LOG.log(Level.WARNING, "Failed to parse " + OR_RULE_EVENT_EXPIRES, exception);
+ }
+ }
container.getService(ManagerWebService.class).addApiSingleton(
new FlowResourceImpl(
@@ -363,10 +373,19 @@ public class RulesService extends RouteBuilder implements ContainerService, Asse
// Then as asset event (if there wasn't an error), this will also fire the rules engines
if (assetState.getMetaValue(MetaItemType.RULE_EVENT).orElse(false)) {
- insertAssetEvent(
- assetState,
- assetState.getMetaValue(MetaItemType.OR_RULE_EVENT_EXPIRES).orElse(configEventExpires)
- );
+
+ long expireMillis = assetState.getMetaValue(MetaItemType.RULE_EVENT_EXPIRES).map(expires -> {
+ long expMillis = defaultEventExpiresMillis;
+
+ try {
+ expMillis = TimeUtil.parseTimeDuration(expires);
+ } catch (RuntimeException exception) {
+ LOG.log(Level.WARNING, "Failed to parse '" + MetaItemType.RULE_EVENT_EXPIRES.getName() + "' value '" + expires + "' for attribute: " + assetState, exception);
+ }
+ return expMillis;
+ }).orElse(defaultEventExpiresMillis);
+
+ insertAssetEvent(assetState, expireMillis);
}
}
@@ -772,7 +791,7 @@ public class RulesService extends RouteBuilder implements ContainerService, Asse
});
}
- protected void insertAssetEvent(AssetState<?> assetState, String expires) {
+ protected void insertAssetEvent(AssetState<?> assetState, long expiresMillis) {
withLock(getClass().getSimpleName() + "::insertAssetEvent", () -> {
// Get the chain of rule engines that we need to pass through
List<RulesEngine<?>> rulesEngines = getEnginesInScope(assetState.getRealm(), assetState.getPath());
@@ -792,7 +811,7 @@ public class RulesService extends RouteBuilder implements ContainerService, Asse
// Pass through each engine
for (RulesEngine<?> rulesEngine : rulesEngines) {
- rulesEngine.insertAssetEvent(expires, assetState);
+ rulesEngine.insertAssetEvent(expiresMillis, assetState);
}
});
}
diff --git a/manager/src/main/java/org/openremote/manager/setup/ManagerSetup.java b/manager/src/main/java/org/openremote/manager/setup/ManagerSetup.java
index 3c6633f0d..6328f405b 100644
--- a/manager/src/main/java/org/openremote/manager/setup/ManagerSetup.java
+++ b/manager/src/main/java/org/openremote/manager/setup/ManagerSetup.java
@@ -170,7 +170,7 @@ public class ManagerSetup implements Setup {
room.getAttributes().addOrReplace(new Attribute<>("co2Level", POSITIVE_INTEGER).addMeta(
new MetaItem<>(UNITS, Constants.units(UNITS_PART_PER_MILLION)), new MetaItem<>(LABEL, "CO2 level"),
new MetaItem<>(RULE_STATE, true), new MetaItem<>(RULE_EVENT, true),
- new MetaItem<>(OR_RULE_EVENT_EXPIRES, "PT45M"), new MetaItem<>(ACCESS_RESTRICTED_READ, true),
+ new MetaItem<>(RULE_EVENT_EXPIRES, "PT45M"), new MetaItem<>(ACCESS_RESTRICTED_READ, true),
new MetaItem<>(READ_ONLY, true), new MetaItem<>(STORE_DATA_POINTS)));
if (shouldBeLinked) {
@@ -184,7 +184,7 @@ public class ManagerSetup implements Setup {
room.getAttributes()
.addOrReplace(new Attribute<>("humidity", POSITIVE_INTEGER).addMeta(new MetaItem<>(LABEL, "Humidity"),
new MetaItem<>(RULE_STATE, true), new MetaItem<>(RULE_EVENT, true),
- new MetaItem<>(OR_RULE_EVENT_EXPIRES, "PT45M"), new MetaItem<>(ACCESS_RESTRICTED_READ, true),
+ new MetaItem<>(RULE_EVENT_EXPIRES, "PT45M"), new MetaItem<>(ACCESS_RESTRICTED_READ, true),
new MetaItem<>(READ_ONLY, true), new MetaItem<>(UNITS, Constants.units(UNITS_PERCENTAGE)),
new MetaItem<>(STORE_DATA_POINTS)));
@@ -239,7 +239,7 @@ public class ManagerSetup implements Setup {
new MetaItem<>(LABEL, "Smart Switch mode " + switchName),
new MetaItem<>(ACCESS_RESTRICTED_READ, true), new MetaItem<>(ACCESS_RESTRICTED_WRITE, true),
new MetaItem<>(RULE_STATE, true), new MetaItem<>(RULE_EVENT, true),
- new MetaItem<>(OR_RULE_EVENT_EXPIRES, "PT48H")),
+ new MetaItem<>(RULE_EVENT_EXPIRES, "PT48H")),
// Time
new Attribute<>("smartSwitchBeginEnd" + switchName, TIMESTAMP).addMeta(
new MetaItem<>(LABEL, "Smart Switch begin/end cycle " + switchName),
diff --git a/model/src/main/java/org/openremote/model/value/MetaItemType.java b/model/src/main/java/org/openremote/model/value/MetaItemType.java
index da2bbce84..9b6bdcdf7 100644
--- a/model/src/main/java/org/openremote/model/value/MetaItemType.java
+++ b/model/src/main/java/org/openremote/model/value/MetaItemType.java
@@ -23,7 +23,6 @@ import org.openremote.model.Constants;
import org.openremote.model.asset.UserAssetLink;
import org.openremote.model.asset.agent.AgentLink;
import org.openremote.model.attribute.Attribute;
-import org.openremote.model.attribute.AttributeEvent;
import org.openremote.model.attribute.AttributeLink;
import org.openremote.model.rules.AssetState;
import org.openremote.model.rules.TemporaryFact;
@@ -110,17 +109,17 @@ public final class MetaItemType {
* timestamp, not event processing time).
* <p>
* The default expiration for asset events can be configured with environment variable
- * <code>OR_RULE_EVENT_EXPIRES</code>.
+ * <code>RULE_EVENT_EXPIRES</code>.
* <p>
* Also see {@link TemporaryFact#GUARANTEED_MIN_EXPIRATION_MILLIS}.
*/
@Pattern(regexp = Constants.ISO8601_DURATION_REGEXP)
- public static final MetaItemDescriptor<String> OR_RULE_EVENT_EXPIRES = new MetaItemDescriptor<>("ruleEventExpires", ValueType.TEXT);
+ public static final MetaItemDescriptor<String> RULE_EVENT_EXPIRES = new MetaItemDescriptor<>("ruleEventExpires", ValueType.TEXT);
/**
* Should attribute writes be processed by the rules engines as temporary facts. When an attribute is updated, the
* change will be inserted as a new {@link AssetState} temporary fact in rules engines. These facts expire
- * automatically after a defined time, see {@link #OR_RULE_EVENT_EXPIRES}. If you want to match (multiple) {@link
+ * automatically after a defined time, see {@link #RULE_EVENT_EXPIRES}. If you want to match (multiple) {@link
* AssetState}s for the same attribute over time, to evaluate the change history of an attribute, add this meta
* item.
*/
diff --git a/profile/deploy.yml b/profile/deploy.yml
index 033ea7fec..3333e483a 100644
--- a/profile/deploy.yml
+++ b/profile/deploy.yml
@@ -264,7 +264,7 @@ services:
# When asset attributes are enabled for rule events, each value update will be a temporary
# fact in rules. You can set the global default expiration time span used when no other
# attribute-level configuration exists. Default is 1 hour.
- OR_RULE_EVENT_EXPIRES: ${OR_RULE_EVENT_EXPIRES:-1h}
+ OR_RULE_EVENT_EXPIRES: ${OR_RULE_EVENT_EXPIRES:-PT1H}
# The identity provider implementation to use for managing user identities, roles, and
# access authorization. The default is 'keycloak' which requires a separate service but
diff --git a/test/src/test/groovy/org/openremote/test/notification/NotificationTest.groovy b/test/src/test/groovy/org/openremote/test/notification/NotificationTest.groovy
index 5d6106c66..ce4939c86 100644
--- a/test/src/test/groovy/org/openremote/test/notification/NotificationTest.groovy
+++ b/test/src/test/groovy/org/openremote/test/notification/NotificationTest.groovy
@@ -553,7 +553,7 @@ class NotificationTest extends Specification implements ManagerContainerTrait {
given: "the container environment is started with the mock handler"
def conditions = new PollingConditions(timeout: 10, delay: 0.2)
- def container = startContainer(defaultConfig() << [(OR_EMAIL_X_HEADERS): "Test 1=Hello World 1\nTest2=Hello World 2"], defaultServices())
+ def container = startContainer(defaultConfig() << [(OR_EMAIL_X_HEADERS): "Test 1: Hello World 1\nTest2: Hello World 2"], defaultServices())
def managerTestSetup = container.getService(SetupService.class).getTaskOfType(ManagerTestSetup.class)
def keycloakTestSetup = container.getService(SetupService.class).getTaskOfType(KeycloakTestSetup.class)
def notificationService = container.getService(NotificationService.class)
diff --git a/test/src/test/java/org/openremote/test/setup/ManagerTestSetup.java b/test/src/test/java/org/openremote/test/setup/ManagerTestSetup.java
index 6b80ec823..4315c5f8b 100644
--- a/test/src/test/java/org/openremote/test/setup/ManagerTestSetup.java
+++ b/test/src/test/java/org/openremote/test/setup/ManagerTestSetup.java
@@ -448,7 +448,7 @@ public class ManagerTestSetup extends ManagerSetup {
.addMeta(
new MetaItem<>(LABEL, "All Lights Off Switch"),
new MetaItem<>(RULE_EVENT, true),
- new MetaItem<>(OR_RULE_EVENT_EXPIRES, "PT3S")
+ new MetaItem<>(RULE_EVENT_EXPIRES, "PT3S")
)
);
apartment2 = assetStorageService.merge(apartment2);