Skip to content

Commit

Permalink
Merge pull request #9 from chinmoy12c/enhancement/async-health-endpoints
Browse files Browse the repository at this point in the history
Modified health checks to work asynchronously.
  • Loading branch information
pankajjangid05 authored Apr 12, 2023
2 parents d160e09 + a84bfec commit c02638a
Showing 1 changed file with 46 additions and 81 deletions.
127 changes: 46 additions & 81 deletions src/main/java/com/uci/dao/service/HealthService.java
Original file line number Diff line number Diff line change
@@ -1,107 +1,72 @@
package com.uci.dao.service;

import java.io.IOException;
import java.util.Arrays;
import java.util.function.Function;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.uci.dao.repository.XMessageRepository;
import com.uci.utils.UtilHealthService;

import reactor.core.publisher.Mono;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class HealthService extends UtilHealthService{
public class HealthService extends UtilHealthService {
@Autowired
private XMessageRepository xMessageRepository;



/**
* Returns health json node for kafka, campaign url and cassandra
* Returns health node
*
* @return JsonNode
* @throws JsonMappingException
* @throws JsonProcessingException
* @throws IOException
* @return Mono<JsonNode>
*/
public JsonNode getAllHealthNode() throws JsonMappingException, JsonProcessingException, IOException {
ObjectMapper mapper = new ObjectMapper();
JsonNode jsonNode = mapper.readTree("{\"checks\":[{\"name\":\"kafka\",\"healthy\":false},{\"name\":\"campaign\",\"healthy\":false}],\"healthy\":true}");

/* Cassandra health info */
JsonNode cassandraHealthNode = getCassandraHealthNode();
JsonNode cassandraNode = mapper.createObjectNode();
((ObjectNode) cassandraNode).put("name", "Cassandra");
((ObjectNode) cassandraNode).put("healthy", cassandraHealthNode.get("healthy").asBoolean());

/* Kafka health info */
JsonNode kafkaHealthNode = getKafkaHealthNode();
JsonNode kafkaNode = mapper.createObjectNode();
((ObjectNode) kafkaNode).put("name", "Kafka");
((ObjectNode) kafkaNode).put("healthy", kafkaHealthNode.get("healthy").asBoolean());
((ObjectNode) kafkaNode).put("details", kafkaHealthNode.get("details"));

/* create `ArrayNode` object */
ArrayNode arrayNode = mapper.createArrayNode();

/* add JSON users to array */
arrayNode.addAll(Arrays.asList(cassandraNode, kafkaNode));

((ObjectNode) jsonNode).putArray("checks").addAll(arrayNode);

/* System overall health */
if(cassandraNode.get("healthy").booleanValue() && kafkaHealthNode.get("healthy").booleanValue()) {
((ObjectNode) jsonNode).put("healthy", true);
} else {
((ObjectNode) jsonNode).put("healthy", false);
}

return jsonNode;
public Mono<JsonNode> getCassandraHealthNode() {
return getIsCassandraHealthy().map(healthy -> new ObjectMapper().createObjectNode().put("healthy", healthy));
}



/**
* Returns health node
*
* @return
* @throws JsonMappingException
* @throws JsonProcessingException
* Returns the combined health of kafka, campaign and cassandra.
*
* @return Returns details of each service in `checks` key and
* the overall health status in `healthy` key.
*/
@SuppressWarnings("deprecation")
public JsonNode getCassandraHealthNode() throws IOException, JsonMappingException, JsonProcessingException {
Boolean cassandraHealth = getIsCassandraHealthy();

/* Result node */
ObjectMapper mapper = new ObjectMapper();
JsonNode jsonNode = mapper.readTree("{\"healthy\":false}");
/* Add data in result node */
((ObjectNode) jsonNode).put("healthy", cassandraHealth);

return jsonNode;
@Override
public Mono<JsonNode> getAllHealthNode() {
ObjectNode resultNode = new ObjectMapper().createObjectNode();
Mono<JsonNode> externalComponentHealth = super.getAllHealthNode();
Mono<JsonNode> cassandraHealth = getCassandraHealthNode().map(result -> {
ObjectNode objectNode = new ObjectMapper().createObjectNode();
objectNode.put("name", "cassandra");
objectNode.set("healthy", result.get("healthy"));
return objectNode;
});
return Mono.zip(externalComponentHealth, cassandraHealth).map(healths -> {
resultNode.putArray("checks")
.add(healths.getT1().get("checks"))
.add(healths.getT2());
resultNode.put("healthy",
healths.getT1().get("healthy").booleanValue() &&
healths.getT2().get("healthy").booleanValue()
);
return resultNode;
});
}

/**
* Check if Cassandra connecting or not
*
* @return Boolean
* @return Mono<Boolean>
*/
public Boolean getIsCassandraHealthy() {
Boolean isHealthy = false;
try {
Mono<Boolean> exists = xMessageRepository.existsByUserId("Test");
exists.subscribe(System.out::println);
isHealthy = true;
} catch(Exception e) {
isHealthy = false;
}

return isHealthy;
private Mono<Boolean> getIsCassandraHealthy() {
return Mono.fromCallable(() -> {
try {
xMessageRepository.existsByUserId("Test");
return true;
} catch(Exception e) {
return false;
}
});
}
}

0 comments on commit c02638a

Please sign in to comment.