From b88ede75017f077eea6a7985d4c7ab20b374e3c2 Mon Sep 17 00:00:00 2001 From: Alexander Baranov Date: Fri, 8 May 2026 22:47:30 +0300 Subject: [PATCH] =?UTF-8?q?smart-house-web:=20=D0=B2=20=D1=80=D0=B0=D0=B1?= =?UTF-8?q?=D0=BE=D1=82=D0=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- smart-house-web/backend/Cargo.toml | 2 +- smart-house-web/backend/src/lib.rs | 21 +++++++++++++------ smart-house-web/backend/src/server.rs | 30 ++++++++++++++++++++++----- 3 files changed, 41 insertions(+), 12 deletions(-) diff --git a/smart-house-web/backend/Cargo.toml b/smart-house-web/backend/Cargo.toml index fcb0467..de16104 100644 --- a/smart-house-web/backend/Cargo.toml +++ b/smart-house-web/backend/Cargo.toml @@ -7,4 +7,4 @@ edition = "2024" tracing = "0.1" tracing-subscriber = "0.3" axum = "0.8" -tokio = { version = "1.52", features = ["rt", "rt-multi-thread", "signal"] } +tokio = { version = "1.52", features = ["rt", "rt-multi-thread", "signal", "time"] } diff --git a/smart-house-web/backend/src/lib.rs b/smart-house-web/backend/src/lib.rs index b24503e..c010ebd 100644 --- a/smart-house-web/backend/src/lib.rs +++ b/smart-house-web/backend/src/lib.rs @@ -5,19 +5,28 @@ const CODE_STARTIG_SERVER_ERROR: i32 = 4; const CODE_CTRL_C_SIGNAL_INSTALL_ERROR: i32 = 5; pub fn init_logger() { - use tracing_subscriber::{Layer, layer::SubscriberExt, util::SubscriberInitExt}; + use std::process::exit; + use tracing::{Level, trace}; + use tracing_subscriber::{ + Layer, filter::Targets, fmt::layer, layer::SubscriberExt, registry, util::SubscriberInitExt, + }; - let layer = tracing_subscriber::fmt::layer() + let layer = layer() + .compact() .with_thread_names(true) .with_file(false) .with_line_number(false) - .compact() + .with_filter( + Targets::new() + .with_target("axum::serve", Level::INFO) + .with_default(Level::TRACE), + ) .boxed(); - if let Err(e) = tracing_subscriber::registry().with(vec![layer]).try_init() { + if let Err(e) = registry().with(vec![layer]).try_init() { eprintln!("Logger initialization failed: {:?}", e); - std::process::exit(CODE_LOGGER_INITIALIZATION_ERROR); + exit(CODE_LOGGER_INITIALIZATION_ERROR); } else { - tracing::trace!("Logger succesfully initialized"); + trace!("Logger succesfully initialized"); } } diff --git a/smart-house-web/backend/src/server.rs b/smart-house-web/backend/src/server.rs index e4695bc..7b67ac5 100644 --- a/smart-house-web/backend/src/server.rs +++ b/smart-house-web/backend/src/server.rs @@ -1,9 +1,19 @@ -use std::process::exit; +use std::{ + process::exit, + sync::atomic::{AtomicUsize, Ordering}, +}; use tracing::{error, info}; pub fn run_server() { let runtime = match tokio::runtime::Builder::new_multi_thread() + .name("tokio") + .thread_name_fn(|| { + static LAST_ID: AtomicUsize = AtomicUsize::new(0); + let id = LAST_ID.fetch_add(1, Ordering::SeqCst); + format!("tkwr-{id}") + }) .worker_threads(2) + .thread_stack_size(256 * 1024) .enable_all() .build() { @@ -41,11 +51,21 @@ async fn fallback() -> axum::response::Response { } async fn shutdown_signal() { - let ctrl_c = match tokio::signal::ctrl_c().await { - Ok(signal) => signal, - Err(e) => { + // let timeout = async { + // tokio::time::sleep(std::time::Duration::from_secs(10)).await; + // info!("10 seconds timeout expired"); + // }; + let ctrl_c = async { + tokio::signal::ctrl_c().await.map_err(|e| { error!("Can't install Ctrl+C signal handler: {:?}", e); exit(crate::CODE_CTRL_C_SIGNAL_INSTALL_ERROR); - } + }); + info!("Ctrl+C pressed"); }; + let pending = std::future::pending::<()>(); + tokio::select! { + // _ = timeout => {}, + _ = ctrl_c => {}, + _ = pending => {}, + } }