ai_agent/utils/
process.rs1use crate::constants::env::system;
5use std::collections::HashMap;
6use std::process::{Command, Stdio};
7
8pub fn get_process_id() -> u32 {
10 std::process::id()
11}
12
13pub fn get_process_info() -> HashMap<String, String> {
15 let mut info = HashMap::new();
16 info.insert("pid".to_string(), get_process_id().to_string());
17
18 if let Ok(cwd) = std::env::current_dir() {
20 info.insert("cwd".to_string(), cwd.to_string_lossy().to_string());
21 }
22
23 if let Ok(exe) = std::env::current_exe() {
24 info.insert("exe".to_string(), exe.to_string_lossy().to_string());
25 }
26
27 info
28}
29
30pub fn is_running_in_container() -> bool {
32 std::env::var(system::DOCKER_CONTAINER).is_ok()
34 || std::env::var(system::KUBERNETES_SERVICE_HOST).is_ok()
35 || std::path::Path::new("/.dockerenv").exists()
36}
37
38pub fn get_parent_process_id() -> Option<u32> {
40 #[cfg(unix)]
43 {
44 use std::os::unix::process::CommandExt;
45 None
47 }
48 #[cfg(not(unix))]
49 {
50 None
51 }
52}
53
54pub fn run_command(cmd: &str, args: &[&str]) -> Result<String, String> {
56 let output = Command::new(cmd)
57 .args(args)
58 .stdout(Stdio::piped())
59 .stderr(Stdio::piped())
60 .output()
61 .map_err(|e| e.to_string())?;
62
63 if output.status.success() {
64 Ok(String::from_utf8_lossy(&output.stdout).to_string())
65 } else {
66 Err(String::from_utf8_lossy(&output.stderr).to_string())
67 }
68}