Skip to main content

jax_daemon/http_server/health/
liveness.rs

1use axum::http::StatusCode;
2use axum::response::{IntoResponse, Response};
3use axum::Json;
4use reqwest::{Client, RequestBuilder, Url};
5use serde::{Deserialize, Serialize};
6
7use crate::http_server::api::client::ApiRequest;
8
9/// Request type for the liveness probe endpoint.
10#[derive(Debug, Clone, Serialize, Deserialize)]
11pub struct LivezRequest {}
12
13/// Response type for the liveness probe endpoint.
14#[derive(Debug, Clone, Serialize, Deserialize)]
15pub struct LivezResponse {
16    pub status: String,
17}
18
19impl ApiRequest for LivezRequest {
20    type Response = LivezResponse;
21
22    fn build_request(self, base_url: &Url, client: &Client) -> RequestBuilder {
23        let full_url = base_url.join("/_status/livez").unwrap();
24        client.get(full_url)
25    }
26}
27
28/// This is a very simple handler that always returns with a valid response. It's intended to be
29/// used by external healthchecks to see whether the service is "alive". Failing this check for any
30/// reason generally leads to immediate termination of the service.
31///
32/// If you're looking for how to report a service issue, please refer to
33/// [`crate::health_check::readiness::handler`].
34#[tracing::instrument]
35pub async fn handler() -> Response {
36    let msg = serde_json::json!({"status": "ok"});
37    (StatusCode::OK, Json(msg)).into_response()
38}
39
40#[cfg(test)]
41mod tests {
42    use super::*;
43
44    #[tokio::test]
45    async fn test_handler_direct() {
46        let response = handler().await;
47        assert_eq!(response.status(), StatusCode::OK);
48
49        let body = axum::body::to_bytes(response.into_body(), usize::MAX)
50            .await
51            .unwrap();
52
53        assert_eq!(&body[..], b"{\"status\":\"ok\"}");
54    }
55}