From e70d6e58dc55d8be21f471c345a4e368f3747914 Mon Sep 17 00:00:00 2001 From: Szepesi Tibor Date: Sun, 3 Nov 2024 18:47:37 +0100 Subject: [PATCH] Implement better liveness probe (#91) --- .github/workflows/check.yml | 1 + src/handlers/mod.rs | 17 ++++++++++++++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/.github/workflows/check.yml b/.github/workflows/check.yml index b6180713..1800b3e0 100644 --- a/.github/workflows/check.yml +++ b/.github/workflows/check.yml @@ -52,6 +52,7 @@ jobs: name: cargo-deny check ${{ matrix.checks }} runs-on: ubuntu-latest strategy: + fail-fast: false matrix: checks: - advisories diff --git a/src/handlers/mod.rs b/src/handlers/mod.rs index fa017e96..3e867cd4 100644 --- a/src/handlers/mod.rs +++ b/src/handlers/mod.rs @@ -7,10 +7,13 @@ mod team; use crate::{middlewares::PermissionsLayer, state::StateTrait}; use axum::{ + extract::State, handler::Handler, + http::StatusCode, routing::{get, post}, Router, }; +use sea_orm::ConnectionTrait; pub fn routes(state: S) -> Router { Router::new() @@ -26,6 +29,18 @@ pub fn routes(state: S) -> Router { .layer(PermissionsLayer::new(state, &["mathcompetition.admin"])), ), ) - .route("/liveness", get(|| async {})) + .route("/liveness", get(liveness::)) .route("/readiness", get(|| async {})) } + +async fn liveness(State(state): State) -> StatusCode { + if state.db().execute_unprepared("select 1").await.is_err() { + return StatusCode::INTERNAL_SERVER_ERROR; + } + + if state.nats().connection_state() != async_nats::connection::State::Connected { + return StatusCode::INTERNAL_SERVER_ERROR; + } + + StatusCode::OK +}