elif_core/providers/
lifecycle.rs1use crate::providers::{ServiceProvider, ProviderRegistry, ProviderError};
2use crate::container::{Container, ContainerBuilder};
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(&mut self, builder: ContainerBuilder) -> Result<Container, ProviderError> {
27 let start_time = Instant::now();
28
29 tracing::info!("Starting provider lifecycle execution...");
30
31 let dep_start = Instant::now();
33 self.registry.resolve_dependencies()?;
34 self.lifecycle_stats.dependency_resolution_time = dep_start.elapsed();
35
36 let reg_start = Instant::now();
38 let builder = self.registry.register_all(builder)?;
39 self.lifecycle_stats.registration_time = reg_start.elapsed();
40
41 let build_start = Instant::now();
43 let mut container = builder.build()?;
44 container.initialize().await?;
45 self.lifecycle_stats.container_build_time = build_start.elapsed();
46
47 let boot_start = Instant::now();
49 self.registry.boot_all(&container)?;
50 self.lifecycle_stats.boot_time = boot_start.elapsed();
51
52 self.lifecycle_stats.total_time = start_time.elapsed();
53 self.lifecycle_stats.provider_count = self.registry.provider_count();
54
55 tracing::info!(
56 "Provider lifecycle completed successfully in {:?} with {} providers",
57 self.lifecycle_stats.total_time,
58 self.lifecycle_stats.provider_count
59 );
60
61 Ok(container)
62 }
63
64 pub fn lifecycle_stats(&self) -> &ProviderLifecycleStats {
66 &self.lifecycle_stats
67 }
68
69 pub fn registry(&self) -> &ProviderRegistry {
71 &self.registry
72 }
73}
74
75impl Default for ProviderLifecycleManager {
76 fn default() -> Self {
77 Self::new()
78 }
79}
80
81#[derive(Debug, Clone)]
83pub struct ProviderLifecycleStats {
84 pub provider_count: usize,
85 pub total_time: std::time::Duration,
86 pub dependency_resolution_time: std::time::Duration,
87 pub registration_time: std::time::Duration,
88 pub container_build_time: std::time::Duration,
89 pub boot_time: std::time::Duration,
90}
91
92impl ProviderLifecycleStats {
93 pub fn new() -> Self {
95 Self {
96 provider_count: 0,
97 total_time: std::time::Duration::ZERO,
98 dependency_resolution_time: std::time::Duration::ZERO,
99 registration_time: std::time::Duration::ZERO,
100 container_build_time: std::time::Duration::ZERO,
101 boot_time: std::time::Duration::ZERO,
102 }
103 }
104}
105
106impl Default for ProviderLifecycleStats {
107 fn default() -> Self {
108 Self::new()
109 }
110}