-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New Health Check service for cb-ext (#177)
* New Health Check service for cb-ext * Review changes
- Loading branch information
1 parent
496b67e
commit 1531e42
Showing
4 changed files
with
109 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
src/main/java/org/sunbird/health/controller/HealthController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
74
src/main/java/org/sunbird/health/service/HealthServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} | ||
|