Mountain/Binary/Shutdown/SchedulerShutdown.rs
1//! # Scheduler Shutdown Module
2//!
3//! Handles graceful shutdown of the Echo task scheduler.
4
5#[allow(unused_imports)]
6use std::sync::Arc;
7
8use Echo::Scheduler::Scheduler::Scheduler;
9
10use crate::dev_log;
11
12/// Stops the Echo task scheduler and cleans up its resources.
13///
14/// # Arguments
15///
16/// * `SchedulerForShutdown` - Arc-wrapped scheduler to shut down
17///
18/// # Returns
19///
20/// A `Result` indicating success or failure.
21///
22/// # Shutdown Process
23///
24/// This function performs:
25/// - Stops accepting new tasks
26/// - Completes in-progress tasks
27/// - Cleans up scheduler resources
28///
29/// # Errors
30///
31/// Returns an error if the scheduler is not exclusively owned or stop fails.
32pub async fn SchedulerShutdown(SchedulerForShutdown:Arc<Scheduler>) -> Result<(), String> {
33 dev_log!("lifecycle", "[Shutdown] [Scheduler] Stopping Echo scheduler...");
34
35 // Try to get exclusive ownership for shutdown
36 match Arc::try_unwrap(SchedulerForShutdown) {
37 Ok(mut Scheduler) => {
38 Scheduler.Stop().await;
39 dev_log!("lifecycle", "[Shutdown] [Scheduler] Echo scheduler stopped successfully.");
40 Ok(())
41 },
42 Err(_) => Err("Scheduler not exclusively owned".to_string()),
43 }
44}