hai/src/pman.rs
2025-02-20 22:18:14 +01:00

91 lines
2.6 KiB
Rust

/*
* SPDX-FileCopyrightText: 2025 April Faye John <april.john@denic.de> & Contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
use crate::PMAN_SENDER;
use anyhow::{Ok, Result};
use tokio::process::{Child, Command};
use std::sync::{Arc, Mutex};
use log::{debug, info};
use tokio::sync::mpsc;use tokio::sync::mpsc::Receiver;
pub fn init_process_manager() -> Result<()> {
let (tx, mut rx) = mpsc::channel::<ProcessCommand>(32);
let mut process_manager = ProcessManager::default();
let _ = PMAN_SENDER.set(tx);
debug!("[pman-main] Spawning pman task");
tokio::spawn(async move {
info!("[pman-task] Hello from Pman");
while process_manager.lifecycle(&mut rx).await.is_ok() {
let result = process_manager.alive_check().await;
if let Err(e) = result {
println!("{}", e);
}
}
});
Ok(())
}
#[derive(Debug)]
pub struct ProcessManager {
processes: Vec<Child>,
}
impl Default for ProcessManager {
fn default() -> ProcessManager {
ProcessManager {
processes: Vec::new(),
}
}
}
#[derive(Debug)]
pub enum ProcessCommand {
SpawnShellCmd { cmd: String },
Shutdown,
}
impl ProcessManager {
fn spawn_shell_command(&mut self, command: String) {
if cfg!(target_os = "windows") {
return self.spawn_cmd_command(command);
} else if cfg!(target_os = "macos") || cfg!(target_os = "ios") {
// TODO: reasearch if ios also has zsh
return self.spawn_zsh_command(command);
} else {
return self.spawn_bash_command(command);
}
}
fn spawn_bash_command(&mut self, command: String) {
let cmd = Command::new("bash")
.arg("-c")
.arg(command)
.spawn().unwrap();
self.processes.push(cmd);
}
fn spawn_cmd_command(&mut self, command: String) {}
fn spawn_zsh_command(&mut self, command: String) {}
pub async fn lifecycle(&mut self, rx: &mut Receiver<ProcessCommand>) -> Result<()> {
let opt = rx.recv().await;
if let Some(message) = opt {
match message {
ProcessCommand::SpawnShellCmd { cmd } => {}
ProcessCommand::Shutdown => {
info!("Close command");
anyhow::bail!("Close command");
}
}
} else {
anyhow::bail!("Channel closed");
}
Ok(())
}
async fn alive_check(&mut self) -> Result<()> {
for process in &mut self.processes {
process.stdout
}
Ok(())
}
}