readiness probe 추가 및 워크플로 보강

- ReadinessController로 DB 연결 여부 확인 엔드포인트 추가
- CI 워크플로에 readiness probe 검증 로직 추가 (120초 제한)
- develop, stage 브랜치 push 이벤트 트리거 추가
This commit is contained in:
Rinjae
2026-06-12 18:23:13 +09:00
parent 9238584cb9
commit 34f5a89f78
2 changed files with 97 additions and 21 deletions
@@ -0,0 +1,67 @@
package com.eactive.apim.portal.apps;
import java.sql.Connection;
import java.sql.SQLException;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* Readiness probe.
*
* <p>Distinct from {@link HealthCheckController} (liveness — servlet alive?).
* This endpoint validates both EMS and Gateway datasources via JDBC
* {@code Connection.isValid(timeout)} to confirm the app is ready to serve
* requests that depend on the database.
*
* <p>HTTP 200 + JSON when all checks pass.
* <p>HTTP 503 + JSON when any check fails — body still includes the per-component
* status so CI/CD logs the failing dependency.
*/
@RestController
public class ReadinessController {
private static final int VALIDATION_TIMEOUT_SECONDS = 2;
private final DataSource portalDataSource;
private final DataSource gatewayDataSource;
public ReadinessController(
@Qualifier("portalDataSource") DataSource portalDataSource,
@Qualifier("gatewayDataSource") DataSource gatewayDataSource) {
this.portalDataSource = portalDataSource;
this.gatewayDataSource = gatewayDataSource;
}
@GetMapping(value = "/health/ready", produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<String> ready() {
String ems = check(portalDataSource);
String gw = check(gatewayDataSource);
boolean ok = "UP".equals(ems) && "UP".equals(gw);
String body = "{"
+ "\"status\":\"" + (ok ? "UP" : "DOWN") + "\","
+ "\"ems\":\"" + ems + "\","
+ "\"gateway\":\""+ gw + "\""
+ "}";
return ResponseEntity.status(ok ? 200 : 503)
.contentType(MediaType.APPLICATION_JSON)
.body(body);
}
private static String check(DataSource ds) {
if (ds == null) return "DOWN:NO_DATASOURCE";
try (Connection c = ds.getConnection()) {
return c.isValid(VALIDATION_TIMEOUT_SECONDS) ? "UP" : "DOWN:INVALID";
} catch (SQLException e) {
String msg = e.getMessage() == null ? "" : e.getMessage();
return "DOWN:" + msg.replace('"', '\'').replace('\n', ' ').replace('\r', ' ');
}
}
}