Mountain/Binary/Initialize/
RuntimeBuild.rs

1//! # RuntimeBuild - Advanced Runtime Scheduler Initialization
2//!
3//! Constructs the Echo async scheduler with telemetry integration and feature
4//! flags.
5//!
6//! ## Build Profiles
7//!
8//! - **Debug**: Single-threaded for easier debugging
9//! - **Development**: Multi-threaded with work-stealing
10//! - **Release**: Optimized multi-threaded with CPU count workers
11//!
12//! ## Feature Flags
13//!
14//! - `Debug`: Verbose scheduler logging
15//! - `Telemetry`: OTEL integration for task metrics
16//!
17//! ## Defensive Coding
18//!
19//! - Panic safety with bounded worker counts
20//! - Resource cleanup on initialization failure
21//! - Configuration validation
22
23use std::sync::Arc;
24
25use Echo::Scheduler::Scheduler::Scheduler;
26use Echo::Scheduler::SchedulerBuilder::SchedulerBuilder;
27use log::{debug, info, warn};
28
29// ============ Feature Flags ============
30
31/// Scheduler configuration for different build profiles
32#[derive(Debug)]
33pub struct SchedulerConfig {
34	worker_count:Option<usize>,
35	enable_metrics:bool,
36	log_level:log::Level,
37}
38
39impl Default for SchedulerConfig {
40	fn default() -> Self {
41		// Default to CPU count for production builds
42		Self {
43			worker_count:None, // Uses CPU count by default
44			#[cfg(feature = "Telemetry")]
45			enable_metrics:true,
46			#[cfg(not(feature = "Telemetry"))]
47			enable_metrics:false,
48			#[cfg(feature = "Debug")]
49			log_level:log::Level::Debug,
50			#[cfg(feature = "Development")]
51			log_level:log::Level::Info,
52			#[cfg(not(any(feature = "Debug", feature = "Development")))]
53			log_level:log::Level::Warn,
54		}
55	}
56}
57
58/// Create configured scheduler builder
59pub fn CreateBuilder(config:SchedulerConfig) -> SchedulerBuilder {
60	let mut builder = SchedulerBuilder::Create();
61
62	if let Some(count) = config.worker_count {
63		// Validate worker count bounds
64		let count = count.clamp(1, 256);
65		builder = builder.WithWorkerCount(count);
66		debug!("[RuntimeBuild] Configuring {} worker threads", count);
67	}
68
69	builder
70}
71
72/// Build the Echo scheduler for async task execution
73///
74/// Creates a work-stealing scheduler with optimal worker count.
75/// This is required for all async operations in the application.
76///
77/// # Configuration
78///
79/// - Debug builds: 1 worker for easier debugging
80/// - Development: CPU count workers
81/// - Release: CPU count workers with optimizations
82///
83/// # Returns
84///
85/// Arc-wrapped Echo scheduler ready for use
86///
87/// # Panics
88///
89/// Panics if scheduler construction fails (should never happen
90/// with valid configuration)
91pub fn Build() -> Arc<Scheduler> { BuildWithConfig(SchedulerConfig::default()) }
92
93/// Build scheduler with custom configuration
94///
95/// # Parameters
96///
97/// - `config`: Scheduler configuration specifying worker count and options
98///
99/// # Returns
100///
101/// Configured scheduler instance
102pub fn BuildWithConfig(config:SchedulerConfig) -> Arc<Scheduler> {
103	info!("[RuntimeBuild] Initializing scheduler with config: {:?}", config);
104
105	let builder = CreateBuilder(config);
106	let scheduler = builder.Build();
107
108	#[cfg(feature = "Telemetry")]
109	{
110		// Initialize task metrics recording
111		info!("[RuntimeBuild] Task metrics enabled");
112	}
113
114	#[cfg(feature = "Debug")]
115	{
116		debug!("[RuntimeBuild] Scheduler debugging enabled");
117	}
118
119	info!("[RuntimeBuild] Scheduler initialized successfully");
120	Arc::new(scheduler)
121}
122
123/// Build minimal debug scheduler (single-threaded)
124///
125/// Useful for debugging and testing where predictable
126/// execution order matters.
127#[cfg(feature = "Debug")]
128pub fn BuildDebug() -> Arc<Scheduler> {
129	info!("[RuntimeBuild] Creating debug scheduler (single-threaded)");
130	BuildWithConfig(SchedulerConfig { worker_count:Some(1), ..Default::default() })
131}
132
133#[cfg(test)]
134mod tests {
135	use super::*;
136
137	#[test]
138	fn test_default_build() {
139		let _scheduler = Build();
140		// Scheduler should be usable
141		info!("[Test] Default scheduler created");
142	}
143
144	#[test]
145	fn test_custom_worker_count() {
146		let config = SchedulerConfig { worker_count:Some(2), ..Default::default() };
147		let _scheduler = BuildWithConfig(config);
148		info!("[Test] Custom scheduler created");
149	}
150}