From b11ade277ed7f3c9cf1a42305fbb16680c054362 Mon Sep 17 00:00:00 2001 From: ReeseHatfield Date: Sat, 5 Sep 2026 09:18:14 -0400 Subject: [PATCH] refactor to use atomic bool and polling speeds up check by about 9 seconds or so --- src/cmd/check.rs | 56 ++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 47 insertions(+), 9 deletions(-) diff --git a/src/cmd/check.rs b/src/cmd/check.rs index 532fb89..17fb832 100644 --- a/src/cmd/check.rs +++ b/src/cmd/check.rs @@ -1,4 +1,10 @@ -use std::time::Duration; +use std::{ + sync::{ + atomic::{AtomicBool, Ordering}, + Arc, + }, + time::Duration, +}; use clap::Args; use reqwest::StatusCode; @@ -8,6 +14,7 @@ use crate::config; use crate::net::{self, kill_port}; + /// Options for the `oseda check` command #[derive(Args, Debug)] pub struct CheckOptions { @@ -91,14 +98,38 @@ fn verify_project(port_num: u16) -> OsedaProjectStatus { Err(err) => return OsedaProjectStatus::NotDeploymentReady(err), }; - let _run_handle = std::thread::spawn(run::run); + let shutdown_flag = Arc::new(AtomicBool::new(false)); + let shutdown_flag_clone = shutdown_flag.clone(); - std::thread::sleep(Duration::from_millis(10000)); + // use shutdown hook and kill once polled as alive + let run_handle = std::thread::spawn(move || { + let _ = run::run_with_shutdown(shutdown_flag_clone); + }); let addr = format!("http://localhost:{}", port_num); - let status = match net::get_status(&addr) { - Ok(status) => status, - Err(_) => { + let mut status = None; + + // poll oseda run process at: + let max_polls = 100; + let poll_delay = Duration::from_millis(200); + + for i in 0..max_polls { + println!("polled {}", i); + if let Ok(res_status) = net::get_status(&addr) { + if res_status == StatusCode::OK { + status = Some(res_status); + break; + } + } + std::thread::sleep(poll_delay); + } + + let status = match status { + Some(status) => status, + None => { + // if could not get status, ensure process dies + shutdown_flag.store(true, Ordering::SeqCst); + let _ = run_handle.join(); return OsedaProjectStatus::NotDeploymentReady( OsedaCheckError::CouldNotPingLocalPresentation( "Could not ping presentation".to_owned(), @@ -108,6 +139,9 @@ fn verify_project(port_num: u16) -> OsedaProjectStatus { }; if status != StatusCode::OK { + // send shutdown flag, but happy about it this time + shutdown_flag.store(true, Ordering::SeqCst); + let _ = run_handle.join(); return OsedaProjectStatus::NotDeploymentReady( OsedaCheckError::CouldNotPingLocalPresentation( "Presentation returned non 200 error status code".to_owned(), @@ -124,11 +158,15 @@ fn verify_project(port_num: u16) -> OsedaProjectStatus { // would also get rid of the mpsc stuff going on in run(), but honestly // im just not that familiar with the mpsc pattern and rust api - if kill_port(port_num).is_err() { - println!("Warning: could not kill process on port, project could still be running"); + // shutdown other process + shutdown_flag.store(true, Ordering::SeqCst); + if run_handle.join().is_err() { + if kill_port(port_num).is_err() { + println!("Warning: could not kill process on port, project could still be running"); + } } else { println!("Project process sucessfully terminated"); } OsedaProjectStatus::DeployReady -} +} \ No newline at end of file