|
| 1 | +/* |
| 2 | +* AMRIT – Accessible Medical Records via Integrated Technology |
| 3 | +* Integrated EHR (Electronic Health Records) Solution |
| 4 | +* |
| 5 | +* Copyright (C) "Piramal Swasthya Management and Research Institute" |
| 6 | +* |
| 7 | +* This file is part of AMRIT. |
| 8 | +* |
| 9 | +* This program is free software: you can redistribute it and/or modify |
| 10 | +* it under the terms of the GNU General Public License as published by |
| 11 | +* the Free Software Foundation, either version 3 of the License, or |
| 12 | +* (at your option) any later version. |
| 13 | +* |
| 14 | +* This program is distributed in the hope that it will be useful, |
| 15 | +* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | +* GNU General Public License for more details. |
| 18 | +* |
| 19 | +* You should have received a copy of the GNU General Public License |
| 20 | +* along with this program. If not, see https://www.gnu.org/licenses/. |
| 21 | +*/ |
| 22 | +package com.iemr.admin.service.health; |
| 23 | + |
| 24 | +import java.sql.Connection; |
| 25 | +import java.sql.PreparedStatement; |
| 26 | +import java.sql.ResultSet; |
| 27 | +import java.util.HashMap; |
| 28 | +import java.util.Map; |
| 29 | + |
| 30 | +import javax.sql.DataSource; |
| 31 | + |
| 32 | +import org.slf4j.Logger; |
| 33 | +import org.slf4j.LoggerFactory; |
| 34 | +import org.springframework.beans.factory.annotation.Autowired; |
| 35 | +import org.springframework.data.redis.core.RedisCallback; |
| 36 | +import org.springframework.data.redis.core.RedisTemplate; |
| 37 | +import org.springframework.stereotype.Service; |
| 38 | + |
| 39 | +@Service |
| 40 | +public class HealthService { |
| 41 | + |
| 42 | + private static final Logger logger = LoggerFactory.getLogger(HealthService.class); |
| 43 | + private static final String DB_HEALTH_CHECK_QUERY = "SELECT 1 as health_check"; |
| 44 | + |
| 45 | + @Autowired |
| 46 | + private DataSource dataSource; |
| 47 | + |
| 48 | + @Autowired(required = false) |
| 49 | + private RedisTemplate<String, Object> redisTemplate; |
| 50 | + |
| 51 | + public Map<String, Object> checkHealth() { |
| 52 | + Map<String, Object> healthStatus = new HashMap<>(); |
| 53 | + boolean overallHealth = true; |
| 54 | + |
| 55 | + // Check database connectivity (details logged internally, not exposed) |
| 56 | + boolean dbHealthy = checkDatabaseHealthInternal(); |
| 57 | + if (!dbHealthy) { |
| 58 | + overallHealth = false; |
| 59 | + } |
| 60 | + |
| 61 | + // Check Redis connectivity if configured (details logged internally) |
| 62 | + if (redisTemplate != null) { |
| 63 | + boolean redisHealthy = checkRedisHealthInternal(); |
| 64 | + if (!redisHealthy) { |
| 65 | + overallHealth = false; |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + healthStatus.put("status", overallHealth ? "UP" : "DOWN"); |
| 70 | + |
| 71 | + logger.info("Health check completed - Overall status: {}", overallHealth ? "UP" : "DOWN"); |
| 72 | + return healthStatus; |
| 73 | + } |
| 74 | + |
| 75 | + private boolean checkDatabaseHealthInternal() { |
| 76 | + long startTime = System.currentTimeMillis(); |
| 77 | + |
| 78 | + try (Connection connection = dataSource.getConnection()) { |
| 79 | + boolean isConnectionValid = connection.isValid(2); // 2 second timeout per best practices |
| 80 | + |
| 81 | + if (isConnectionValid) { |
| 82 | + try (PreparedStatement stmt = connection.prepareStatement(DB_HEALTH_CHECK_QUERY)) { |
| 83 | + stmt.setQueryTimeout(3); // 3 second query timeout |
| 84 | + try (ResultSet rs = stmt.executeQuery()) { |
| 85 | + if (rs.next() && rs.getInt(1) == 1) { |
| 86 | + long responseTime = System.currentTimeMillis() - startTime; |
| 87 | + logger.debug("Database health check: UP ({}ms)", responseTime); |
| 88 | + return true; |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + logger.warn("Database health check: Connection not valid"); |
| 94 | + return false; |
| 95 | + } catch (Exception e) { |
| 96 | + logger.error("Database health check failed: {}", e.getMessage()); |
| 97 | + return false; |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + private boolean checkRedisHealthInternal() { |
| 102 | + long startTime = System.currentTimeMillis(); |
| 103 | + |
| 104 | + try { |
| 105 | + String pong = redisTemplate.execute((RedisCallback<String>) connection -> connection.ping()); |
| 106 | + |
| 107 | + if ("PONG".equals(pong)) { |
| 108 | + long responseTime = System.currentTimeMillis() - startTime; |
| 109 | + logger.debug("Redis health check: UP ({}ms)", responseTime); |
| 110 | + return true; |
| 111 | + } |
| 112 | + logger.warn("Redis health check: Ping returned unexpected response"); |
| 113 | + return false; |
| 114 | + } catch (Exception e) { |
| 115 | + logger.error("Redis health check failed: {}", e.getMessage()); |
| 116 | + return false; |
| 117 | + } |
| 118 | + } |
| 119 | +} |
0 commit comments