smart-house-web: в работе
This commit is contained in:
@@ -7,4 +7,4 @@ edition = "2024"
|
|||||||
tracing = "0.1"
|
tracing = "0.1"
|
||||||
tracing-subscriber = "0.3"
|
tracing-subscriber = "0.3"
|
||||||
axum = "0.8"
|
axum = "0.8"
|
||||||
tokio = { version = "1.52", features = ["rt", "rt-multi-thread", "signal"] }
|
tokio = { version = "1.52", features = ["rt", "rt-multi-thread", "signal", "time"] }
|
||||||
|
|||||||
@@ -5,19 +5,28 @@ const CODE_STARTIG_SERVER_ERROR: i32 = 4;
|
|||||||
const CODE_CTRL_C_SIGNAL_INSTALL_ERROR: i32 = 5;
|
const CODE_CTRL_C_SIGNAL_INSTALL_ERROR: i32 = 5;
|
||||||
|
|
||||||
pub fn init_logger() {
|
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_thread_names(true)
|
||||||
.with_file(false)
|
.with_file(false)
|
||||||
.with_line_number(false)
|
.with_line_number(false)
|
||||||
.compact()
|
.with_filter(
|
||||||
|
Targets::new()
|
||||||
|
.with_target("axum::serve", Level::INFO)
|
||||||
|
.with_default(Level::TRACE),
|
||||||
|
)
|
||||||
.boxed();
|
.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);
|
eprintln!("Logger initialization failed: {:?}", e);
|
||||||
std::process::exit(CODE_LOGGER_INITIALIZATION_ERROR);
|
exit(CODE_LOGGER_INITIALIZATION_ERROR);
|
||||||
} else {
|
} else {
|
||||||
tracing::trace!("Logger succesfully initialized");
|
trace!("Logger succesfully initialized");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,9 +1,19 @@
|
|||||||
use std::process::exit;
|
use std::{
|
||||||
|
process::exit,
|
||||||
|
sync::atomic::{AtomicUsize, Ordering},
|
||||||
|
};
|
||||||
use tracing::{error, info};
|
use tracing::{error, info};
|
||||||
|
|
||||||
pub fn run_server() {
|
pub fn run_server() {
|
||||||
let runtime = match tokio::runtime::Builder::new_multi_thread()
|
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)
|
.worker_threads(2)
|
||||||
|
.thread_stack_size(256 * 1024)
|
||||||
.enable_all()
|
.enable_all()
|
||||||
.build()
|
.build()
|
||||||
{
|
{
|
||||||
@@ -41,11 +51,21 @@ async fn fallback() -> axum::response::Response {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async fn shutdown_signal() {
|
async fn shutdown_signal() {
|
||||||
let ctrl_c = match tokio::signal::ctrl_c().await {
|
// let timeout = async {
|
||||||
Ok(signal) => signal,
|
// tokio::time::sleep(std::time::Duration::from_secs(10)).await;
|
||||||
Err(e) => {
|
// 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);
|
error!("Can't install Ctrl+C signal handler: {:?}", e);
|
||||||
exit(crate::CODE_CTRL_C_SIGNAL_INSTALL_ERROR);
|
exit(crate::CODE_CTRL_C_SIGNAL_INSTALL_ERROR);
|
||||||
}
|
});
|
||||||
|
info!("Ctrl+C pressed");
|
||||||
};
|
};
|
||||||
|
let pending = std::future::pending::<()>();
|
||||||
|
tokio::select! {
|
||||||
|
// _ = timeout => {},
|
||||||
|
_ = ctrl_c => {},
|
||||||
|
_ = pending => {},
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user