Skip to content

Commit

Permalink
New Health Check service for cb-ext (#177)
Browse files Browse the repository at this point in the history
* New Health Check service for cb-ext

* Review changes
  • Loading branch information
sreeragksgh authored Mar 7, 2023
1 parent 496b67e commit 1531e42
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/main/java/org/sunbird/common/util/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
21 changes: 21 additions & 0 deletions src/main/java/org/sunbird/health/controller/HealthController.java
Original file line number Diff line number Diff line change
@@ -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());
}
}
9 changes: 9 additions & 0 deletions src/main/java/org/sunbird/health/service/HealthService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.sunbird.health.service;

import org.sunbird.common.model.SBApiResponse;

public interface HealthService {

SBApiResponse checkHealthStatus() throws Exception;

}
74 changes: 74 additions & 0 deletions src/main/java/org/sunbird/health/service/HealthServiceImpl.java
Original file line number Diff line number Diff line change
@@ -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<Map<String, Object>> 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<String, Object> result = new HashMap<>();
result.put(Constants.NAME, Constants.CASSANDRA_DB);
Boolean res = true;
List<Map<String, Object>> 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<Map<String, Object>>) response.get(Constants.CHECKS)).add(result);
}

private void redisHealthStatus(SBApiResponse response) throws Exception {
Map<String, Object> 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<Map<String, Object>>) response.get(Constants.CHECKS)).add(result);
}

}

0 comments on commit 1531e42

Please sign in to comment.