Skip to content

Commit

Permalink
use tokio::process and select! instead of native process and loop
Browse files Browse the repository at this point in the history
  • Loading branch information
qihexiang committed Oct 3, 2022
1 parent c96f642 commit 3cf8d76
Showing 1 changed file with 17 additions and 16 deletions.
33 changes: 17 additions & 16 deletions src/bin/srun.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use srun::config::Config;
use std::{env::current_exe, path::PathBuf, process::Command, time::Duration};
use tokio::time::sleep;
use std::{env::current_exe, path::PathBuf, time::Duration};
use tokio::{time::sleep, process::Command, select};

#[tokio::main]
async fn main() {
Expand All @@ -15,22 +15,23 @@ async fn main() {
let mut core = Command::new(core_path)
.spawn()
.expect("Failed to start srun login core");

loop {
let driver_status = driver.try_wait().expect("Can't get status of webdriver");
let core_status = core.try_wait().expect("Cat get status of srun-core");
if let Some(_driver_status) = driver_status {
if let None = core_status {
core.kill().expect("Failed to kill srun-core, may need to kill manually.");
select! {
status = core.wait() => {
if let Ok(status) = status {
println!("srun-core was killed. status code: {}", status);
} else {
println!("Losing control of srun-core");
}
break ();
} else {
if let Some(_core_status) = core_status {
driver.kill().expect("Failed to kill webdriver, may need to kill it manually.");
break ();
driver.kill().await.expect("Failed to kill driver process.")
}
status = driver.wait() => {
if let Ok(status) = status {
println!("webdriver was killed. status code: {}", status);
} else {
println!("Losing control of webdriver");
}
core.kill().await.expect("Failed to kill srun-core process.")
}
sleep(Duration::from_millis(10000)).await;
}
}

Expand All @@ -44,5 +45,5 @@ fn current_dir_file(filename: &str) -> PathBuf {
current_dir.push(filename);
#[cfg(windows)]
current_dir.set_extension("exe");
current_dir.iter().collect()
current_dir
}

0 comments on commit 3cf8d76

Please sign in to comment.