3 Commits

Author SHA1 Message Date
878df8da40 Start graceful shutdown on SIGTERM
All checks were successful
Build and deploy / Build container and manifests (push) Successful in 5m32s
2025-04-20 00:58:18 +02:00
27f6119905 Second ctrl-c forces application to stop directly
All checks were successful
Build and deploy / Build container and manifests (push) Successful in 5m21s
2025-04-20 00:26:23 +02:00
c7b0cfc888 Gracefully shutdown if LDAP connection is lost 2025-04-20 00:24:32 +02:00
2 changed files with 34 additions and 8 deletions

View File

@@ -3,7 +3,7 @@ use russh::keys::PublicKey;
use tokio::select;
use tokio::task::JoinHandle;
use tokio_util::sync::CancellationToken;
use tracing::{debug, warn};
use tracing::{debug, error};
#[derive(Debug, Clone)]
pub struct Ldap {
@@ -51,9 +51,10 @@ impl Ldap {
select! {
res = conn.drive() => {
if let Err(err) = res {
warn!("LDAP connection error: {}", err);
error!("LDAP connection error: {}", err);
} else {
debug!("LDAP drive has stopped, this should not happen?");
error!("LDAP connection lost");
token.cancel();
}
}
_ = token.cancelled() => {

View File

@@ -15,18 +15,43 @@ use siranga::web::{ForwardAuth, Service};
use tokio::net::TcpListener;
use tokio::select;
use tokio_util::sync::CancellationToken;
use tracing::{error, info, warn};
use tracing::{debug, error, info, warn};
use tracing_subscriber::EnvFilter;
use tracing_subscriber::layer::SubscriberExt;
use tracing_subscriber::util::SubscriberInitExt;
#[cfg(unix)]
async fn sigterm() {
use tokio::signal::unix::SignalKind;
let mut sigterm =
tokio::signal::unix::signal(SignalKind::terminate()).expect("should be able to initialize");
sigterm.recv().await;
}
#[cfg(not(unix))]
async fn sigterm() {
std::future::pending::<()>().await;
}
async fn shutdown_task(token: CancellationToken) {
tokio::signal::ctrl_c()
.await
.expect("Failed to listen for ctrl-c event");
select! {
_ = tokio::signal::ctrl_c() => {
debug!("Received SIGINT");
}
_ = sigterm() => {
debug!("Received SIGTERM");
}
_ = token.cancelled() => {
debug!("Application called for graceful shutdown");
}
}
info!("Starting graceful shutdown");
token.cancel();
tokio::time::sleep(Duration::from_secs(5)).await;
select! {
_ = tokio::time::sleep(Duration::from_secs(5)) => {}
_ = tokio::signal::ctrl_c() => {}
}
}
#[tokio::main]