better shutdown, still not perfect
This commit is contained in:
parent
342b634388
commit
c02e4bc879
6 changed files with 32 additions and 23 deletions
2
Cargo.lock
generated
2
Cargo.lock
generated
|
@ -2158,9 +2158,7 @@ dependencies = [
|
||||||
"io-uring",
|
"io-uring",
|
||||||
"libc",
|
"libc",
|
||||||
"mio",
|
"mio",
|
||||||
"parking_lot",
|
|
||||||
"pin-project-lite",
|
"pin-project-lite",
|
||||||
"signal-hook-registry",
|
|
||||||
"slab",
|
"slab",
|
||||||
"socket2",
|
"socket2",
|
||||||
"tokio-macros",
|
"tokio-macros",
|
||||||
|
|
|
@ -19,5 +19,5 @@ serde = { version = "1", features = ["derive"] }
|
||||||
serde_json = "1"
|
serde_json = "1"
|
||||||
sha256 = "1.6"
|
sha256 = "1.6"
|
||||||
thiserror = "2"
|
thiserror = "2"
|
||||||
tokio = { version = "1", features = ["full", "macros", "rt-multi-thread"] }
|
tokio = { version = "1", default-features = false, features = ["time", "macros", "rt-multi-thread"] }
|
||||||
tower-http = { version = "0.6", features = ["limit"] }
|
tower-http = { version = "0.6", features = ["limit"] }
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
use std::{
|
use std::{
|
||||||
net::{SocketAddr, SocketAddrV4},
|
net::{SocketAddr, SocketAddrV4},
|
||||||
sync::Arc,
|
sync::Arc,
|
||||||
|
time::Duration,
|
||||||
};
|
};
|
||||||
|
|
||||||
use axum::{
|
use axum::{
|
||||||
|
@ -27,21 +28,30 @@ impl JoecalState {
|
||||||
println!("Socket local addr: {:?}", self.socket.local_addr()?);
|
println!("Socket local addr: {:?}", self.socket.local_addr()?);
|
||||||
println!("Listening on multicast addr: {}", config.multicast_addr);
|
println!("Listening on multicast addr: {}", config.multicast_addr);
|
||||||
|
|
||||||
|
let mut timeout = tokio::time::interval(Duration::from_secs(5));
|
||||||
|
timeout.tick().await;
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
match self.socket.recv_from(&mut buf).await {
|
tokio::select! {
|
||||||
Ok((size, src)) => {
|
_ = timeout.tick() => {
|
||||||
let received_msg = String::from_utf8_lossy(&buf[..size]);
|
if let Ok(state) = self.running_state.try_lock()
|
||||||
self.process_device(&received_msg, src, config).await;
|
&& *state == RunningState::Stopping
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
r = self.socket.recv_from(&mut buf) => {
|
||||||
|
match r {
|
||||||
|
Ok((size, src)) => {
|
||||||
|
let received_msg = String::from_utf8_lossy(&buf[..size]);
|
||||||
|
self.process_device(&received_msg, src, config).await;
|
||||||
|
}
|
||||||
|
Err(e) => {
|
||||||
|
eprintln!("Error receiving message: {e}");
|
||||||
|
tokio::time::sleep(tokio::time::Duration::from_secs(1)).await;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Err(e) => {
|
|
||||||
eprintln!("Error receiving message: {e}");
|
|
||||||
tokio::time::sleep(tokio::time::Duration::from_secs(1)).await;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if let Ok(state) = self.running_state.try_lock()
|
|
||||||
&& *state == RunningState::Stopping
|
|
||||||
{
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|
10
src/lib.rs
10
src/lib.rs
|
@ -8,7 +8,6 @@ use std::{
|
||||||
collections::HashMap,
|
collections::HashMap,
|
||||||
net::{Ipv4Addr, SocketAddr, SocketAddrV4},
|
net::{Ipv4Addr, SocketAddr, SocketAddrV4},
|
||||||
sync::Arc,
|
sync::Arc,
|
||||||
time::Duration,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
use models::Device;
|
use models::Device;
|
||||||
|
@ -85,15 +84,16 @@ impl JoecalState {
|
||||||
let announcement_handle = {
|
let announcement_handle = {
|
||||||
tokio::spawn(async move {
|
tokio::spawn(async move {
|
||||||
loop {
|
loop {
|
||||||
if let Err(e) = state.announce(None, &config).await {
|
|
||||||
eprintln!("Announcement error: {e}");
|
|
||||||
}
|
|
||||||
tokio::time::sleep(std::time::Duration::from_secs(5)).await;
|
|
||||||
if let Ok(lock) = state.running_state.try_lock()
|
if let Ok(lock) = state.running_state.try_lock()
|
||||||
&& *lock == RunningState::Stopping
|
&& *lock == RunningState::Stopping
|
||||||
{
|
{
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if let Err(e) = state.announce(None, &config).await {
|
||||||
|
eprintln!("Announcement error: {e}");
|
||||||
|
}
|
||||||
|
tokio::time::sleep(std::time::Duration::from_secs(5)).await;
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
};
|
};
|
||||||
|
|
|
@ -50,6 +50,7 @@ async fn main() -> error::Result<()> {
|
||||||
let mut terminal = ratatui::init();
|
let mut terminal = ratatui::init();
|
||||||
let result = app.run(&mut terminal).await;
|
let result = app.run(&mut terminal).await;
|
||||||
ratatui::restore();
|
ratatui::restore();
|
||||||
|
|
||||||
let _ = tokio::join!(h1, h2, h3);
|
let _ = tokio::join!(h1, h2, h3);
|
||||||
|
|
||||||
Ok(result?)
|
Ok(result?)
|
||||||
|
|
|
@ -48,8 +48,8 @@ impl FileMetadata {
|
||||||
let sha256 = Some(sha256::try_digest(path)?);
|
let sha256 = Some(sha256::try_digest(path)?);
|
||||||
|
|
||||||
let metadata = Some(FileMetadataExt {
|
let metadata = Some(FileMetadataExt {
|
||||||
modified: metadata.modified().ok().map(|t| format_datetime(t)),
|
modified: metadata.modified().ok().map(format_datetime),
|
||||||
accessed: metadata.accessed().ok().map(|t| format_datetime(t)),
|
accessed: metadata.accessed().ok().map(format_datetime),
|
||||||
});
|
});
|
||||||
|
|
||||||
Ok(FileMetadata {
|
Ok(FileMetadata {
|
||||||
|
|
Loading…
Reference in a new issue