smart-house-web: в работе

This commit is contained in:
3 changed files with 41 additions and 12 deletions

View File

@@ -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"] }

View File

@@ -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");
}
}

View File

@@ -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 => {},
}
}