diff options
| author | Serguey Parkhomovsky <xindigo@gmail.com> | 2026-03-21 12:30:04 -0700 |
|---|---|---|
| committer | Serguey Parkhomovsky <xindigo@gmail.com> | 2026-03-21 12:30:04 -0700 |
| commit | 12856d63d081e9b995375710503563c2aa6bb07a (patch) | |
| tree | 85997e2b0e0266fa3b0bfbed81f95cb9ddc90cae /src/main.rs | |
| parent | 5712f0469ee604eaa525594a6c5488ca24813afe (diff) | |
Remove JoinSet
Diffstat (limited to 'src/main.rs')
| -rw-r--r-- | src/main.rs | 49 |
1 files changed, 20 insertions, 29 deletions
diff --git a/src/main.rs b/src/main.rs index 929815f..89f7709 100644 --- a/src/main.rs +++ b/src/main.rs @@ -10,7 +10,6 @@ use std::process; use std::sync::Arc; use std::sync::RwLock; use std::time::Duration; -use tokio::task::{Id, JoinSet}; use tracing::{debug, error, info, warn}; type StateMap = Arc<RwLock<HashMap<String, PrinterState>>>; @@ -41,9 +40,6 @@ async fn main() { process::exit(1); }); let state: StateMap = Arc::new(RwLock::new(HashMap::new())); - let mut tasks = JoinSet::new(); - let mut task_names: HashMap<Id, String> = HashMap::new(); - for printer in config.printers { let state = state.clone(); match printer { @@ -53,9 +49,12 @@ async fn main() { api_key, } => { info!(name, host, "Found Prusa"); - let task_name = name.clone(); - let handle = tasks.spawn(poll_prusa(name, host, api_key, state)); - task_names.insert(handle.id(), task_name); + tokio::spawn(async move { + match tokio::spawn(poll_prusa(name.clone(), host, api_key, state)).await { + Ok(()) => warn!(name, "Prusa polling task exited unexpectedly"), + Err(e) => error!(name, error = ?e, "Prusa polling task panicked"), + } + }); } Printer::Bambu { name, @@ -64,32 +63,24 @@ async fn main() { access_code, } => { info!(name, host, "Found Bambu"); - let task_name = name.clone(); - let handle = tasks.spawn(poll_bambu(name, host, serial_number, access_code, state)); - task_names.insert(handle.id(), task_name); + tokio::spawn(async move { + match tokio::spawn(poll_bambu( + name.clone(), + host, + serial_number, + access_code, + state, + )) + .await + { + Ok(()) => warn!(name, "Bambu polling task exited unexpectedly"), + Err(e) => error!(name, error = ?e, "Bambu polling task panicked"), + } + }); } } } - tokio::spawn(async move { - while let Some(result) = tasks.join_next_with_id().await { - match result { - Ok((id, ())) => { - let name = task_names.get(&id).map(String::as_str).unwrap_or("unknown"); - warn!(name, "Printer polling task exited unexpectedly"); - } - Err(e) => { - let name = task_names - .get(&e.id()) - .map(String::as_str) - .unwrap_or("unknown"); - error!(name, error = ?e, "A printer polling task panicked"); - } - } - } - warn!("All printer polling tasks have exited"); - }); - let app = Router::new().route("/", get(root)).with_state(state); let listener = tokio::net::TcpListener::bind(&args.bind) |
