Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 47 additions & 9 deletions src/cmd/check.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
use std::time::Duration;
use std::{
sync::{
atomic::{AtomicBool, Ordering},
Arc,
},
time::Duration,
};

use clap::Args;
use reqwest::StatusCode;
Expand All @@ -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 {
Expand Down Expand Up @@ -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(),
Expand All @@ -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(),
Expand All @@ -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
}
}
Loading