mecha10-cli 0.6.3

Mecha10 CLI tool
Documentation
#![allow(dead_code)]

//! Process service for managing child processes
//!
//! This service provides process lifecycle management for CLI commands,
//! particularly for the `dev` and `run` commands that need to manage
//! multiple node processes.
//!
//! This is a thin wrapper around mecha10-runtime's ProcessManager,
//! delegating core process management to the runtime layer.
//!
//! The implementation is split across a few focused submodules, all of
//! which add `impl ProcessService` blocks for the struct defined here:
//!
//! - [`resolve`]: binary path resolution (framework dev mode, global installs, node-runner)
//! - [`spawn`]: plain process spawning primitives (inherited/piped output, env, working dir)
//! - [`spawn_node`]: node-oriented spawning (node-runner, standalone/direct)
//! - [`control`]: status, stop/restart, and cleanup of tracked processes
//! - [`build`]: cargo build helpers used before spawning nodes

mod build;
mod control;
mod resolve;
mod spawn;
mod spawn_node;

use mecha10_runtime::ProcessManager;

/// Process service for managing child processes
///
/// This is a thin wrapper around the runtime's ProcessManager,
/// adding CLI-specific conveniences and delegating core functionality
/// to the runtime layer.
///
/// # Examples
///
/// ```rust,ignore
/// use mecha10_cli::services::ProcessService;
///
/// # async fn example() -> anyhow::Result<()> {
/// let mut service = ProcessService::new();
///
/// // Spawn a node process
/// service.spawn_node("camera_driver", "target/debug/camera_driver", &[])?;
///
/// // Check status
/// let status = service.get_status();
/// println!("Running processes: {:?}", status);
///
/// // Stop a specific process
/// service.stop("camera_driver")?;
///
/// // Cleanup all processes
/// service.cleanup();
/// # Ok(())
/// # }
/// ```
pub struct ProcessService {
    /// Runtime's process manager (handles core lifecycle)
    manager: ProcessManager,
}

impl ProcessService {
    /// Create a new process service
    pub fn new() -> Self {
        Self {
            manager: ProcessManager::new(),
        }
    }

    /// Track dependency relationship for a node
    ///
    /// # Arguments
    ///
    /// * `node` - Name of the node
    /// * `dependencies` - List of nodes this node depends on
    pub fn track_dependency(&mut self, node: &str, dependencies: Vec<String>) {
        for dep in dependencies {
            self.manager.add_dependency(node.to_string(), dep);
        }
    }

    /// Get shutdown order (reverse dependency order)
    ///
    /// Returns nodes in order they should be stopped:
    /// - Nodes with dependents first (high-level nodes)
    /// - Then their dependencies (low-level nodes)
    ///
    /// This ensures we don't stop a node while other nodes depend on it.
    ///
    /// Delegates to the runtime's ProcessManager.
    pub fn get_shutdown_order(&self) -> Vec<String> {
        self.manager.shutdown_order()
    }

    /// Get the number of tracked processes
    pub fn count(&self) -> usize {
        self.manager.len()
    }

    /// Check if any processes are being tracked
    pub fn is_empty(&self) -> bool {
        self.manager.is_empty()
    }

    /// Get access to the underlying ProcessManager
    ///
    /// Useful for advanced operations or when migrating existing code.
    /// Provides direct access to the runtime's ProcessManager.
    pub fn manager(&mut self) -> &mut ProcessManager {
        &mut self.manager
    }
}

impl Default for ProcessService {
    fn default() -> Self {
        Self::new()
    }
}

impl Drop for ProcessService {
    fn drop(&mut self) {
        // ProcessManager's Drop will handle graceful shutdown
    }
}