mf_state/plugin/
manager.rs1use std::collections::{HashMap, HashSet};
2use std::sync::atomic::{AtomicBool, Ordering};
3use std::sync::Arc;
4use anyhow::Result;
5
6use super::dependency::DependencyManager;
7use super::plugin::Plugin;
8
9#[derive(Debug, Clone)]
42pub struct PluginManager {
43 plugins: Arc<HashMap<String, Arc<Plugin>>>,
45 sorted_plugins: Arc<Vec<Arc<Plugin>>>,
47 initialized: Arc<AtomicBool>,
49}
50
51pub struct PluginManagerBuilder {
55 plugins: HashMap<String, Arc<Plugin>>,
56 dependency_manager: DependencyManager,
57}
58
59impl PluginManagerBuilder {
60 pub fn new() -> Self {
62 Self {
63 plugins: HashMap::new(),
64 dependency_manager: DependencyManager::new(),
65 }
66 }
67
68 pub fn register_plugin(
75 &mut self,
76 plugin: Arc<Plugin>,
77 ) -> Result<()> {
78 let plugin_name = plugin.spec.tr.metadata().name.clone();
79
80 if self.plugins.contains_key(&plugin_name) {
82 return Err(anyhow::anyhow!("插件 '{}' 已存在", plugin_name));
83 }
84
85 let metadata = plugin.spec.tr.metadata();
87 self.dependency_manager.add_plugin(&metadata.name);
88 for dep in &metadata.dependencies {
89 self.dependency_manager.add_dependency(&metadata.name, dep)?;
90 }
91
92 self.plugins.insert(plugin_name.clone(), plugin);
94
95 tracing::debug!("插件 '{}' 注册成功", plugin_name);
96 Ok(())
97 }
98
99 pub fn build(self) -> Result<PluginManager> {
117 if self.dependency_manager.has_circular_dependencies() {
119 let report =
120 self.dependency_manager.get_circular_dependency_report();
121 return Err(anyhow::anyhow!(
122 "检测到循环依赖: {}",
123 report.to_string()
124 ));
125 }
126
127 let missing_report =
129 self.dependency_manager.check_missing_dependencies();
130 if missing_report.has_missing_dependencies {
131 return Err(anyhow::anyhow!(
132 "检测到缺失依赖: {}",
133 missing_report.to_string()
134 ));
135 }
136
137 let available_plugins: HashSet<String> =
139 self.plugins.keys().cloned().collect();
140 for (name, plugin) in &self.plugins {
141 let metadata = plugin.spec.tr.metadata();
142 for conflict in &metadata.conflicts {
143 if available_plugins.contains(conflict) {
144 return Err(anyhow::anyhow!(
145 "插件 '{}' 与插件 '{}' 冲突",
146 name,
147 conflict
148 ));
149 }
150 }
151 }
152
153 let plugin_order = self.dependency_manager.get_topological_order()?;
155
156 let sorted_plugins: Vec<Arc<Plugin>> = plugin_order
158 .iter()
159 .filter_map(|name| self.plugins.get(name).cloned())
160 .collect();
161
162 tracing::info!(
163 "插件管理器构建完成,共注册 {} 个插件",
164 self.plugins.len()
165 );
166
167 Ok(PluginManager {
168 plugins: Arc::new(self.plugins),
169 sorted_plugins: Arc::new(sorted_plugins),
170 initialized: Arc::new(AtomicBool::new(true)),
171 })
172 }
173}
174
175impl Default for PluginManagerBuilder {
176 fn default() -> Self {
177 Self::new()
178 }
179}
180
181impl PluginManager {
182 pub fn new() -> Self {
184 Self {
185 plugins: Arc::new(HashMap::new()),
186 sorted_plugins: Arc::new(Vec::new()),
187 initialized: Arc::new(AtomicBool::new(true)),
188 }
189 }
190
191 #[inline]
203 pub async fn get_sorted_plugins(&self) -> Vec<Arc<Plugin>> {
204 self.sorted_plugins.as_ref().clone()
206 }
207
208 #[inline]
220 pub fn get_sorted_plugins_sync(&self) -> &[Arc<Plugin>] {
221 self.sorted_plugins.as_ref()
222 }
223
224 #[inline]
228 pub async fn is_initialized(&self) -> bool {
229 self.initialized.load(Ordering::Acquire)
230 }
231
232 #[inline]
234 pub fn is_initialized_sync(&self) -> bool {
235 self.initialized.load(Ordering::Acquire)
236 }
237
238 #[inline]
240 pub fn plugin_count(&self) -> usize {
241 self.plugins.len()
242 }
243
244 #[inline]
250 pub fn get_plugin(
251 &self,
252 name: &str,
253 ) -> Option<&Arc<Plugin>> {
254 self.plugins.get(name)
255 }
256
257 #[inline]
259 pub fn has_plugin(
260 &self,
261 name: &str,
262 ) -> bool {
263 self.plugins.contains_key(name)
264 }
265}
266
267impl Default for PluginManager {
268 fn default() -> Self {
269 Self::new()
270 }
271}