use std::sync::{Arc, OnceLock}; use swactor::actor::{ActorAddress, Ctx}; use swactor::runtime::ExternalSender; use swactor::Error; use crate::actor::ProcessActor; use crate::local::LocalDriver; use crate::message::ProcessCommand; use crate::session::ProcessSession; use crate::types::{EventQueue, ProcessDriver, ProcessSpec, ProcessWaker}; /// Spawn a process actor using the real `LocalDriver` (OS subprocess). /// /// Creates a `ProcessActor`, spawns it in the runtime, and /// wires up the waker so that I/O thread events automatically wake the actor. /// /// Returns the actor's address. Send `ProcessCommand` messages to control it. pub fn spawn_local_process( ctx: &Ctx, sender: &ExternalSender, spec: ProcessSpec, ) -> Result { let waker_slot = Arc::new(OnceLock::new()); let queue = EventQueue::new(); let driver = LocalDriver::new(queue, waker_slot.clone()); spawn_process_inner(ctx, sender, spec, driver, waker_slot) } /// Spawn a process actor with a custom driver. /// /// Useful for testing with `MockDriver` or other custom drivers while /// still getting the full actor integration (waker, lifecycle, etc.). pub fn spawn_process( ctx: &Ctx, sender: &ExternalSender, spec: ProcessSpec, driver: D, waker_slot: Arc>, ) -> Result { spawn_process_inner(ctx, sender, spec, driver, waker_slot) } /// Spawn a process actor using the `SshDriver` (remote host via SSH). /// /// Requires a tokio runtime handle (e.g. from `IrohDriver::tokio_handle()`) /// and SSH connection config. #[cfg(feature = "ssh")] pub fn spawn_ssh_process( ctx: &Ctx, sender: &ExternalSender, spec: ProcessSpec, tokio_handle: tokio::runtime::Handle, ssh_config: crate::ssh::SshConfig, ) -> Result { let waker_slot = Arc::new(OnceLock::new()); let queue = EventQueue::new(); let driver = crate::ssh::SshDriver::new(queue, waker_slot.clone(), tokio_handle, ssh_config); spawn_process_inner(ctx, sender, spec, driver, waker_slot) } /// The basename of a command path, used as the process's telemetry label. /// `/usr/bin/python3` → `python3`, `python` → `python`. Falls back to the whole /// string when there is no path separator or trailing component. fn command_basename(command: &str) -> String { command .rsplit(['/', '\\']) .find(|s| !s.is_empty()) .unwrap_or(command) .to_string() } fn spawn_process_inner( ctx: &Ctx, sender: &ExternalSender, spec: ProcessSpec, driver: D, waker_slot: Arc>, ) -> Result { // Label this process's output by its command basename so the node's // per-runtime observer (if any) taps it onto `proc.