ProcessService

Struct ProcessService 

Source
pub struct ProcessService { /* private fields */ }
Expand description

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

use mecha10_cli::services::ProcessService;

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();

Implementations§

Source§

impl ProcessService

Source

pub fn new() -> Self

Create a new process service

Source

pub fn track_dependency(&mut self, node: &str, dependencies: Vec<String>)

Track dependency relationship for a node

§Arguments
  • node - Name of the node
  • dependencies - List of nodes this node depends on
Source

pub fn get_shutdown_order(&self) -> Vec<String>

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.

Source

pub fn is_framework_dev_mode() -> bool

Check if we’re in framework development mode

Framework dev mode is detected by:

  1. MECHA10_FRAMEWORK_PATH environment variable
  2. Existence of .cargo/config.toml with patches
Source

pub fn find_global_binary(node_name: &str) -> Option<PathBuf>

Find globally installed binary for a node

Searches in:

  1. ~/.cargo/bin/{node_name}
  2. ~/.mecha10/bin/{node_name}
§Arguments
  • node_name - Name of the node (e.g., “simulation-bridge”)
§Returns

Path to the binary if found, None otherwise

Source

pub fn resolve_node_binary( node_name: &str, is_monorepo_node: bool, project_name: &str, ) -> String

Resolve binary path for a node with smart resolution

Resolution strategy:

  1. If framework dev mode: use local build (target/debug or target/release)
  2. If global binary exists: use global binary
  3. Fallback: use local build path
§Arguments
  • node_name - Name of the node
  • is_monorepo_node - Whether this is a framework node
  • project_name - Name of the project
§Returns

Path to the binary to execute

Source

pub fn resolve_node_runner_path() -> String

Resolve path to mecha10-node-runner binary

Resolution strategy:

  1. Framework dev mode: $MECHA10_FRAMEWORK_PATH/target/release/mecha10-node-runner
  2. Global installation: ~/.cargo/bin/mecha10-node-runner
  3. Fallback: “mecha10-node-runner” (rely on PATH)
§Returns

Path to the mecha10-node-runner binary

Source

pub fn spawn_node( &mut self, name: &str, binary_path: &str, args: &[&str], ) -> Result<u32>

Spawn a node process

§Arguments
  • name - Name to identify the process
  • binary_path - Path to the binary to execute
  • args - Command-line arguments
§Errors

Returns an error if the process cannot be spawned

Source

pub fn spawn_with_output( &mut self, name: &str, binary_path: &str, args: &[&str], ) -> Result<u32>

Spawn a process with output capture

§Arguments
  • name - Name to identify the process
  • binary_path - Path to the binary to execute
  • args - Command-line arguments
§Errors

Returns an error if the process cannot be spawned

Source

pub fn spawn_with_env( &mut self, name: &str, binary_path: &str, args: &[&str], env: HashMap<String, String>, ) -> Result<u32>

Spawn a process with custom environment variables

§Arguments
  • name - Name to identify the process
  • binary_path - Path to the binary to execute
  • args - Command-line arguments
  • env - Environment variables to set
Source

pub fn spawn_in_dir( &mut self, name: &str, binary_path: &str, args: &[&str], working_dir: impl AsRef<Path>, ) -> Result<u32>

Spawn a process in a specific working directory

§Arguments
  • name - Name to identify the process
  • binary_path - Path to the binary to execute
  • args - Command-line arguments
  • working_dir - Working directory for the process
Source

pub fn spawn_nodes( &mut self, nodes: Vec<(&str, &str, Vec<&str>)>, ) -> Result<HashMap<String, u32>>

Spawn multiple node processes from a list

§Arguments
  • nodes - Vec of (name, binary_path, args) tuples
§Returns

HashMap of node names to PIDs

Source

pub fn get_status(&mut self) -> HashMap<String, String>

Get status of all processes

Returns a HashMap mapping process names to status strings

Source

pub fn count(&self) -> usize

Get the number of tracked processes

Source

pub fn is_empty(&self) -> bool

Check if any processes are being tracked

Source

pub fn stop(&mut self, name: &str) -> Result<()>

Stop a specific process by name

§Arguments
  • name - Name of the process to stop
§Errors

Returns an error if the process is not found

Note: This uses a default 10-second timeout for graceful shutdown. Use stop_with_timeout() for custom timeout.

Source

pub fn stop_with_timeout(&mut self, name: &str, timeout: Duration) -> Result<()>

Stop a process with timeout for graceful shutdown

