From 1531e427168f722c52ea9589b692c3dc6b9c758d Mon Sep 17 00:00:00 2001 From: Sreerag K S <58926794+sreeragksgh@users.noreply.github.com> Date: Tue, 7 Mar 2023 16:41:25 +0530 Subject: [PATCH] New Health Check service for cb-ext (#177) * New Health Check service for cb-ext * Review changes --- .../org/sunbird/common/util/Constants.java | 5 ++ .../health/controller/HealthController.java | 21 ++++++ .../sunbird/health/service/HealthService.java | 9 +++ .../health/service/HealthServiceImpl.java | 74 +++++++++++++++++++ 4 files changed, 109 insertions(+) create mode 100644 src/main/java/org/sunbird/health/controller/HealthController.java create mode 100644 src/main/java/org/sunbird/health/service/HealthService.java create mode 100644 src/main/java/org/sunbird/health/service/HealthServiceImpl.java diff --git a/src/main/java/org/sunbird/common/util/Constants.java b/src/main/java/org/sunbird/common/util/Constants.java index 8b9a6ac85..c407763d5 100644 --- a/src/main/java/org/sunbird/common/util/Constants.java +++ b/src/main/java/org/sunbird/common/util/Constants.java @@ -627,9 +627,14 @@ public class Constants { public static final String ADD_OFFENSIVE_DATA_FLAG = "api.add.offensive.data.flag"; public static final String UPDATE_OFFENSIVE_DATA_FLAG = "api.update.offensive.data.flag"; public static final String GET_OFFENSIVE_DATA_FLAG = "api.get.offensive.data.flag"; + public static final String API_HEALTH_CHECK = "api.health.check"; public static final String DRAFT = "DRAFT"; public static final Object CREATED = "Created"; public static final Object UPDATED = "Updated"; + public static final String HEALTHY = "healthy"; + public static final String CHECKS = "checks"; + public static final String CASSANDRA_DB = "cassandra db"; + public static final String REDIS_CACHE = "redis cache"; private Constants() { throw new IllegalStateException("Utility class"); diff --git a/src/main/java/org/sunbird/health/controller/HealthController.java b/src/main/java/org/sunbird/health/controller/HealthController.java new file mode 100644 index 000000000..4115d6a51 --- /dev/null +++ b/src/main/java/org/sunbird/health/controller/HealthController.java @@ -0,0 +1,21 @@ +package org.sunbird.health.controller; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RestController; +import org.sunbird.common.model.SBApiResponse; +import org.sunbird.health.service.HealthService; + +@RestController +public class HealthController { + + @Autowired + private HealthService healthService; + + @GetMapping("/health") + public ResponseEntity healthCheck() throws Exception { + SBApiResponse response = healthService.checkHealthStatus(); + return new ResponseEntity<>(response, response.getResponseCode()); + } +} diff --git a/src/main/java/org/sunbird/health/service/HealthService.java b/src/main/java/org/sunbird/health/service/HealthService.java new file mode 100644 index 000000000..1151bc372 --- /dev/null +++ b/src/main/java/org/sunbird/health/service/HealthService.java @@ -0,0 +1,9 @@ +package org.sunbird.health.service; + +import org.sunbird.common.model.SBApiResponse; + +public interface HealthService { + + SBApiResponse checkHealthStatus() throws Exception; + +} diff --git a/src/main/java/org/sunbird/health/service/HealthServiceImpl.java b/src/main/java/org/sunbird/health/service/HealthServiceImpl.java new file mode 100644 index 000000000..c172c0ff4 --- /dev/null +++ b/src/main/java/org/sunbird/health/service/HealthServiceImpl.java @@ -0,0 +1,74 @@ +package org.sunbird.health.service; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.stereotype.Service; +import org.sunbird.cache.service.RedisCacheService; +import org.sunbird.cassandra.utils.CassandraOperation; +import org.sunbird.common.model.SBApiResponse; +import org.sunbird.common.util.Constants; +import org.sunbird.common.util.ProjectUtil; + +import java.util.*; + +@Service +public class HealthServiceImpl implements HealthService { + + + @Autowired + CassandraOperation cassandraOperation; + + @Autowired + RedisCacheService redisCacheService; + + private Logger log = LoggerFactory.getLogger(getClass().getName()); + + @Override + public SBApiResponse checkHealthStatus() throws Exception { + SBApiResponse response = ProjectUtil.createDefaultResponse(Constants.API_HEALTH_CHECK); + try { + response.put(Constants.HEALTHY, true); + List> healthResults = new ArrayList<>(); + response.put(Constants.CHECKS, healthResults); + cassandraHealthStatus(response); + redisHealthStatus(response); + } catch (Exception e) { + log.error("Failed to process health check. Exception: ", e); + response.getParams().setStatus(Constants.FAILED); + response.getParams().setErr(e.getMessage()); + response.setResponseCode(HttpStatus.INTERNAL_SERVER_ERROR); + } + return response; + } + + private void cassandraHealthStatus(SBApiResponse response) throws Exception { + Map result = new HashMap<>(); + result.put(Constants.NAME, Constants.CASSANDRA_DB); + Boolean res = true; + List> cassandraQueryResponse = cassandraOperation.getRecordsByProperties( + Constants.KEYSPACE_SUNBIRD, Constants.TABLE_SYSTEM_SETTINGS, null, null); + if (cassandraQueryResponse.isEmpty()) { + res = false; + response.put(Constants.HEALTHY, res); + } + result.put(Constants.HEALTHY, res); + ((List>) response.get(Constants.CHECKS)).add(result); + } + + private void redisHealthStatus(SBApiResponse response) throws Exception { + Map result = new HashMap<>(); + result.put(Constants.NAME, Constants.REDIS_CACHE); + Boolean res = true; + SBApiResponse redisResponse = redisCacheService.getKeys(); + if (!HttpStatus.OK.equals(redisResponse.getResponseCode())) { + res = false; + response.put(Constants.HEALTHY, res); + } + result.put(Constants.HEALTHY, res); + ((List>) response.get(Constants.CHECKS)).add(result); + } + +} +