tcrm_task/
lib.rs

1//! # tcrm-task
2//!
3//! A secure, async-first Rust library for executing and managing system processes.
4//! Built for developers who need reliable process execution with comprehensive
5//! security validation and real-time event monitoring.
6//!
7//! ## Features
8//!
9//! - **Async Process Execution**: Built on Tokio for high-performance async operations
10//! - **Security Validation**: Prevents command injection and validates all inputs
11//! - **Real-time Events**: Monitor process output, state changes, and lifecycle events
12//! - **Timeout Management**: Configurable timeouts for process execution
13//! - **Stdin Support**: Send input to running processes
14//! - **Ready Indicators**: Detect when long-running processes are ready to accept requests
15//! - **Serialization**: Optional serde and flatbuffers support for persistence
16//!
17//! ## Quick Start
18//!
19//! ```rust
20//! use tcrm_task::tasks::{config::TaskConfig, async_tokio::spawner::TaskSpawner};
21//! use tokio::sync::mpsc;
22//!
23//! #[tokio::main]
24//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
25//!     // Create a simple command configuration
26//!     let config = TaskConfig::new("cmd")
27//!         .args(["/C", "echo", "Hello, World!"]);
28//!
29//!     // Validate the configuration
30//!     config.validate()?;
31//!
32//!     // Create a spawner and execute the task
33//!     let (tx, mut rx) = mpsc::channel(100);
34//!     let mut spawner = TaskSpawner::new("hello".to_string(), config);
35//!     
36//!     spawner.start_direct(tx).await?;
37//!
38//!     // Process events
39//!     while let Some(event) = rx.recv().await {
40//!         println!("Event: {:?}", event);
41//!     }
42//!
43//!     Ok(())
44//! }
45//! ```
46//!
47//! ## Advanced Usage
48//!
49//! ### Long-running Process with Ready Indicator
50//!
51//! ```rust
52//! use tcrm_task::tasks::{config::{TaskConfig, StreamSource}, async_tokio::spawner::TaskSpawner};
53//! use tokio::sync::mpsc;
54//!
55//! #[tokio::main]
56//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
57//!     let config = TaskConfig::new("cmd")
58//!         .args(["/C", "echo", "Server listening on"])
59//!         .ready_indicator("Server listening on")
60//!         .ready_indicator_source(StreamSource::Stdout)
61//!         .timeout_ms(30000);
62//!
63//!     let (tx, mut rx) = mpsc::channel(100);
64//!     let mut spawner = TaskSpawner::new("server".to_string(), config);
65//!     
66//!     spawner.start_direct(tx).await?;
67//!
68//!     // Wait for ready event
69//!     while let Some(event) = rx.recv().await {
70//!         if matches!(event, tcrm_task::tasks::event::TaskEvent::Ready { .. }) {
71//!             println!("Server is ready!");
72//!             break;
73//!         }
74//!     }
75//!
76//!     Ok(())
77//! }
78//! ```
79//!
80//! ### Process with Environment Variables and Working Directory
81//!
82//! ```rust
83//! use tcrm_task::tasks::{config::TaskConfig, async_tokio::spawner::TaskSpawner};
84//! use std::collections::HashMap;
85//!
86//! #[tokio::main]
87//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
88//!     let mut env = HashMap::new();
89//!     env.insert("RUST_LOG".to_string(), "debug".to_string());
90//!     env.insert("APP_ENV".to_string(), "production".to_string());
91//!
92//!     let config = TaskConfig::new("cmd")
93//!         .args(["/C", "dir"])
94//!         .working_dir("C:\\")
95//!         .env(env)
96//!         .timeout_ms(300000); // 5 minutes
97//!
98//!     config.validate()?;
99//!     
100//!     // ... execute task
101//!     Ok(())
102//! }
103//! ```
104//!
105//! ## Validation
106//!
107//! This library includes validation to prevent:
108//! - Path traversal  
109//! - Null byte
110//!
111//! All configurations are validated before execution using the built-in validator.
112//!
113//! ## Optional Features
114//!
115//! - `serde`: Enable serialization support for all types
116//! - `flatbuffers`: Enable `FlatBuffers` serialization for high-performance scenarios
117//! - `tracing`: Enable structured logging integration
118
119#[cfg(feature = "flatbuffers")]
120pub mod flatbuffers;
121pub mod helper;
122pub mod tasks;