use crate::common::config::Config;
use tokio::sync::mpsc;
use tracing::info;
use crate::global;
#[cfg(feature = "sock")]
pub mod sock;
#[cfg(feature = "socket")]
pub mod socket;
#[cfg(feature = "http")]
pub mod http;
pub async fn start(config: Config, load: bool) {
if load {
if let Some(path) = config.watchmen.cache.clone() {
global::set_cache(path.clone()).await;
match global::load(&path).await {
Ok(_) => {
info!("Cache tasks loaded.");
println!("Cache tasks loaded.");
}
Err(e) => {
info!("Cache tasks load failed: {}", e);
println!("Cache tasks load failed: {}", e);
}
}
} else {
info!("Cache tasks load failed: cache file not set in config.");
println!("Cache tasks load failed: cache file not set in config.");
}
}
let (tx, mut rx) = mpsc::channel::<i32>(12);
let tx_ctrl_c = tx.clone(); let tx_ctrl_d = tx.clone();
let s_ctrl_c = tokio::spawn(async move {
tokio::signal::ctrl_c().await.unwrap();
tx_ctrl_c.send(9).await.unwrap();
});
let s_ctrl_d = tokio::spawn(async move {
tokio::signal::unix::signal(tokio::signal::unix::SignalKind::terminate())
.unwrap()
.recv()
.await;
tx_ctrl_d.send(15).await.unwrap();
});
#[cfg(feature = "sock")]
let joinhandle_sock = if config.watchmen.engines.contains(&"sock".to_string()) {
info!("Starting sock...");
println!("Starting sock...");
Some(sock::start(config.clone()).await)
} else {
None
};
#[cfg(feature = "socket")]
let joinhandle_socket = if config.watchmen.engines.contains(&"socket".to_string()) {
info!("Starting socket...");
println!("Starting socket...");
Some(socket::start(config.clone()).await)
} else {
None
};
#[cfg(feature = "http")]
let joinhandle_http = if config.watchmen.engines.contains(&"http".to_string()) {
info!("Starting http...");
println!("Starting http...");
Some(http::start(config.clone()).await)
} else {
None
};
info!("All engines started.");
println!("All engines started.");
let _res = rx.recv().await;
s_ctrl_c.abort();
s_ctrl_d.abort();
println!("Shutting down...");
#[cfg(feature = "sock")]
if config.watchmen.engines.contains(&"sock".to_string()) && joinhandle_sock.is_some() {
joinhandle_sock.unwrap().abort();
}
#[cfg(feature = "socket")]
if config.watchmen.engines.contains(&"socket".to_string()) && joinhandle_socket.is_some() {
joinhandle_socket.unwrap().abort();
}
#[cfg(feature = "http")]
if config.watchmen.engines.contains(&"http".to_string()) && joinhandle_http.is_some() {
joinhandle_http.unwrap().abort();
}
}