elif_core/providers/
lifecycle.rs1use crate::container::{Container, ContainerBuilder};
2use crate::providers::{ProviderError, ProviderRegistry, ServiceProvider};
3use std::time::Instant;
4
5pub struct ProviderLifecycleManager {
7 registry: ProviderRegistry,
8 lifecycle_stats: ProviderLifecycleStats,
9}
10
11impl ProviderLifecycleManager {
12 pub fn new() -> Self {
14 Self {
15 registry: ProviderRegistry::new(),
16 lifecycle_stats: ProviderLifecycleStats::new(),
17 }
18 }
19
20 pub fn register<P: ServiceProvider + 'static>(&mut self, provider: P) {
22 self.registry.register(provider);
23 }
24
25 pub async fn execute_lifecycle(
27 &mut self,
28 builder: ContainerBuilder,
29 ) -> Result<Container, ProviderError> {
30 let start_time = Instant::now();
31
32 tracing::info!("Starting provider lifecycle execution...");
33
34 let dep_start = Instant::now();
36 self.registry.resolve_dependencies()?;
37 self.lifecycle_stats.dependency_resolution_time = dep_start.elapsed();
38
39 let reg_start = Instant::now();
41 let builder = self.registry.register_all(builder)?;
42 self.lifecycle_stats.registration_time = reg_start.elapsed();
43
44 let build_start = Instant::now();
46 let mut container = builder.build()?;
47 container.initialize().await?;
48 self.lifecycle_stats.container_build_time = build_start.elapsed();
49
50 let boot_start = Instant::now();
52 self.registry.boot_all(&container)?;
53 self.lifecycle_stats.boot_time = boot_start.elapsed();
54
55 self.lifecycle_stats.total_time = start_time.elapsed();
56 self.lifecycle_stats.provider_count = self.registry.provider_count();
57
58 tracing::info!(
59 "Provider lifecycle completed successfully in {:?} with {} providers",
60 self.lifecycle_stats.total_time,
61 self.lifecycle_stats.provider_count
62 );
63
64 Ok(container)
65 }
66
67 pub fn lifecycle_stats(&self) -> &ProviderLifecycleStats {
69 &self.lifecycle_stats
70 }
71
72 pub fn registry(&self) -> &ProviderRegistry {
74 &self.registry
75 }
76}
77
78impl Default for ProviderLifecycleManager {
79 fn default() -> Self {
80 Self::new()
81 }
82}
83
84#[derive(Debug, Clone)]
86pub struct ProviderLifecycleStats {
87 pub provider_count: usize,
88 pub total_time: std::time::Duration,
89 pub dependency_resolution_time: std::time::Duration,
90 pub registration_time: std::time::Duration,
91 pub container_build_time: std::time::Duration,
92 pub boot_time: std::time::Duration,
93}
94
95impl ProviderLifecycleStats {
96 pub fn new() -> Self {
98 Self {
99 provider_count: 0,
100 total_time: std::time::Duration::ZERO,
101 dependency_resolution_time: std::time::Duration::ZERO,
102 registration_time: std::time::Duration::ZERO,
103 container_build_time: std::time::Duration::ZERO,
104 boot_time: std::time::Duration::ZERO,
105 }
106 }
107}
108
109impl Default for ProviderLifecycleStats {
110 fn default() -> Self {
111 Self::new()
112 }
113}