use std::process::Command;
#[macro_use]
extern crate wei_log;
pub async fn start() -> Result<(), Box<dyn std::error::Error>> {
loop {
if wei_env::status() == "0" {
return Ok(());
}
let content = std::fs::read_to_string("./daemon.dat")?;
let map: serde_yaml::Value = serde_yaml::from_str(&content)?;
if let serde_yaml::Value::Mapping(m) = map.clone() {
for (k, _) in m {
let data = k.clone();
tokio::task::spawn( async move {
let name = data.as_str().expect("process is not string");
if !is_process_running(name.clone()) {
info!("{} is not running", name);
wei_run::run(name, Vec::new()).unwrap();
}
});
}
}
tokio::time::sleep(tokio::time::Duration::from_secs(60)).await;
}
}
pub fn is_process_running(process_name: &str) -> bool {
let output = if cfg!(target_os = "windows") {
Command::new("powershell")
.arg("-Command")
.arg(format!("Get-Process -Name {} -ErrorAction SilentlyContinue", process_name))
.output()
.expect("Failed to execute command")
} else {
Command::new("bash")
.arg("-c")
.arg(format!("pgrep -f {}", process_name))
.output()
.expect("Failed to execute command")
};
!output.stdout.is_empty()
}
#[cfg(target_os = "windows")]
pub fn hide() -> Result<(), Box<dyn std::error::Error>> {
if !is_debug()? {
let window = unsafe { winapi::um::wincon::GetConsoleWindow() };
if window != std::ptr::null_mut() {
unsafe {
winapi::um::winuser::ShowWindow(window, winapi::um::winuser::SW_HIDE);
}
}
}
Ok(())
}
pub fn is_debug() -> Result<bool, Box<dyn std::error::Error>> {
let home_dir = std::env::var("USERPROFILE")?;
if std::path::Path::new(&home_dir).join("AppData\\Local\\Ai\\debug.dat").exists() {
return Ok(true);
}
return Ok(false);
}