Skip to main content

Mountain/Binary/Initialize/
RuntimeBuild.rs

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