tcrm_monitor/
lib.rs

1//! # TCRM Monitor
2//!
3//! A task dependency management and execution library for Rust applications.
4//!
5//! ## Overview
6//!
7//! TCRM Monitor provides tools for defining, managing, and executing tasks with complex dependency relationships.
8//! It supports parallel execution of independent tasks while ensuring dependencies are respected,
9//! real-time event monitoring, and runtime control of task execution.
10//!
11//! ## Core Features
12//!
13//! - **Dependency Management**: Define task graphs with automatic dependency validation and circular dependency detection
14//! - **Parallel Execution**: Execute independent tasks concurrently while respecting dependency order
15//! - **Shell Integration**: Support for running tasks from various shells (Bash, Sh, PowerShell, CMD) based on OS
16//! - **Real-time Events**: Monitor task execution with detailed event streams
17//! - **Runtime Control**: Stop tasks, send stdin input, and terminate specific tasks during execution
18//! - **Serialization**: Optional flatbuffers and serde support for task configuration
19//!
20//! ## Quick Start
21//!
22//! ```rust
23//! use std::collections::HashMap;
24//! use tcrm_monitor::monitor::{tasks::TaskMonitor, config::{TaskSpec, TaskShell}};
25//! use tcrm_task::tasks::config::TaskConfig;
26//!
27//! #[tokio::main]
28//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
29//!     let mut tasks = HashMap::new();
30//!     
31//!     // Define a task with dependencies
32//!     tasks.insert(
33//!         "setup".to_string(),
34//!         TaskSpec::new(TaskConfig::new("echo").args(["Setting up..."]))
35//!             .shell(TaskShell::Auto),
36//!     );
37//!     
38//!     tasks.insert(
39//!         "build".to_string(),
40//!         TaskSpec::new(TaskConfig::new("echo").args(["Building..."]))
41//!             .dependencies(["setup"])
42//!             .shell(TaskShell::Auto),
43//!     );
44//!     
45//!     // Create and execute
46//!     let mut monitor = TaskMonitor::new(tasks)?;
47//!     monitor.execute_all_direct(None).await;
48//!     
49//!     Ok(())
50//! }
51//! ```
52//!
53//! ## Event Monitoring
54//!
55//! Monitor task execution in real-time:
56//!
57//! ```rust
58//! use tokio::sync::mpsc;
59//! use std::collections::HashMap;
60//! use tcrm_monitor::monitor::{tasks::TaskMonitor, config::{TaskSpec, TaskShell}, event::{TaskMonitorEvent, TaskMonitorControlCommand}};
61//! use tcrm_task::tasks::config::TaskConfig;
62//!
63//! # #[tokio::main]
64//! # async fn main() -> Result<(), Box<dyn std::error::Error>> {
65//! let mut tasks = HashMap::new();
66//! tasks.insert(
67//!     "test".to_string(),
68//!     TaskSpec::new(TaskConfig::new("echo").args(["Hello"]))
69//!         .shell(TaskShell::Auto)
70//! );
71//! let mut monitor = TaskMonitor::new(tasks)?;
72//! let (event_tx, mut event_rx) = mpsc::channel(100);
73//! let (control_tx, control_rx) = mpsc::channel(10);
74//!
75//! // Start monitoring events
76//! let event_handler = tokio::spawn(async move {
77//!     while let Some(event) = event_rx.recv().await {
78//!         match event {
79//!             TaskMonitorEvent::Started { total_tasks } => {
80//!                 println!("Started execution of {} tasks", total_tasks);
81//!             }
82//!             TaskMonitorEvent::Task(task_event) => {
83//!                 println!("Task event: {:?}", task_event);
84//!             }
85//!             TaskMonitorEvent::Control(control_event) => {
86//!                 println!("Control event: {:?}", control_event);
87//!             }
88//!             TaskMonitorEvent::Completed { .. } => {
89//!                 break; // Exit when execution completes
90//!             }
91//!             _ => {}
92//!         }
93//!     }
94//! });
95//!
96//! // Execute with control
97//! monitor.execute_all_direct_with_control(Some(event_tx), control_rx).await;
98//!
99//! // Wait for event handler to finish
100//! let _ = event_handler.await;
101//! # Ok(())
102//! # }
103//! ```
104//!
105//! ## Task Configuration
106//!
107//! Tasks are configured using [`TaskSpec`](monitor::config::TaskSpec) which wraps a
108//! [`TaskConfig`](https://docs.rs/tcrm-task/latest/tcrm_task/tasks/config/struct.TaskConfig.html)
109//! with additional dependency and execution options:
110//!
111//! ```rust
112//! use tcrm_monitor::monitor::config::{TaskSpec, TaskShell};
113//! use tcrm_task::tasks::config::TaskConfig;
114//!
115//! let task = TaskSpec::new(
116//!     TaskConfig::new("cargo")
117//!         .args(["build", "--release"])
118//!         .working_dir("/project")
119//!         .timeout_ms(300_000)
120//!         .enable_stdin(true)
121//! )
122//! .shell(TaskShell::Auto)
123//! .dependencies(["setup", "test"])
124//! .terminate_after_dependents(true)
125//! .ignore_dependencies_error(false);
126//! ```
127//!
128//! ## Features
129//!
130//! The library supports several optional features:
131//!
132//! - `serde`: Enable serde serialization for task configurations
133//! - `flatbuffers`: Enable flatbuffers serialization for efficient data exchange
134//! - `tracing`: Enable structured logging with the tracing crate
135//!
136//! Enable features in your `Cargo.toml`:
137//!
138//! ```toml
139//! [dependencies]
140//! tcrm-monitor = { version = "0.1", features = ["serde", "tracing"] }
141//! ```
142
143#[cfg(feature = "flatbuffers")]
144pub mod flatbuffers;
145pub mod monitor;
146
147pub use tcrm_task::tasks as tcrm_tasks;