added and used `HealthCheckResponseGeoportalProvider`

This commit is contained in:
Francesco Mangiacrapa 2024-10-16 17:54:29 +02:00
parent 52d456a9b3
commit fc8b551249
2 changed files with 126 additions and 11 deletions

View File

@ -12,11 +12,11 @@ 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.application.geoportal.service.rest.check.HealthCheckResponseGeoportalProvider;
import org.gcube.common.scope.api.ScopeProvider;
import com.mongodb.client.MongoIterable;
@ -55,40 +55,41 @@ public class GeoportalHealthCheck implements HealthCheck {
}
private HealthCheckResponse checkMongo(String context) {
HealthCheckResponseBuilder buildHCRBuilder = HealthCheckResponse.named("mongo");
HealthCheckResponseGeoportalProvider buildHCRProvider = new HealthCheckResponseGeoportalProvider();
buildHCRProvider.setName("mongo");
Mongo mongo = null;
try {
ScopeProvider.instance.set(context);
mongo = ImplementationProvider.get().getProvidedObjectByClass(Mongo.class);
buildHCRBuilder = appendMongoInfo(buildHCRBuilder, mongo.getConnection());
buildHCRBuilder = buildHCRBuilder.up();
buildHCRProvider = appendMongoInfo(buildHCRProvider, mongo.getConnection());
buildHCRProvider.setStatus(Status.UP);
MongoIterable<String> collections = mongo.getTheClient().getDatabase(mongo.getConnection().getDatabase())
.listCollectionNames();
int i = 1;
for (String coll : collections) {
buildHCRBuilder.withData("collection " + i, coll);
buildHCRProvider.withData("collection " + i, coll);
i++;
}
return buildHCRBuilder.build();
return buildHCRProvider.buildHealthCheckResponse();
} catch (Exception e) {
buildHCRBuilder.down();
buildHCRProvider.setStatus(Status.DOWN);
if (mongo != null) {
MongoConnection connection = null;
try {
connection = mongo.getConnection();
buildHCRBuilder = appendMongoInfo(buildHCRBuilder, connection);
buildHCRProvider = appendMongoInfo(buildHCRProvider, connection);
} catch (Exception e1) {
buildHCRBuilder.withData("hosts ", connection.getHosts() + "");
buildHCRProvider.withData("hosts ", connection.getHosts() + "");
}
}
return buildHCRBuilder.build();
return buildHCRProvider.buildHealthCheckResponse();
} finally {
ScopeProvider.instance.reset();
}
}
private HealthCheckResponseBuilder appendMongoInfo(HealthCheckResponseBuilder buildHCRBuilder,
private HealthCheckResponseGeoportalProvider appendMongoInfo(HealthCheckResponseGeoportalProvider buildHCRBuilder,
MongoConnection connection) {
buildHCRBuilder.withData("hosts ", connection.getHosts() + "");
buildHCRBuilder.withData("db_name ", connection.getDatabase());

View File

@ -0,0 +1,114 @@
package org.gcube.application.geoportal.service.rest.check;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import org.eclipse.microprofile.health.HealthCheckResponse;
import org.eclipse.microprofile.health.HealthCheckResponse.Status;
/**
* The Class HealthCheckResponseGeoportalProvider.
*
* @author Francesco Mangiacrapa at ISTI-CNR francesco.mangiacrapa@isti.cnr.it
*
* Oct 16, 2024
*/
public class HealthCheckResponseGeoportalProvider {
private String name;
private Status status;
private Optional<Map<String, Object>> data;
private Map<String, Object> myMap;
/**
* Instantiates a new health check response geoportal provider.
*/
public HealthCheckResponseGeoportalProvider() {
}
/**
* Instantiates a new health check response geoportal provider.
*
* @param name the name
* @param status the status
* @param data the data
*/
public HealthCheckResponseGeoportalProvider(String name, Status status, Optional<Map<String, Object>> data) {
this.name = name;
this.status = status;
this.data = data;
}
/**
* Gets the name.
*
* @return the name
*/
public String getName() {
return name;
}
/**
* Gets the status.
*
* @return the status
*/
public Status getStatus() {
return status;
}
/**
* Gets the data.
*
* @return the data
*/
public Optional<Map<String, Object>> getData() {
return data;
}
/**
* Sets the name.
*
* @param name the new name
*/
public void setName(String name) {
this.name = name;
}
/**
* Sets the status.
*
* @param status the new status
*/
public void setStatus(Status status) {
this.status = status;
}
/**
* With data.
*
* @param key the key
* @param value the value
*/
public void withData(String key, String value) {
if (myMap == null) {
myMap = new HashMap<>();
}
myMap.put(key, value);
}
/**
* Builds the health check response.
*
* @return the health check response
*/
public HealthCheckResponse buildHealthCheckResponse() {
if (myMap != null)
data = Optional.of(myMap);
return new HealthCheckResponse(name, status, data);
}
}