Skip to main content

mecha10_cli/services/process/
control.rs

1//! Status, stop/restart, and cleanup operations for `ProcessService`
2
3use super::ProcessService;
4use anyhow::Result;
5use std::collections::HashMap;
6
7impl ProcessService {
8    /// Get status of all processes
9    ///
10    /// Returns a HashMap mapping process names to status strings
11    pub fn get_status(&mut self) -> HashMap<String, String> {
12        use mecha10_runtime::ProcessStatus;
13
14        self.manager
15            .status_all()
16            .into_iter()
17            .map(|(name, status)| {
18                let status_str = match status {
19                    ProcessStatus::Running => "running".to_string(),
20                    ProcessStatus::Exited(code) => format!("exited (code: {})", code),
21                    ProcessStatus::Error => "error".to_string(),
22                };
23                (name, status_str)
24            })
25            .collect()
26    }
27
28    /// Stop a specific process by name
29    ///
30    /// # Arguments
31    ///
32    /// * `name` - Name of the process to stop
33    ///
34    /// # Errors
35    ///
36    /// Returns an error if the process is not found
37    ///
38    /// Note: This uses a default 10-second timeout for graceful shutdown.
39    /// Use stop_with_timeout() for custom timeout.
40    pub fn stop(&mut self, name: &str) -> Result<()> {
41        self.manager.stop_graceful(name, std::time::Duration::from_secs(10))
42    }
43
44    /// Stop a process with timeout for graceful shutdown
45    ///
46    /// Tries graceful shutdown (SIGTERM on Unix), then force kills after timeout
47    ///
48    /// # Arguments
49    ///
50    /// * `name` - Name of the process to stop
51    /// * `timeout` - How long to wait for graceful shutdown
52    ///
53    /// # Errors
54    ///
55    /// Returns an error if process not found or cannot be stopped
56    pub fn stop_with_timeout(&mut self, name: &str, timeout: std::time::Duration) -> Result<()> {
57        self.manager.stop_graceful(name, timeout)
58    }
59
60    /// * `name` - Name of the process to kill
61    pub fn force_kill(&mut self, name: &str) -> Result<()> {
62        self.manager.force_kill(name)
63    }
64
65    /// Stop all processes gracefully in dependency order
66    ///
67    /// This delegates to the runtime's ProcessManager which handles:
68    /// - Dependency-based shutdown ordering
69    /// - Graceful shutdown with timeout
70    /// - Force kill fallback
71    pub fn cleanup(&mut self) {
72        self.manager.shutdown_all();
73    }
74
75    /// * `name` - Name of the process to check
76    pub fn is_running(&mut self, name: &str) -> bool {
77        self.manager.is_running(name)
78    }
79
80    /// Restart a specific process
81    ///
82    /// Stops the process if running and starts it again
83    ///
84    /// # Arguments
85    ///
86    /// * `name` - Name of the process
87    /// * `binary_path` - Path to the binary
88    /// * `args` - Command-line arguments
89    pub fn restart(&mut self, name: &str, binary_path: &str, args: &[&str]) -> Result<u32> {
90        if self.is_running(name) {
91            self.stop(name)?;
92            std::thread::sleep(std::time::Duration::from_millis(100));
93        }
94
95        self.spawn_node(name, binary_path, args)
96    }
97
98    /// Restart all processes
99    ///
100    /// # Arguments
101    ///
102    /// * `nodes` - Vec of (name, binary_path, args) tuples
103    pub fn restart_all(&mut self, nodes: Vec<(&str, &str, Vec<&str>)>) -> Result<HashMap<String, u32>> {
104        self.cleanup();
105
106        std::thread::sleep(std::time::Duration::from_millis(500));
107
108        self.spawn_nodes(nodes)
109    }
110}