use super::ProcessService;
use anyhow::Result;
use std::collections::HashMap;
impl ProcessService {
pub fn get_status(&mut self) -> HashMap<String, String> {
use mecha10_runtime::ProcessStatus;
self.manager
.status_all()
.into_iter()
.map(|(name, status)| {
let status_str = match status {
ProcessStatus::Running => "running".to_string(),
ProcessStatus::Exited(code) => format!("exited (code: {})", code),
ProcessStatus::Error => "error".to_string(),
};
(name, status_str)
})
.collect()
}
pub fn stop(&mut self, name: &str) -> Result<()> {
self.manager.stop_graceful(name, std::time::Duration::from_secs(10))
}
pub fn stop_with_timeout(&mut self, name: &str, timeout: std::time::Duration) -> Result<()> {
self.manager.stop_graceful(name, timeout)
}
pub fn force_kill(&mut self, name: &str) -> Result<()> {
self.manager.force_kill(name)
}
pub fn cleanup(&mut self) {
self.manager.shutdown_all();
}
pub fn is_running(&mut self, name: &str) -> bool {
self.manager.is_running(name)
}
pub fn restart(&mut self, name: &str, binary_path: &str, args: &[&str]) -> Result<u32> {
if self.is_running(name) {
self.stop(name)?;
std::thread::sleep(std::time::Duration::from_millis(100));
}
self.spawn_node(name, binary_path, args)
}
pub fn restart_all(&mut self, nodes: Vec<(&str, &str, Vec<&str>)>) -> Result<HashMap<String, u32>> {
self.cleanup();
std::thread::sleep(std::time::Duration::from_millis(500));
self.spawn_nodes(nodes)
}
}