Tries graceful shutdown (SIGTERM on Unix), then force kills after timeout

§Arguments
  • name - Name of the process to stop
  • timeout - How long to wait for graceful shutdown
§Errors

Returns an error if process not found or cannot be stopped

Source

pub fn force_kill(&mut self, name: &str) -> Result<()>

Force kill a process by name

§Arguments
  • name - Name of the process to kill
Source

pub fn cleanup(&mut self)

Stop all processes gracefully in dependency order

This delegates to the runtime’s ProcessManager which handles:

  • Dependency-based shutdown ordering
  • Graceful shutdown with timeout
  • Force kill fallback
Source

pub fn is_running(&mut self, name: &str) -> bool

Check if a process is running

§Arguments
  • name - Name of the process to check
Source

pub fn manager(&mut self) -> &mut ProcessManager

Get access to the underlying ProcessManager

Useful for advanced operations or when migrating existing code. Provides direct access to the runtime’s ProcessManager.

Source

pub fn build_node(&self, node_name: &str, release: bool) -> Result<()>

Build a node binary if needed

Helper method to build a specific node package

§Arguments
  • node_name - Name of the node to build
  • release - Whether to build in release mode
Source

pub fn build_all(&self, release: bool) -> Result<()>

Build all nodes in the workspace

§Arguments
  • release - Whether to build in release mode
Source

pub fn build_from_framework( &self, package_name: &str, release: bool, ) -> Result<()>

Build a binary from the framework monorepo

This builds a binary from the framework path (MECHA10_FRAMEWORK_PATH). Used for binaries like mecha10-node-runner that exist in the monorepo but need to be built when running from a generated project.

§Arguments
  • package_name - Name of the package to build (e.g., “mecha10-node-runner”)
  • release - Whether to build in release mode
§Returns

Ok(()) on success, or error if build fails or framework path not set

Source

pub fn build_project_packages(&self, release: bool) -> Result<()>

Build only packages needed by the current project (smart selective build)

For generated projects, this just builds the project binary. Cargo automatically builds only the dependencies actually used. With .cargo/config.toml patches, this rebuilds framework packages from source.

§Arguments
  • release - Whether to build in release mode
§Returns

Ok(()) on success, or error if build fails

Source

pub fn restart( &mut self, name: &str, binary_path: &str, args: &[&str], ) -> Result<u32>

Restart a specific process

Stops the process if running and starts it again

§Arguments
  • name - Name of the process
  • binary_path - Path to the binary
  • args - Command-line arguments
Source

pub fn restart_all( &mut self, nodes: Vec<(&str, &str, Vec<&str>)>, ) -> Result<HashMap<String, u32>>

Restart all processes

§Arguments
  • nodes - Vec of (name, binary_path, args) tuples
Source

pub fn spawn_node_runner( &mut self, node_name: &str, project_env: Option<HashMap<String, String>>, ) -> Result<u32>

Spawn a node using mecha10-node-runner

This is the simplified spawning method for Phase 2+ of Node Lifecycle Architecture. It delegates all complexity (binary resolution, model pulling, env setup) to node-runner.

§Arguments
  • node_name - Name of the node to run
§Returns

Process ID of the spawned node-runner instance

§Errors

Returns an error if the node-runner cannot be spawned

§Configuration

The node-runner reads configuration from the node’s config file (e.g., configs/nodes/{node_name}.json) and supports the following runtime settings:

{
  "runtime": {
    "restart_policy": "on-failure",  // never, on-failure, always
    "max_retries": 3,
    "backoff_secs": 1
  },
  "depends_on": ["camera", "lidar"],
  "startup_timeout_secs": 30
}

To enable dependency checking, use: mecha10-node-runner --wait-for-deps <node-name>

Source

pub fn spawn_node_direct( &mut self, node_name: &str, _project_name: &str, project_env: Option<HashMap<String, String>>, ) -> Result<u32>

Spawn a node directly via the project binary (standalone mode)

This is used when mecha10-node-runner is not available (standalone projects installed from crates.io). Handles both bundled nodes (via CLI) and local project nodes (built and run directly).

§Arguments
  • node_name - Name of the node to run
  • project_name - Name of the project (used to find the binary)
  • project_env - Optional additional environment variables from project config
§Returns

Process ID of the spawned node process

Source

pub fn is_node_runner_available() -> bool

Check if mecha10-node-runner is available

Trait Implementations§

Source§

impl Default for ProcessService

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl Drop for ProcessService

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more