JAMES-2526 Add a Guice lifecycle health check
Project: http://git-wip-us.apache.org/repos/asf/james-project/repo Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/26557639 Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/26557639 Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/26557639 Branch: refs/heads/master Commit: 265576395a1486ef5dec64f84851513a1286e082 Parents: 1091636 Author: Benoit Tellier <btell...@linagora.com> Authored: Thu Aug 23 09:11:33 2018 +0000 Committer: Antoine Duprat <adup...@linagora.com> Committed: Mon Aug 27 14:18:07 2018 +0200 ---------------------------------------------------------------------- .../apache/james/GuiceLifecycleHealthCheck.java | 49 ++++++++ .../james/modules/IsStartedProbeModule.java | 3 + .../james/GuiceLifecycleHeathCheckTest.java | 118 +++++++++++++++++++ 3 files changed, 170 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/james-project/blob/26557639/server/container/guice/guice-common/src/main/java/org/apache/james/GuiceLifecycleHealthCheck.java ---------------------------------------------------------------------- diff --git a/server/container/guice/guice-common/src/main/java/org/apache/james/GuiceLifecycleHealthCheck.java b/server/container/guice/guice-common/src/main/java/org/apache/james/GuiceLifecycleHealthCheck.java new file mode 100644 index 0000000..b7a6fe4 --- /dev/null +++ b/server/container/guice/guice-common/src/main/java/org/apache/james/GuiceLifecycleHealthCheck.java @@ -0,0 +1,49 @@ +/**************************************************************** + * Licensed to the Apache Software Foundation (ASF) under one * + * or more contributor license agreements. See the NOTICE file * + * distributed with this work for additional information * + * regarding copyright ownership. The ASF licenses this file * + * to you under the Apache License, Version 2.0 (the * + * "License"); you may not use this file except in compliance * + * with the License. You may obtain a copy of the License at * + * * + * http://www.apache.org/licenses/LICENSE-2.0 * + * * + * Unless required by applicable law or agreed to in writing, * + * software distributed under the License is distributed on an * + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * + * KIND, either express or implied. See the License for the * + * specific language governing permissions and limitations * + * under the License. * + ****************************************************************/ + +package org.apache.james; + +import javax.inject.Inject; + +import org.apache.james.core.healthcheck.ComponentName; +import org.apache.james.core.healthcheck.HealthCheck; +import org.apache.james.core.healthcheck.Result; + +public class GuiceLifecycleHealthCheck implements HealthCheck { + private final IsStartedProbe probe; + + @Inject + public GuiceLifecycleHealthCheck(IsStartedProbe probe) { + this.probe = probe; + } + + @Override + public ComponentName componentName() { + return new ComponentName("Guice application lifecycle"); + } + + @Override + public Result check() { + if (probe.isStarted()) { + return Result.healthy(componentName()); + } else { + return Result.unhealthy(componentName(), "James server is not started."); + } + } +} http://git-wip-us.apache.org/repos/asf/james-project/blob/26557639/server/container/guice/guice-common/src/main/java/org/apache/james/modules/IsStartedProbeModule.java ---------------------------------------------------------------------- diff --git a/server/container/guice/guice-common/src/main/java/org/apache/james/modules/IsStartedProbeModule.java b/server/container/guice/guice-common/src/main/java/org/apache/james/modules/IsStartedProbeModule.java index 6fd539b..fe3eef5 100644 --- a/server/container/guice/guice-common/src/main/java/org/apache/james/modules/IsStartedProbeModule.java +++ b/server/container/guice/guice-common/src/main/java/org/apache/james/modules/IsStartedProbeModule.java @@ -19,7 +19,9 @@ package org.apache.james.modules; +import org.apache.james.GuiceLifecycleHealthCheck; import org.apache.james.IsStartedProbe; +import org.apache.james.core.healthcheck.HealthCheck; import org.apache.james.utils.GuiceProbe; import com.google.inject.AbstractModule; @@ -37,5 +39,6 @@ public class IsStartedProbeModule extends AbstractModule { bind(IsStartedProbe.class).toInstance(probe); Multibinder.newSetBinder(binder(), GuiceProbe.class).addBinding().toInstance(probe); + Multibinder.newSetBinder(binder(), HealthCheck.class).addBinding().to(GuiceLifecycleHealthCheck.class); } } http://git-wip-us.apache.org/repos/asf/james-project/blob/26557639/server/container/guice/memory-guice/src/test/java/org/apache/james/GuiceLifecycleHeathCheckTest.java ---------------------------------------------------------------------- diff --git a/server/container/guice/memory-guice/src/test/java/org/apache/james/GuiceLifecycleHeathCheckTest.java b/server/container/guice/memory-guice/src/test/java/org/apache/james/GuiceLifecycleHeathCheckTest.java new file mode 100644 index 0000000..a0ea030 --- /dev/null +++ b/server/container/guice/memory-guice/src/test/java/org/apache/james/GuiceLifecycleHeathCheckTest.java @@ -0,0 +1,118 @@ +/**************************************************************** + * Licensed to the Apache Software Foundation (ASF) under one * + * or more contributor license agreements. See the NOTICE file * + * distributed with this work for additional information * + * regarding copyright ownership. The ASF licenses this file * + * to you under the Apache License, Version 2.0 (the * + * "License"); you may not use this file except in compliance * + * with the License. You may obtain a copy of the License at * + * * + * http://www.apache.org/licenses/LICENSE-2.0 * + * * + * Unless required by applicable law or agreed to in writing, * + * software distributed under the License is distributed on an * + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * + * KIND, either express or implied. See the License for the * + * specific language governing permissions and limitations * + * under the License. * + ****************************************************************/ + +package org.apache.james; + +import static io.restassured.RestAssured.when; +import static io.restassured.config.EncoderConfig.encoderConfig; +import static io.restassured.config.RestAssuredConfig.newConfig; + +import java.nio.charset.StandardCharsets; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.CountDownLatch; + +import org.apache.james.task.Task; +import org.apache.james.utils.WebAdminGuiceProbe; +import org.apache.james.webadmin.WebAdminConfiguration; +import org.eclipse.jetty.http.HttpStatus; +import org.junit.After; +import org.junit.Rule; +import org.junit.Test; + +import com.google.inject.multibindings.Multibinder; + +import io.restassured.RestAssured; +import io.restassured.builder.RequestSpecBuilder; +import io.restassured.http.ContentType; + +public class GuiceLifecycleHeathCheckTest { + private void configureRequestSpecification() { + WebAdminGuiceProbe webAdminGuiceProbe = guiceJamesServer.getProbe(WebAdminGuiceProbe.class); + + RestAssured.requestSpecification = new RequestSpecBuilder() + .setContentType(ContentType.JSON) + .setAccept(ContentType.JSON) + .setConfig(newConfig().encoderConfig(encoderConfig().defaultContentCharset(StandardCharsets.UTF_8))) + .setPort(webAdminGuiceProbe.getWebAdminPort().getValue()) + .build(); + } + + @Rule + public MemoryJmapTestRule memoryJmap = new MemoryJmapTestRule(); + private GuiceJamesServer guiceJamesServer; + + @After + public void cleanUp() { + if (guiceJamesServer != null) { + guiceJamesServer.stop(); + } + } + + @Test + public void startedJamesServerShouldBeHealthy() throws Exception { + guiceJamesServer = memoryJmap.jmapServer() + .overrideWith(binder -> binder.bind(WebAdminConfiguration.class) + .toInstance(WebAdminConfiguration.TEST_CONFIGURATION)); + + + guiceJamesServer.start(); + configureRequestSpecification(); + + when() + .get("/healthcheck") + .then() + .statusCode(HttpStatus.OK_200); + } + + @Test + public void stoppingJamesServerShouldBeUnhealthy() throws Exception { + CountDownLatch latch = new CountDownLatch(1); + CleanupTasksPerformer.CleanupTask awaitCleanupTask = () -> { + try { + latch.await(); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + return Task.Result.COMPLETED; + }; + CompletableFuture<Void> stopCompletedFuture = CompletableFuture.completedFuture(null); + + try { + guiceJamesServer = memoryJmap.jmapServer( + binder -> Multibinder.newSetBinder(binder, CleanupTasksPerformer.CleanupTask.class) + .addBinding() + .toInstance(awaitCleanupTask)) + .overrideWith(binder -> binder.bind(WebAdminConfiguration.class) + .toInstance(WebAdminConfiguration.TEST_CONFIGURATION)); + + guiceJamesServer.start(); + configureRequestSpecification(); + + stopCompletedFuture = CompletableFuture.runAsync(() -> guiceJamesServer.stop()); + + when() + .get("/healthcheck") + .then() + .statusCode(HttpStatus.INTERNAL_SERVER_ERROR_500); + } finally { + latch.countDown(); + stopCompletedFuture.join(); + } + } +} --------------------------------------------------------------------- To unsubscribe, e-mail: server-dev-unsubscr...@james.apache.org For additional commands, e-mail: server-dev-h...@james.apache.org