This is an automated email from the ASF dual-hosted git repository.

rcordier pushed a commit to branch postgresql
in repository https://gitbox.apache.org/repos/asf/james-project.git

commit 9132406cb6c74501f3e9313b08c6cab0280ce1db
Author: Benoit TELLIER <[email protected]>
AuthorDate: Tue Dec 12 11:53:37 2023 +0100

    JAMES-2586 Implement and bind PostgresHealthCheck
---
 .../postgres/utils/PostgresHealthCheck.java        | 55 ++++++++++++++++++
 .../james/backends/postgres/PostgresExtension.java | 12 ++++
 .../postgres/utils/PostgresHealthCheckTest.java    | 66 ++++++++++++++++++++++
 .../james/modules/data/PostgresCommonModule.java   |  5 ++
 4 files changed, 138 insertions(+)

diff --git 
a/backends-common/postgres/src/main/java/org/apache/james/backends/postgres/utils/PostgresHealthCheck.java
 
b/backends-common/postgres/src/main/java/org/apache/james/backends/postgres/utils/PostgresHealthCheck.java
new file mode 100644
index 0000000000..40262bd88e
--- /dev/null
+++ 
b/backends-common/postgres/src/main/java/org/apache/james/backends/postgres/utils/PostgresHealthCheck.java
@@ -0,0 +1,55 @@
+/****************************************************************
+ * 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.backends.postgres.utils;
+
+import java.time.Duration;
+
+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;
+import org.jooq.impl.DSL;
+import org.reactivestreams.Publisher;
+
+import reactor.core.publisher.Mono;
+
+public class PostgresHealthCheck implements HealthCheck {
+    public static final ComponentName COMPONENT_NAME = new 
ComponentName("Postgres");
+    private final PostgresExecutor postgresExecutor;
+
+    @Inject
+    public PostgresHealthCheck(PostgresExecutor postgresExecutor) {
+        this.postgresExecutor = postgresExecutor;
+    }
+
+    @Override
+    public ComponentName componentName() {
+        return COMPONENT_NAME;
+    }
+
+    @Override
+    public Publisher<Result> check() {
+        return postgresExecutor.executeRow(context -> 
Mono.from(context.select(DSL.now())))
+            .timeout(Duration.ofSeconds(5))
+            .map(any -> Result.healthy(COMPONENT_NAME))
+            .onErrorResume(e -> Mono.just(Result.unhealthy(COMPONENT_NAME, 
"Failed to execute request against postgres", e)));
+    }
+}
diff --git 
a/backends-common/postgres/src/test/java/org/apache/james/backends/postgres/PostgresExtension.java
 
b/backends-common/postgres/src/test/java/org/apache/james/backends/postgres/PostgresExtension.java
index 126cc722b1..e332b9474d 100644
--- 
a/backends-common/postgres/src/test/java/org/apache/james/backends/postgres/PostgresExtension.java
+++ 
b/backends-common/postgres/src/test/java/org/apache/james/backends/postgres/PostgresExtension.java
@@ -35,6 +35,8 @@ import 
org.apache.james.backends.postgres.utils.SinglePostgresConnectionFactory;
 import org.junit.jupiter.api.extension.ExtensionContext;
 import org.testcontainers.containers.PostgreSQLContainer;
 
+import com.github.dockerjava.api.command.PauseContainerCmd;
+import com.github.dockerjava.api.command.UnpauseContainerCmd;
 import com.github.fge.lambdas.Throwing;
 import com.google.inject.Module;
 import com.google.inject.util.Modules;
@@ -69,6 +71,16 @@ public class PostgresExtension implements 
GuiceModuleTestExtension {
     private PostgresqlConnectionFactory connectionFactory;
     private PostgresExecutor.Factory executorFactory;
 
+    public void pause() {
+        
PG_CONTAINER.getDockerClient().pauseContainerCmd(PG_CONTAINER.getContainerId())
+            .exec();
+    }
+
+    public void unpause() {
+        
PG_CONTAINER.getDockerClient().unpauseContainerCmd(PG_CONTAINER.getContainerId())
+            .exec();
+    }
+
     private PostgresExtension(PostgresModule postgresModule, boolean 
rlsEnabled) {
         this.postgresModule = postgresModule;
         this.rlsEnabled = rlsEnabled;
diff --git 
a/backends-common/postgres/src/test/java/org/apache/james/backends/postgres/utils/PostgresHealthCheckTest.java
 
b/backends-common/postgres/src/test/java/org/apache/james/backends/postgres/utils/PostgresHealthCheckTest.java
new file mode 100644
index 0000000000..e380920b5f
--- /dev/null
+++ 
b/backends-common/postgres/src/test/java/org/apache/james/backends/postgres/utils/PostgresHealthCheckTest.java
@@ -0,0 +1,66 @@
+/****************************************************************
+ * 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.backends.postgres.utils;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+import org.apache.james.backends.postgres.PostgresExtension;
+import org.apache.james.backends.postgres.quota.PostgresQuotaLimitDAO;
+import org.apache.james.backends.postgres.quota.PostgresQuotaModule;
+import org.apache.james.core.healthcheck.Result;
+import org.apache.james.core.healthcheck.ResultStatus;
+import org.apache.james.core.quota.QuotaComponent;
+import org.apache.james.core.quota.QuotaLimit;
+import org.apache.james.core.quota.QuotaScope;
+import org.apache.james.core.quota.QuotaType;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.RegisterExtension;
+
+import reactor.core.publisher.Mono;
+
+public class PostgresHealthCheckTest {
+    private PostgresHealthCheck testee;
+
+    @RegisterExtension
+    static PostgresExtension postgresExtension = 
PostgresExtension.withoutRowLevelSecurity(PostgresQuotaModule.MODULE);
+
+    @BeforeEach
+    void setup() {
+        testee = new 
PostgresHealthCheck(postgresExtension.getPostgresExecutor());
+    }
+
+    @Test
+    void shouldBeHealthy() {
+        Result result = Mono.from(testee.check()).block();
+        assertThat(result.getStatus()).isEqualTo(ResultStatus.HEALTHY);
+    }
+
+    @Test
+    void shouldBeUnhealthyWhenPaused() {
+        try {
+            postgresExtension.pause();
+            Result result = Mono.from(testee.check()).block();
+            assertThat(result.getStatus()).isEqualTo(ResultStatus.UNHEALTHY);
+        } finally {
+            postgresExtension.unpause();
+        }
+    }
+}
\ No newline at end of file
diff --git 
a/server/container/guice/postgres-common/src/main/java/org/apache/james/modules/data/PostgresCommonModule.java
 
b/server/container/guice/postgres-common/src/main/java/org/apache/james/modules/data/PostgresCommonModule.java
index 82366095ae..53e98144d1 100644
--- 
a/server/container/guice/postgres-common/src/main/java/org/apache/james/modules/data/PostgresCommonModule.java
+++ 
b/server/container/guice/postgres-common/src/main/java/org/apache/james/modules/data/PostgresCommonModule.java
@@ -30,7 +30,9 @@ import 
org.apache.james.backends.postgres.PostgresTableManager;
 import 
org.apache.james.backends.postgres.utils.DomainImplPostgresConnectionFactory;
 import org.apache.james.backends.postgres.utils.JamesPostgresConnectionFactory;
 import org.apache.james.backends.postgres.utils.PostgresExecutor;
+import org.apache.james.backends.postgres.utils.PostgresHealthCheck;
 import 
org.apache.james.backends.postgres.utils.SinglePostgresConnectionFactory;
+import org.apache.james.core.healthcheck.HealthCheck;
 import org.apache.james.utils.PropertiesProvider;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -56,6 +58,9 @@ public class PostgresCommonModule extends AbstractModule {
         bind(PostgresExecutor.Factory.class).in(Scopes.SINGLETON);
 
         bind(PostgresExecutor.class).toProvider(PostgresTableManager.class);
+
+        Multibinder.newSetBinder(binder(), HealthCheck.class)
+            .addBinding().to(PostgresHealthCheck.class);
     }
 
     @Provides


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to