|
| 1 | +use axum::{extract::State, http::StatusCode, response::IntoResponse, Json}; |
| 2 | +use chrono::{DateTime, Utc}; |
| 3 | +use serde::Serialize; |
| 4 | +use std::sync::Arc; |
| 5 | + |
| 6 | +use crate::api::AppState; |
| 7 | + |
| 8 | +const MAX_INDEXER_AGE_MINUTES: i64 = 5; |
| 9 | + |
| 10 | +#[derive(Serialize)] |
| 11 | +struct HealthResponse { |
| 12 | + status: &'static str, |
| 13 | + #[serde(skip_serializing_if = "Option::is_none")] |
| 14 | + reason: Option<String>, |
| 15 | +} |
| 16 | + |
| 17 | +fn readiness_status( |
| 18 | + latest_indexed_at: Option<DateTime<Utc>>, |
| 19 | + now: DateTime<Utc>, |
| 20 | +) -> (StatusCode, HealthResponse) { |
| 21 | + let Some(indexed_at) = latest_indexed_at else { |
| 22 | + return ( |
| 23 | + StatusCode::SERVICE_UNAVAILABLE, |
| 24 | + HealthResponse { |
| 25 | + status: "not_ready", |
| 26 | + reason: Some("indexer state unavailable".to_string()), |
| 27 | + }, |
| 28 | + ); |
| 29 | + }; |
| 30 | + |
| 31 | + let age = now - indexed_at; |
| 32 | + if age > chrono::Duration::minutes(MAX_INDEXER_AGE_MINUTES) { |
| 33 | + return ( |
| 34 | + StatusCode::SERVICE_UNAVAILABLE, |
| 35 | + HealthResponse { |
| 36 | + status: "not_ready", |
| 37 | + reason: Some(format!( |
| 38 | + "indexer stale: last block indexed {}s ago", |
| 39 | + age.num_seconds() |
| 40 | + )), |
| 41 | + }, |
| 42 | + ); |
| 43 | + } |
| 44 | + |
| 45 | + ( |
| 46 | + StatusCode::OK, |
| 47 | + HealthResponse { |
| 48 | + status: "ready", |
| 49 | + reason: None, |
| 50 | + }, |
| 51 | + ) |
| 52 | +} |
| 53 | + |
| 54 | +/// GET /health/live — liveness probe (process is alive) |
| 55 | +pub async fn liveness() -> impl IntoResponse { |
| 56 | + Json(HealthResponse { |
| 57 | + status: "ok", |
| 58 | + reason: None, |
| 59 | + }) |
| 60 | +} |
| 61 | + |
| 62 | +/// GET /health/ready — readiness probe (DB reachable, indexer fresh) |
| 63 | +pub async fn readiness(State(state): State<Arc<AppState>>) -> impl IntoResponse { |
| 64 | + // Check DB connectivity |
| 65 | + if let Err(e) = sqlx::query("SELECT 1").execute(&state.pool).await { |
| 66 | + tracing::warn!(error = %e, "readiness database check failed"); |
| 67 | + return ( |
| 68 | + StatusCode::SERVICE_UNAVAILABLE, |
| 69 | + Json(HealthResponse { |
| 70 | + status: "not_ready", |
| 71 | + reason: Some("database unreachable".to_string()), |
| 72 | + }), |
| 73 | + ); |
| 74 | + } |
| 75 | + |
| 76 | + let latest = match super::status::latest_indexed_block(state.as_ref()).await { |
| 77 | + Ok(latest) => latest, |
| 78 | + Err(e) => { |
| 79 | + tracing::warn!(error = %e, "readiness indexer state check failed"); |
| 80 | + return ( |
| 81 | + StatusCode::SERVICE_UNAVAILABLE, |
| 82 | + Json(HealthResponse { |
| 83 | + status: "not_ready", |
| 84 | + reason: Some("indexer state unavailable".to_string()), |
| 85 | + }), |
| 86 | + ); |
| 87 | + } |
| 88 | + }; |
| 89 | + |
| 90 | + let (status, body) = readiness_status(latest.map(|(_, indexed_at)| indexed_at), Utc::now()); |
| 91 | + (status, Json(body)) |
| 92 | +} |
| 93 | + |
| 94 | +#[cfg(test)] |
| 95 | +mod tests { |
| 96 | + use super::*; |
| 97 | + use crate::head::HeadTracker; |
| 98 | + use crate::metrics::Metrics; |
| 99 | + use axum::body::to_bytes; |
| 100 | + use sqlx::postgres::PgPoolOptions; |
| 101 | + use std::sync::Arc; |
| 102 | + use tokio::sync::broadcast; |
| 103 | + |
| 104 | + fn app_state(pool: sqlx::PgPool, head_tracker: Arc<HeadTracker>) -> Arc<AppState> { |
| 105 | + let (block_tx, _) = broadcast::channel(1); |
| 106 | + let (da_tx, _) = broadcast::channel(1); |
| 107 | + let prometheus_handle = metrics_exporter_prometheus::PrometheusBuilder::new() |
| 108 | + .build_recorder() |
| 109 | + .handle(); |
| 110 | + |
| 111 | + Arc::new(AppState { |
| 112 | + pool, |
| 113 | + block_events_tx: block_tx, |
| 114 | + da_events_tx: da_tx, |
| 115 | + head_tracker, |
| 116 | + rpc_url: String::new(), |
| 117 | + da_tracking_enabled: false, |
| 118 | + faucet: None, |
| 119 | + chain_id: 1, |
| 120 | + chain_name: "Test Chain".to_string(), |
| 121 | + chain_logo_url: None, |
| 122 | + chain_logo_url_light: None, |
| 123 | + chain_logo_url_dark: None, |
| 124 | + accent_color: None, |
| 125 | + background_color_dark: None, |
| 126 | + background_color_light: None, |
| 127 | + success_color: None, |
| 128 | + error_color: None, |
| 129 | + metrics: Metrics::new(), |
| 130 | + prometheus_handle, |
| 131 | + }) |
| 132 | + } |
| 133 | + |
| 134 | + async fn json_response(response: axum::response::Response) -> (StatusCode, serde_json::Value) { |
| 135 | + let status = response.status(); |
| 136 | + let body = to_bytes(response.into_body(), usize::MAX) |
| 137 | + .await |
| 138 | + .expect("read response body"); |
| 139 | + let json = serde_json::from_slice(&body).expect("parse json response"); |
| 140 | + (status, json) |
| 141 | + } |
| 142 | + |
| 143 | + #[tokio::test] |
| 144 | + async fn liveness_returns_ok() { |
| 145 | + let (status, json) = json_response(liveness().await.into_response()).await; |
| 146 | + |
| 147 | + assert_eq!(status, StatusCode::OK); |
| 148 | + assert_eq!(json["status"], "ok"); |
| 149 | + assert!(json.get("reason").is_none()); |
| 150 | + } |
| 151 | + |
| 152 | + #[tokio::test] |
| 153 | + async fn readiness_returns_unavailable_when_database_is_down() { |
| 154 | + let pool = PgPoolOptions::new() |
| 155 | + .connect_lazy("postgres://postgres:postgres@127.0.0.1:1/atlas") |
| 156 | + .expect("create lazy pool"); |
| 157 | + let state = app_state(pool, Arc::new(HeadTracker::empty(10))); |
| 158 | + |
| 159 | + let (status, json) = json_response(readiness(State(state)).await.into_response()).await; |
| 160 | + |
| 161 | + assert_eq!(status, StatusCode::SERVICE_UNAVAILABLE); |
| 162 | + assert_eq!(json["status"], "not_ready"); |
| 163 | + assert_eq!(json["reason"], "database unreachable"); |
| 164 | + } |
| 165 | + |
| 166 | + #[test] |
| 167 | + fn readiness_returns_unavailable_when_indexer_state_is_missing() { |
| 168 | + let (status, body) = readiness_status(None, Utc::now()); |
| 169 | + assert_eq!(status, StatusCode::SERVICE_UNAVAILABLE); |
| 170 | + assert_eq!(body.status, "not_ready"); |
| 171 | + assert_eq!(body.reason.as_deref(), Some("indexer state unavailable")); |
| 172 | + } |
| 173 | + |
| 174 | + #[test] |
| 175 | + fn readiness_returns_unavailable_for_stale_indexer_state() { |
| 176 | + let (status, body) = readiness_status( |
| 177 | + Some(Utc::now() - chrono::Duration::minutes(MAX_INDEXER_AGE_MINUTES + 1)), |
| 178 | + Utc::now(), |
| 179 | + ); |
| 180 | + assert_eq!(status, StatusCode::SERVICE_UNAVAILABLE); |
| 181 | + assert_eq!(body.status, "not_ready"); |
| 182 | + assert!(body |
| 183 | + .reason |
| 184 | + .as_deref() |
| 185 | + .expect("reason string") |
| 186 | + .contains("indexer stale")); |
| 187 | + } |
| 188 | + |
| 189 | + #[test] |
| 190 | + fn readiness_returns_ready_for_fresh_indexer_state() { |
| 191 | + let (status, body) = readiness_status(Some(Utc::now()), Utc::now()); |
| 192 | + assert_eq!(status, StatusCode::OK); |
| 193 | + assert_eq!(body.status, "ready"); |
| 194 | + assert!(body.reason.is_none()); |
| 195 | + } |
| 196 | +} |
0 commit comments