Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ serde = { version = "1", features = ["derive"] }
serde_json = "1"
chrono = { version = "0.4", features = ["serde"] }
thiserror = "1"
tokio = { version = "1", features = ["sync", "macros", "rt-multi-thread", "net"] }
tokio = { version = "1", features = ["sync", "macros", "rt-multi-thread", "net", "signal"] }
url = "2"
uuid = { version = "1", features = ["v4"] }
dashmap = "5"
Expand Down
56 changes: 53 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ mod native {
use std::sync::Arc;

use axum::extract::State;
use axum::http::StatusCode;
use axum::response::IntoResponse;
use axum::routing::{get, post};
use axum::{Json, Router};
Expand All @@ -55,6 +56,7 @@ mod native {
cache: Arc<CacheBackend>,
config_watcher: ConfigWatcher,
config_version: u32,
shutdown_flag: Arc<tokio::sync::RwLock<bool>>,
}

/// Build the axum router with all application routes.
Expand All @@ -72,8 +74,14 @@ mod native {
}

/// `GET /health` — returns a JSON health-check payload.
async fn health_handler() -> impl IntoResponse {
Json(json!({"status": "ok"}))
async fn health_handler(State(state): State<AppState>) -> impl IntoResponse {
if *state.shutdown_flag.read().await {
return (
StatusCode::SERVICE_UNAVAILABLE,
Json(json!({"status": "shutting_down"})),
);
}
(StatusCode::OK, Json(json!({"status": "ok"})))
}

/// `GET /metrics` — returns Prometheus text-format metrics.
Expand Down Expand Up @@ -161,6 +169,36 @@ mod native {
}
}

/// Listen for SIGTERM / SIGINT, then flip the shutdown flag so the
/// health endpoint reports 503 while `axum::serve` drains in-flight
/// connections.
async fn shutdown_signal(flag: Arc<tokio::sync::RwLock<bool>>) {
let ctrl_c = async {
tokio::signal::ctrl_c()
.await
.expect("failed to install Ctrl+C handler");
};

#[cfg(unix)]
let terminate = async {
tokio::signal::unix::signal(tokio::signal::unix::SignalKind::terminate())
.expect("failed to install signal handler")
.recv()
.await;
};

#[cfg(not(unix))]
let terminate = std::future::pending::<()>();

tokio::select! {
_ = ctrl_c => {},
_ = terminate => {},
}

*flag.write().await = true;
eprintln!("[proofstell] Shutdown signal received, starting graceful shutdown...");
}

/// Bootstrap: load config, wire up services, and start the server.
pub async fn run() -> anyhow::Result<()> {
// ── Metrics ─────────────────────────────────────────────────
Expand Down Expand Up @@ -263,12 +301,16 @@ mod native {
});

// ── Router ──────────────────────────────────────────────────
let shutdown_flag = Arc::new(tokio::sync::RwLock::new(false));
let webhook_for_shutdown = Arc::clone(&webhook);
let cache_for_shutdown = Arc::clone(&cache);
let state = AppState {
metrics: Arc::clone(&metrics),
webhook,
cache,
config_watcher,
config_version: AppConfig::version(),
shutdown_flag: Arc::clone(&shutdown_flag),
};
let app = build_router(state);

Expand All @@ -278,7 +320,15 @@ mod native {
eprintln!("[proofstell] Config endpoints: GET /config/status, POST /config/reload");

let listener = tokio::net::TcpListener::bind(addr).await?;
axum::serve(listener, app).await?;
axum::serve(listener, app)
.with_graceful_shutdown(shutdown_signal(shutdown_flag))
.await?;

eprintln!("[proofstell] Draining webhook DLQ...");
webhook_for_shutdown.drain_dlq().await;
eprintln!("[proofstell] Flushing cache...");
drop(cache_for_shutdown);
eprintln!("[proofstell] Shutdown complete");

Ok(())
}
Expand Down
Loading