add notify systemd

This commit is contained in:
houseme
2025-04-09 19:11:31 +08:00
parent c872901269
commit 952f04149f
4 changed files with 45 additions and 5 deletions
+1 -1
View File
@@ -68,7 +68,7 @@ humantime = "2.1.0"
keyring = { version = "3.6.1", features = ["apple-native", "windows-native", "sync-secret-service"] } keyring = { version = "3.6.1", features = ["apple-native", "windows-native", "sync-secret-service"] }
lock = { path = "./common/lock" } lock = { path = "./common/lock" }
lazy_static = "1.5.0" lazy_static = "1.5.0"
libsystemd = "0.7.0" libsystemd = { version = "0.7" }
local-ip-address = "0.6.3" local-ip-address = "0.6.3"
matchit = "0.8.4" matchit = "0.8.4"
md-5 = "0.10.6" md-5 = "0.10.6"
+4 -1
View File
@@ -43,7 +43,7 @@ http.workspace = true
http-body.workspace = true http-body.workspace = true
iam = { path = "../iam" } iam = { path = "../iam" }
jsonwebtoken = "9.3.0" jsonwebtoken = "9.3.0"
libsystemd = { workspace = true } libsystemd = { workspace = true, optional = true }
lock.workspace = true lock.workspace = true
local-ip-address = { workspace = true } local-ip-address = { workspace = true }
matchit = { workspace = true } matchit = { workspace = true }
@@ -86,6 +86,9 @@ tower-http.workspace = true
url.workspace = true url.workspace = true
uuid = "1.15.1" uuid = "1.15.1"
[target.'cfg(target_os = "linux")'.dependencies]
libsystemd = "0.7"
[build-dependencies] [build-dependencies]
prost-build.workspace = true prost-build.workspace = true
tonic-build.workspace = true tonic-build.workspace = true
+1 -1
View File
@@ -216,7 +216,7 @@ pub async fn start_static_file_server(
let app = Router::new() let app = Router::new()
.route("/license", get(license_handler)) .route("/license", get(license_handler))
.route("/config.json", get(config_handler)) .route("/config.json", get(config_handler))
.nest_service("/", get(static_handler)); .fallback_service(get(static_handler));
let local_addr: SocketAddr = addrs.parse().expect("Failed to parse socket address"); let local_addr: SocketAddr = addrs.parse().expect("Failed to parse socket address");
info!("WebUI: http://{}:{} http://127.0.0.1:{}", local_ip, local_addr.port(), local_addr.port()); info!("WebUI: http://{}:{} http://127.0.0.1:{}", local_ip, local_addr.port(), local_addr.port());
info!(" RootUser: {}", access_key); info!(" RootUser: {}", access_key);
+39 -2
View File
@@ -53,6 +53,27 @@ use tracing::{debug, error, info, info_span, warn};
use tracing_error::ErrorLayer; use tracing_error::ErrorLayer;
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt}; use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
#[cfg(target_os = "linux")]
fn notify_systemd(state: &str) {
use libsystemd::daemon::{notify, NotifyState};
let notify_state = match state {
"ready" => NotifyState::Ready,
"stopping" => NotifyState::Stopping,
_ => return,
};
if let Err(e) = notify(false, &[notify_state]) {
error!("Failed to notify systemd: {}", e);
} else {
debug!("Successfully notified systemd: {}", state);
}
}
#[cfg(not(target_os = "linux"))]
fn notify_systemd(state: &str) {
debug!("Systemd notifications are not available on this platform (state: {})", state);
}
#[allow(dead_code)] #[allow(dead_code)]
fn setup_tracing() { fn setup_tracing() {
use tracing_subscriber::EnvFilter; use tracing_subscriber::EnvFilter;
@@ -260,6 +281,9 @@ async fn run(opt: config::Opt) -> Result<()> {
None None
}; };
// Create a oneshot channel to wait for the service to start
let (tx, rx) = tokio::sync::oneshot::channel();
tokio::spawn(async move { tokio::spawn(async move {
let hyper_service = service.into_shared(); let hyper_service = service.into_shared();
let hybrid_service = TowerToHyperService::new( let hybrid_service = TowerToHyperService::new(
@@ -272,6 +296,10 @@ async fn run(opt: config::Opt) -> Result<()> {
let mut ctrl_c = std::pin::pin!(tokio::signal::ctrl_c()); let mut ctrl_c = std::pin::pin!(tokio::signal::ctrl_c());
let graceful = hyper_util::server::graceful::GracefulShutdown::new(); let graceful = hyper_util::server::graceful::GracefulShutdown::new();
debug!("graceful initiated"); debug!("graceful initiated");
// Send a message to the main thread to indicate that the server has started
let _ = tx.send(());
loop { loop {
debug!("waiting for SIGINT or SIGTERM has_tls_certs: {}", has_tls_certs); debug!("waiting for SIGINT or SIGTERM has_tls_certs: {}", has_tls_certs);
// Wait for a connection // Wait for a connection
@@ -286,9 +314,12 @@ async fn run(opt: config::Opt) -> Result<()> {
} }
} }
_ = ctrl_c.as_mut() => { _ = ctrl_c.as_mut() => {
drop(listener);
eprintln!("Ctrl-C received, starting shutdown");
break; break;
} }
}; };
if has_tls_certs { if has_tls_certs {
debug!("TLS certificates found, starting with SIGINT"); debug!("TLS certificates found, starting with SIGINT");
let tls_socket = match tls_acceptor let tls_socket = match tls_acceptor
@@ -353,7 +384,7 @@ async fn run(opt: config::Opt) -> Result<()> {
})?; })?;
debug!("init store success!"); debug!("init store success!");
init_iam_sys(store.clone()).await.unwrap(); init_iam_sys(store.clone()).await?;
new_global_notification_sys(endpoint_pools.clone()).await.map_err(|err| { new_global_notification_sys(endpoint_pools.clone()).await.map_err(|err| {
error!("new_global_notification_sys failed {:?}", &err); error!("new_global_notification_sys failed {:?}", &err);
@@ -386,9 +417,15 @@ async fn run(opt: config::Opt) -> Result<()> {
}); });
} }
// Wait for the HTTP service to finish starting
if rx.await.is_ok() {
notify_systemd("ready");
}
tokio::select! { tokio::select! {
_ = tokio::signal::ctrl_c() => { _ = tokio::signal::ctrl_c() => {
eprintln!("Ctrl-C received, starting shutdown");
notify_systemd("stopping");
} }
} }