summary refs log tree commit diff
path: root/src/main.rs
diff options
context:
space:
mode:
authorAshelyn Rose <git@ashen.earth>2024-10-01 01:12:28 -0600
committerAshelyn Rose <git@ashen.earth>2024-10-01 01:12:28 -0600
commitf87e9727e69e981e4acff31a779e29a35637b964 (patch)
tree860cf52703f266f0aded4af6adeac7139ec918fe /src/main.rs
parent7c3f983bc830a85641e8a38545dfb9220570c5a2 (diff)
Adjust system spawn so we're not intentionally doubling threads
Diffstat (limited to 'src/main.rs')
-rw-r--r--src/main.rs35
1 files changed, 16 insertions, 19 deletions
diff --git a/src/main.rs b/src/main.rs
index 41f150b..76a69af 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -2,7 +2,7 @@ mod config;
 mod system;
 mod listener;
 
-use std::{fs, thread};
+use std::{fs, panic};
 use tokio::{runtime, task::JoinSet};
 
 fn main() {
@@ -14,12 +14,7 @@ fn main() {
     let mut waiters = JoinSet::new();
 
     for (system_name, system_config) in config.systems.into_iter() {
-        let system_handle = spawn_system(&system_name, system_config);
-
-        waiters.spawn_blocking_on(move || -> String {
-            system_handle.join();
-            system_name
-        }, waiter_pool.handle());
+        spawn_system(&mut waiters, &waiter_pool, &system_name, system_config);
     }
 
     runtime::Builder::new_current_thread().build().unwrap().block_on(async {
@@ -37,30 +32,32 @@ fn main() {
                 let updated_config = config::Config::load(config_file);
 
                 if let Some((_, system_config)) = updated_config.systems.into_iter().find(|(name, _)| name.eq(&system_name)) {
-                    let system_handle = spawn_system(&system_name, system_config.clone());
-                    
-                    waiters.spawn_blocking_on(move || -> String {
-                        system_handle.join();
-                        system_name
-                    }, waiter_pool.handle());
+                    spawn_system(&mut waiters, &waiter_pool, &system_name, system_config.clone());
                 } else {
                     println!("New config file but this system no longer exists, exiting.");
                 }
+            } else {
+                println!("Thread panicked");
             }
         }
     })
 }
 
-fn spawn_system(system_name : &String, system_config: config::System) -> thread::JoinHandle<()> {
+fn spawn_system(joinset: &mut JoinSet<String>, pool: &tokio::runtime::Runtime, system_name : &String, system_config: config::System) {
     let name = system_name.clone();
-    thread::spawn(move || {
+    let config = system_config.clone();
+    joinset.spawn_blocking_on(move || -> String {
         let thread_local_runtime = runtime::Builder::new_current_thread().enable_all().build().unwrap();
 
-        thread_local_runtime.block_on(async {
-            let mut system = system::System::new(name, system_config);
-            system.start_clients().await;
+        let _ = panic::catch_unwind(|| {
+            thread_local_runtime.block_on(async {
+                let mut system = system::System::new(name.clone(), config);
+                system.start_clients().await;
+            });
         });
-    })
+
+        name
+    }, pool.handle());
 }