diff --git a/src/main/java/com/uci/outbound/health/HealthController.java b/src/main/java/com/uci/outbound/health/HealthController.java index 06f3e51..278df37 100644 --- a/src/main/java/com/uci/outbound/health/HealthController.java +++ b/src/main/java/com/uci/outbound/health/HealthController.java @@ -1,5 +1,6 @@ package com.uci.outbound.health; +import com.fasterxml.jackson.databind.JsonNode; import com.uci.dao.service.HealthService; import com.uci.utils.model.ApiResponse; import com.uci.utils.model.ApiResponseParams; @@ -7,6 +8,7 @@ import reactor.core.publisher.Mono; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.actuate.health.Status; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.RequestMapping; @@ -25,9 +27,17 @@ public Mono> statusCheck() { return healthService.getAllHealthNode().map(health -> ApiResponse.builder() .id("api.health") .params(ApiResponseParams.builder().build()) - .responseCode(HttpStatus.OK.name()) .result(health) .build() - ).map(ResponseEntity::ok); + ).map(response -> { + if (((JsonNode)response.result).get("status").textValue().equals(Status.UP.getCode())) { + response.responseCode = HttpStatus.OK.name(); + return new ResponseEntity<>(response, HttpStatus.OK); + } + else { + response.responseCode = HttpStatus.SERVICE_UNAVAILABLE.name(); + return new ResponseEntity<>(response, HttpStatus.SERVICE_UNAVAILABLE); + } + }); } } diff --git a/src/main/java/com/uci/outbound/health/ServiceStatusController.java b/src/main/java/com/uci/outbound/health/ServiceStatusController.java index d41d0ec..b339c5d 100644 --- a/src/main/java/com/uci/outbound/health/ServiceStatusController.java +++ b/src/main/java/com/uci/outbound/health/ServiceStatusController.java @@ -1,10 +1,7 @@ package com.uci.outbound.health; 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.ObjectNode; import com.uci.dao.service.HealthService; import com.uci.utils.model.ApiResponse; @@ -12,9 +9,8 @@ import lombok.extern.slf4j.Slf4j; import reactor.core.publisher.Mono; -import java.io.IOException; - import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.actuate.health.Status; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.RequestMapping; @@ -30,23 +26,28 @@ public class ServiceStatusController { private HealthService healthService; /** - * In use by sunbird team - to check service liveliness & readliness - * @return - * @throws JsonProcessingException - */ + * In use by sunbird team - to check service liveliness & readliness + * + * @return + * @throws JsonProcessingException + */ @RequestMapping(value = "/health", method = RequestMethod.GET, produces = { "application/json", "text/json" }) - public ResponseEntity statusCheck() throws JsonProcessingException { - ObjectMapper mapper = new ObjectMapper(); - JsonNode resultNode = mapper.readTree("{\"healthy\":true}"); - - ApiResponse response = ApiResponse.builder() - .id("api.service.health.cassandra") - .params(ApiResponseParams.builder().build()) - .responseCode(HttpStatus.OK.name()) - .result(resultNode) - .build(); - - return ResponseEntity.ok(response); + public Mono> statusCheck() throws JsonProcessingException { + return healthService.getAllHealthNode().map(health -> ApiResponse.builder() + .id("api.health") + .params(ApiResponseParams.builder().build()) + .result(health) + .build() + ).map(response -> { + if (((JsonNode)response.result).get("status").textValue().equals(Status.UP.getCode())) { + response.responseCode = HttpStatus.OK.name(); + return new ResponseEntity<>(response, HttpStatus.OK); + } + else { + response.responseCode = HttpStatus.SERVICE_UNAVAILABLE.name(); + return new ResponseEntity<>(response, HttpStatus.SERVICE_UNAVAILABLE); + } + }); } @RequestMapping(value = "/health/cassandra", method = RequestMethod.GET, produces = { "application/json", "text/json" }) @@ -55,10 +56,18 @@ public Mono> cassandraStatusCheck() { ApiResponse.builder() .id("api.service.health.cassandra") .params(ApiResponseParams.builder().build()) - .responseCode(HttpStatus.OK.name()) .result(result) .build()) - .map(ResponseEntity::ok); + .map(response -> { + if (((JsonNode)response.result).get("status").textValue().equals(Status.UP.getCode())) { + response.responseCode = HttpStatus.OK.name(); + return new ResponseEntity<>(response, HttpStatus.OK); + } + else { + response.responseCode = HttpStatus.SERVICE_UNAVAILABLE.name(); + return new ResponseEntity<>(response, HttpStatus.SERVICE_UNAVAILABLE); + } + }); } @RequestMapping(value = "/health/kafka", method = RequestMethod.GET, produces = { "application/json", "text/json" }) @@ -67,10 +76,18 @@ public Mono> kafkaStatusCheck() { ApiResponse.builder() .id("api.service.health.kafka") .params(ApiResponseParams.builder().build()) - .responseCode(HttpStatus.OK.name()) .result(result) .build()) - .map(ResponseEntity::ok); + .map(response -> { + if (((JsonNode)response.result).get("status").textValue().equals(Status.UP.getCode())) { + response.responseCode = HttpStatus.OK.name(); + return new ResponseEntity<>(response, HttpStatus.OK); + } + else { + response.responseCode = HttpStatus.SERVICE_UNAVAILABLE.name(); + return new ResponseEntity<>(response, HttpStatus.SERVICE_UNAVAILABLE); + } + }); } @RequestMapping(value = "/health/campaign", method = RequestMethod.GET, produces = { "application/json", "text/json" }) @@ -78,9 +95,17 @@ public Mono> campaignUrlStatusCheck() { return healthService.getCampaignUrlHealthNode().map(result -> ApiResponse.builder().id("api.service.health.campaign") .params(ApiResponseParams.builder().build()) - .responseCode(HttpStatus.OK.name()) .result(result) .build()) - .map(ResponseEntity::ok); + .map(response -> { + if (((JsonNode)response.result).get("status").textValue().equals(Status.UP.getCode())) { + response.responseCode = HttpStatus.OK.name(); + return new ResponseEntity<>(response, HttpStatus.OK); + } + else { + response.responseCode = HttpStatus.SERVICE_UNAVAILABLE.name(); + return new ResponseEntity<>(response, HttpStatus.SERVICE_UNAVAILABLE); + } + }); } }