Skip to content

armgit / core

Public Imported from GitHub
0

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

Improve container lifecycle management and cleanup logic (#2996)

Rich Turnerca7e1ef909b6
diff --git a/container/src/main/java/org/openremote/container/Container.java b/container/src/main/java/org/openremote/container/Container.java
index c8edeb516..669a818f6 100644
--- a/container/src/main/java/org/openremote/container/Container.java
+++ b/container/src/main/java/org/openremote/container/Container.java
@@ -75,6 +75,7 @@ public class Container implements org.openremote.model.Container {
protected MeterRegistry meterRegistry;
protected Thread waitingThread;
+ protected volatile boolean running;
protected final Map<Class<? extends ContainerService>, ContainerService> services = new LinkedHashMap<>();
/**
@@ -150,7 +151,7 @@ public class Container implements org.openremote.model.Container {
}
public boolean isRunning() {
- return waitingThread != null;
+ return running;
}
public synchronized void start() throws Exception {
@@ -176,13 +177,19 @@ public class Container implements org.openremote.model.Container {
}
} catch (Exception ex) {
LOG.log(ERROR, ">>> Runtime container startup failed", ex);
+ stop(true);
throw ex;
}
+ running = true;
LOG.log(INFO, ">>> Runtime container startup complete");
}
public synchronized void stop() {
- if (!isRunning())
+ stop(false);
+ }
+
+ private void stop(boolean force) {
+ if (!force && !isRunning())
return;
LOG.log(INFO, "<<< Stopping runtime container...");
@@ -211,20 +218,27 @@ public class Container implements org.openremote.model.Container {
LOG.log(WARNING, "Exception thrown whilst trying to stop executor", e);
}
- Metrics.globalRegistry.remove(meterRegistry);
- PrometheusRegistry.defaultRegistry.clear();
+ if (meterRegistry != null) {
+ Metrics.globalRegistry.remove(meterRegistry);
+ PrometheusRegistry.defaultRegistry.clear();
+ }
meterRegistry = null;
- waitingThread.interrupt();
+ if (waitingThread != null) {
+ waitingThread.interrupt();
+ }
waitingThread = null;
+ running = false;
LOG.log(INFO, "<<< Runtime container stopped");
}
/**
* Starts the container and a non-daemon thread that waits forever.
*/
- public void startBackground() throws Exception {
+ public synchronized void startBackground() throws Exception {
start();
- waitingThread = startWaitingThread();
+ if (waitingThread == null || !waitingThread.isAlive()) {
+ waitingThread = startWaitingThread();
+ }
}
static Thread startWaitingThread() {
diff --git a/test/src/main/groovy/org/openremote/test/ContainerTrait.groovy b/test/src/main/groovy/org/openremote/test/ContainerTrait.groovy
index b3ebdfa1f..bf6b2cd1f 100644
--- a/test/src/main/groovy/org/openremote/test/ContainerTrait.groovy
+++ b/test/src/main/groovy/org/openremote/test/ContainerTrait.groovy
@@ -307,13 +307,11 @@ trait ContainerTrait {
} catch (IllegalStateException e) {
LOG.info("Failed to clean the existing container so creating a new one", e)
stopContainer()
- TestFixture.container = null
}
} else {
LOG.info("Request to start container with different config and/or services as already running container so restarting")
LOG.info("Current config = ${currentConfig}, new config = ${config}")
stopContainer()
- TestFixture.container = null
}
}
@@ -374,7 +372,7 @@ trait ContainerTrait {
if (i >= 100) {
LOG.info("Agents didn't load correctly so stopping and starting the container")
stopContainer()
- startContainer(config, services)
+ return startContainer(config, services)
} else {
LOG.info("Agents are deployed")
}
@@ -519,14 +517,27 @@ trait ContainerTrait {
try {
if (container != null) {
container.stop()
- // Clear out the Built in JAX-RS client as the executor service it uses will be aggressively shutdown by Camel
- WebTargetBuilder.BUILT_IN_CLIENT.getAndUpdate {if (it != null) {
- it.close()
- return null
- }}
}
} catch (Exception e) {
LOG.warn("Failed to stop container", e)
+ } finally {
+ TestFixture.container = null
+ closeTestClient(testClient, "test JAX-RS client")
+ // Clear out the Built in JAX-RS client as the executor service it uses will be aggressively shutdown by Camel
+ closeTestClient(WebTargetBuilder.BUILT_IN_CLIENT, "built in JAX-RS client")
+ }
+ }
+
+ void closeTestClient(AtomicReference<ResteasyClient> clientReference, String clientName) {
+ try {
+ clientReference.getAndUpdate {
+ if (it != null) {
+ it.close()
+ }
+ return null
+ }
+ } catch (Exception e) {
+ LOG.warn("Failed to close ${clientName}", e)
}
}