mecha10-cli 0.8.0

Mecha10 CLI tool
Documentation
//! Status, stop/restart, and cleanup operations for `ProcessService`

use super::ProcessService;
use anyhow::Result;
use std::collections::HashMap;

impl ProcessService {
    /// Get status of all processes
    ///
    /// Returns a HashMap mapping process names to status strings
    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()
    }

    /// Stop a specific process by name. Returns an error if the process is not found. Uses a
    /// default 10-second timeout for graceful shutdown; use `stop_with_timeout` for a custom one.
    pub fn stop(&mut self, name: &str) -> Result<()> {
        self.manager.stop_graceful(name, std::time::Duration::from_secs(10))
    }

    /// Stop a process with timeout for graceful shutdown. Tries graceful shutdown (SIGTERM on
    /// Unix), then force kills after the timeout.
    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)
    }

    /// Stop all processes gracefully in dependency order, delegating to the runtime's
    /// ProcessManager.
    pub fn cleanup(&mut self) {
        self.manager.shutdown_all();
    }

    pub fn is_running(&mut self, name: &str) -> bool {
        self.manager.is_running(name)
    }

    /// Restart a specific process: stops it if running and starts it again.
    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)
    }

    /// Restart all processes. `nodes` is a list of (name, binary_path, args) tuples.
    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)
    }
}