From 984c3edf3174e1e7f97c021b1412baece708e7d0 Mon Sep 17 00:00:00 2001 From: "francesco.mangiacrapa" Date: Wed, 16 Oct 2024 17:02:03 +0200 Subject: [PATCH] - added GeoportalHealthCheck --- .../service/rest/GeoportalHealthCheck.java | 95 +++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 geoportal-service/src/main/java/org/gcube/application/geoportal/service/rest/GeoportalHealthCheck.java diff --git a/geoportal-service/src/main/java/org/gcube/application/geoportal/service/rest/GeoportalHealthCheck.java b/geoportal-service/src/main/java/org/gcube/application/geoportal/service/rest/GeoportalHealthCheck.java new file mode 100644 index 0000000..c8a5aed --- /dev/null +++ b/geoportal-service/src/main/java/org/gcube/application/geoportal/service/rest/GeoportalHealthCheck.java @@ -0,0 +1,95 @@ +package org.gcube.application.geoportal.service.rest; + +import java.util.Optional; + +import javax.ws.rs.GET; +import javax.ws.rs.Path; +import javax.ws.rs.Produces; +import javax.ws.rs.QueryParam; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; + +import org.eclipse.microprofile.health.HealthCheck; +import org.eclipse.microprofile.health.HealthCheckResponse; +import org.eclipse.microprofile.health.HealthCheckResponse.Status; +import org.eclipse.microprofile.health.HealthCheckResponseBuilder; +import org.eclipse.microprofile.health.Liveness; +import org.gcube.application.cms.implementations.ImplementationProvider; +import org.gcube.application.geoportal.common.model.configuration.MongoConnection; +import org.gcube.application.geoportal.service.model.internal.db.Mongo; +import org.gcube.common.scope.api.ScopeProvider; + +import com.mongodb.client.MongoIterable; + +@Path("/health") +public class GeoportalHealthCheck implements HealthCheck { + + @GET + @Path("") + @Produces({ MediaType.TEXT_HTML, MediaType.APPLICATION_JSON }) + public Response check() { + + HealthCheckResponse hcr = call(); + return Response.ok().entity(hcr).build(); + } + + @Liveness + @Override + public HealthCheckResponse call() { + + return new HealthCheckResponse("geooportal-service", Status.UP, Optional.empty()); + } + + @GET + @Path("/mongo") + @Produces({ MediaType.TEXT_HTML, MediaType.APPLICATION_JSON }) + public Response checkDatabase(@QueryParam("context") String context) { + + if (context == null) + return Response.status(400).entity("'context' paramater (e.g. '/gcube/devsec/devVRE' is required").build(); + + return Response.ok().entity(checkMongo(context)).build(); + } + + private HealthCheckResponse checkMongo(String context) { + HealthCheckResponseBuilder buildHCRBuilder = HealthCheckResponse.named("mongo"); + Mongo mongo = null; + try { + ScopeProvider.instance.set(context); + mongo = ImplementationProvider.get().getProvidedObjectByClass(Mongo.class); + buildHCRBuilder = appendMongoInfo(buildHCRBuilder, mongo.getConnection()); + buildHCRBuilder = buildHCRBuilder.up(); + MongoIterable collections = mongo.getTheClient().getDatabase(mongo.getConnection().getDatabase()) + .listCollectionNames(); + int i = 1; + for (String coll : collections) { + buildHCRBuilder.withData("collection " + i, coll); + i++; + } + return buildHCRBuilder.build(); + } catch (Exception e) { + buildHCRBuilder.down(); + if (mongo != null) { + MongoConnection connection = null; + try { + connection = mongo.getConnection(); + buildHCRBuilder = appendMongoInfo(buildHCRBuilder, connection); + } catch (Exception e1) { + buildHCRBuilder.withData("hosts ", connection.getHosts() + ""); + } + + } + return buildHCRBuilder.build(); + } finally { + ScopeProvider.instance.reset(); + } + } + + private HealthCheckResponseBuilder appendMongoInfo(HealthCheckResponseBuilder buildHCRBuilder, + MongoConnection connection) { + buildHCRBuilder.withData("hosts ", connection.getHosts() + ""); + buildHCRBuilder.withData("db_name ", connection.getDatabase()); + return buildHCRBuilder; + } + +}