Shutdown the server more gracefully

This commit is contained in:
Demmie 2023-06-05 00:45:38 -04:00
parent 1c42466cbb
commit 63b8079aa8
No known key found for this signature in database
GPG Key ID: B06DAA3D432C6E9A
7 changed files with 53 additions and 15 deletions

2
Cargo.lock generated
View File

@ -3775,7 +3775,7 @@ checksum = "0046fef7e28c3804e5e38bfa31ea2a0f73905319b677e57ebe37e49358989b5d"
[[package]] [[package]]
name = "watchers" name = "watchers"
version = "0.0.1" version = "0.1.0"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"aw-client-rust", "aw-client-rust",

View File

@ -1,6 +1,6 @@
[package] [package]
name = "awatcher" name = "awatcher"
version = "0.1.0" version = { workspace = true }
authors = ["Demmie <2e3s19@gmail.com>"] authors = ["Demmie <2e3s19@gmail.com>"]
edition = "2021" edition = "2021"
license-file = "LICENSE" license-file = "LICENSE"
@ -16,6 +16,9 @@ image = { version = "0.24.6" }
[workspace] [workspace]
members = ["watchers"] members = ["watchers"]
[workspace.package]
version = "0.1.0"
[workspace.dependencies] [workspace.dependencies]
anyhow = "1.0.70" anyhow = "1.0.70"
log = { version = "0.4.17", features = ["std"] } log = { version = "0.4.17", features = ["std"] }

View File

@ -2,19 +2,23 @@ mod menu;
mod server; mod server;
pub use menu::Tray; pub use menu::Tray;
use std::path::PathBuf; use std::{
path::PathBuf,
sync::{atomic::AtomicBool, Arc},
};
use watchers::config::Config; use watchers::config::Config;
pub fn run(config: &Config, config_file: PathBuf, no_tray: bool) { pub fn run(config: &Config, config_file: PathBuf, no_tray: bool, is_stopped: Arc<AtomicBool>) {
if !no_tray { if !no_tray {
let service = ksni::TrayService::new(Tray { let service = ksni::TrayService::new(Tray {
server_host: config.host.clone(), server_host: config.host.clone(),
server_port: config.port, server_port: config.port,
config_file, config_file,
is_stopped: Arc::clone(&is_stopped),
}); });
service.spawn(); service.spawn();
} }
let port = config.port; let port = config.port;
server::run(port); server::run(port, is_stopped);
} }

View File

@ -1,10 +1,17 @@
use std::path::PathBuf; use std::{
path::PathBuf,
sync::{
atomic::{AtomicBool, Ordering},
Arc,
},
};
#[derive(Debug)] #[derive(Debug)]
pub struct Tray { pub struct Tray {
pub server_host: String, pub server_host: String,
pub server_port: u32, pub server_port: u32,
pub config_file: PathBuf, pub config_file: PathBuf,
pub is_stopped: Arc<AtomicBool>,
} }
impl ksni::Tray for Tray { impl ksni::Tray for Tray {
@ -51,9 +58,13 @@ impl ksni::Tray for Tray {
ksni::menu::StandardItem { ksni::menu::StandardItem {
label: "Exit".into(), label: "Exit".into(),
icon_name: "application-exit".into(), icon_name: "application-exit".into(),
activate: Box::new(|_| { activate: {
std::process::exit(0); let is_stopped = Arc::clone(&self.is_stopped);
}),
Box::new(move |_| {
is_stopped.store(true, Ordering::Relaxed);
})
},
..Default::default() ..Default::default()
} }
.into(), .into(),

View File

@ -1,8 +1,12 @@
use anyhow::anyhow; use anyhow::anyhow;
use aw_server::endpoints::{build_rocket, embed_asset_resolver}; use aw_server::endpoints::{build_rocket, embed_asset_resolver};
use std::sync::Mutex; use std::sync::{
atomic::{AtomicBool, Ordering},
Arc, Mutex,
};
use tokio::time::{sleep, Duration};
pub fn run(port: u32) { pub fn run(port: u32, is_stopped: Arc<AtomicBool>) {
std::thread::spawn(move || { std::thread::spawn(move || {
let db_path = aw_server::dirs::db_path(false) let db_path = aw_server::dirs::db_path(false)
.map_err(|_| anyhow!("DB path is not found")) .map_err(|_| anyhow!("DB path is not found"))
@ -21,12 +25,28 @@ pub fn run(port: u32) {
asset_resolver: embed_asset_resolver!("$AW_WEBUI_DIST"), asset_resolver: embed_asset_resolver!("$AW_WEBUI_DIST"),
device_id, device_id,
}; };
let server = build_rocket(server_state, config).launch();
let check = async {
loop {
if is_stopped.load(Ordering::Relaxed) {
warn!("Received an exit signal, stopping the server");
break;
}
sleep(Duration::from_secs(1)).await;
}
};
tokio::runtime::Builder::new_current_thread() tokio::runtime::Builder::new_current_thread()
.enable_all() .enable_all()
.build() .build()
.unwrap() .unwrap()
.block_on(build_rocket(server_state, config).launch()) .block_on(async {
.unwrap(); tokio::select! (
r = server => {r.unwrap();},
_ = check => {},
);
});
}); });
} }

View File

@ -41,7 +41,7 @@ fn main() -> anyhow::Result<()> {
); );
#[cfg(feature = "bundle")] #[cfg(feature = "bundle")]
bundle::run(&config, config_file, no_tray); bundle::run(&config, config_file, no_tray, Arc::clone(&is_stopped));
let client = ReportClient::new(config)?; let client = ReportClient::new(config)?;
let client = Arc::new(client); let client = Arc::new(client);

View File

@ -1,6 +1,6 @@
[package] [package]
name = "watchers" name = "watchers"
version = "0.0.1" version = { workspace = true }
authors = ["Demmie <2e3s19@gmail.com>"] authors = ["Demmie <2e3s19@gmail.com>"]
edition = "2021" edition = "2021"