bugfix: get pid

pull/9/head
prplV 2024-10-30 16:28:00 +03:00
parent 0f5ef66465
commit 5fa3d2fec8
2 changed files with 16 additions and 8 deletions

View File

@ -1,21 +1,27 @@
use crate::options::structs::CustomError;
use log::{error, warn};
use std::io;
use std::process::{Command, Output};
use std::sync::Arc;
use tokio::time::Duration;
pub async fn get_pid(name: &str) -> Result<Output, std::io::Error> {
let name = Arc::new(name.to_string());
tokio::task::spawn_blocking(move || {
let res= tokio::task::spawn_blocking(move || {
Command::new("pidof")
.arg(&*name)
.output()
// .unwrap_or_else(|_| {
// error!("Failed to execute command 'pidof'");
// std::process::exit(101);
// })
})
.await?
.await?;
if let Ok(output) = res {
if output.stderr.is_empty() && output.stdout.is_empty() {
return Err(io::ErrorKind::NotFound.into());
} else {
Ok(output)
}
} else {
return Err(io::ErrorKind::NotFound.into());
}
}
// ! can be with bug !!!
// * APPROVED
@ -154,8 +160,10 @@ mod process_unittests {
async fn pidof_active_process() {
assert!(get_pid("systemd").await.is_ok());
}
// broken mechanism need to check
#[tokio::test]
async fn pidof_disabled_process() {
assert!(get_pid("invalid-process-name").await.is_ok());
assert!(get_pid("invalid-process-name").await.is_err());
}
}

View File

@ -3,4 +3,4 @@ use runner_rs::*;
#[test]
fn test() {
assert_eq!(2, 2);
}
}