- added GeoportalHealthCheck
This commit is contained in:
parent
3c43a53c06
commit
984c3edf31
|
@ -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<String> 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;
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue