summary refs log tree commit diff
path: root/src/main.rs
diff options
context:
space:
mode:
authorAshelyn Dawn <ashe@ashen.earth>2024-10-07 19:23:47 -0600
committerAshelyn Dawn <ashe@ashen.earth>2024-10-07 19:23:47 -0600
commit6442896579bcb92c76219b5d57cff6521fa1559b (patch)
tree42b889d2e88944f8f01ed979698cadeaf62ab3c7 /src/main.rs
parent26a587e223e4c91601e79f7f3a69e8b2a0cbf0b2 (diff)
Now should join on thread signal
Diffstat (limited to 'src/main.rs')
-rw-r--r--src/main.rs21
1 files changed, 13 insertions, 8 deletions
diff --git a/src/main.rs b/src/main.rs
index 2fe1cf0..5837426 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -3,23 +3,29 @@
 mod config;
 mod system;
 use system::{Manager, SystemThreadCommand};
-
-use std::{fs, thread::{self, sleep, JoinHandle}, time::Duration};
+use std::{fs, thread::{self, sleep, JoinHandle}, time::Duration, sync::mpsc};
 use tokio::runtime;
 
 fn main() {
     let initial_config = fs::read_to_string("./config.toml").expect("Could not find config file");
     let config = config::Config::load(initial_config.to_string());
 
+    let (waker, waiter) = mpsc::channel::<()>();
     let mut join_handles = Vec::<(String, JoinHandle<_>)>::new();
 
     for (system_name, system_config) in config.systems.iter() {
-        let handle = spawn_system(system_name, system_config.clone());
+        let handle = spawn_system(system_name, system_config.clone(), waker.clone());
 
         join_handles.push((system_name.clone(), handle));
     }
 
     loop {
+        // Check manually every 10 seconds just in case
+        let _ = waiter.recv_timeout(Duration::from_secs(10));
+
+        // Just to make sure the join handle is updated by the time we check
+        sleep(Duration::from_millis(100));
+
         if let Some(completed_index) = join_handles.iter().position(|(_, handle)| handle.is_finished()) {
             let (name, next_join) = join_handles.swap_remove(completed_index);
 
@@ -32,7 +38,7 @@ fn main() {
                 Ok(SystemThreadCommand::Restart) => {
                     println!("Thread for system {} requested restart", name);
                     if let Some((_, config)) = config.systems.iter().find(|(system_name, _)| name == **system_name) {
-                        let handle = spawn_system(&name, config.clone());
+                        let handle = spawn_system(&name, config.clone(), waker.clone());
                         join_handles.push((name, handle));
                     }
                 },
@@ -53,7 +59,7 @@ fn main() {
                     let updated_config = config::Config::load(config_file);
 
                     if let Some((_, system_config)) = updated_config.systems.into_iter().find(|(system_name, _)| *name == *system_name) {
-                        let handle = spawn_system(&name, system_config);
+                        let handle = spawn_system(&name, system_config, waker.clone());
                         join_handles.push((name.clone(), handle));
                     } else {
                         println!("New config file but this system no longer exists, exiting.");
@@ -63,12 +69,10 @@ fn main() {
                 Ok(SystemThreadCommand::ShutdownAll) => break,
             }
         }
-
-        sleep(Duration::from_secs(5));
     }
 }
 
-fn spawn_system(system_name : &String, system_config: config::System) -> JoinHandle<SystemThreadCommand> {
+fn spawn_system(system_name : &String, system_config: config::System, waker: mpsc::Sender<()>) -> JoinHandle<SystemThreadCommand> {
     let name = system_name.clone();
     let config = system_config.clone();
 
@@ -81,6 +85,7 @@ fn spawn_system(system_name : &String, system_config: config::System) -> JoinHan
             system.start_clients().await;
         });
 
+        let _ = waker.send(());
         SystemThreadCommand::Restart
     })
 }