reactive_graph_plugin_service_api/plugin_container_manager.rs
1use std::path::PathBuf;
2use std::sync::Arc;
3
4use async_trait::async_trait;
5use dashmap::DashSet;
6use springtime_di::injectable;
7use uuid::Uuid;
8
9use crate::PluginTransitionResult;
10use reactive_graph_lifecycle::Lifecycle;
11use reactive_graph_plugin_api::PluginContext;
12use reactive_graph_plugin_api::PluginDependency;
13use reactive_graph_plugin_api::PluginDeployError;
14use reactive_graph_plugin_api::PluginDisableError;
15use reactive_graph_plugin_api::PluginStartError;
16use reactive_graph_plugin_api::PluginState;
17use reactive_graph_plugin_api::PluginStopError;
18use reactive_graph_plugin_api::PluginUninstallError;
19
20#[injectable]
21#[async_trait]
22pub trait PluginContainerManager: Send + Sync + Lifecycle {
23 // Container Management
24
25 /// Creates a new plugin container for a plugin with the given file stem. The dynamically linked
26 /// library is located at the given path which have to be in the plugin installation folder.
27 fn create_and_register_plugin_container(&self, stem: String, path: PathBuf) -> Option<Uuid>;
28
29 /// Returns the plugin with the given id from the list of plugin containers.
30 fn remove_plugin_container(&self, id: &Uuid);
31
32 /// Returns true, if a plugin with the given file stem exists.
33 fn has(&self, stem: &str) -> bool;
34
35 // Getters
36
37 /// Returns the id of the plugin with the given file stem.
38 fn get_id(&self, stem: &str) -> Option<Uuid>;
39
40 /// Returns the file stem of the plugin with the given id.
41 fn get_stem(&self, id: &Uuid) -> Option<String>;
42
43 /// Returns the name of the plugin with the given id.
44 fn name(&self, id: &Uuid) -> Option<String>;
45
46 /// Returns the canonicalized name of the plugin with the given id.
47 fn name_canonicalized(&self, id: &Uuid) -> Option<String>;
48
49 /// Returns the canonicalized name and version of the plugin with the given id.
50 fn name_version(&self, id: &Uuid) -> Option<String>;
51
52 /// Returns the description of the plugin with the given id.
53 fn description(&self, id: &Uuid) -> Option<String>;
54
55 /// Returns the version of the plugin with the given id.
56 fn version(&self, id: &Uuid) -> Option<String>;
57
58 /// Returns the version of the rust compiler the plugin with the given id was compiled with.
59 fn rustc_version(&self, id: &Uuid) -> Option<String>;
60
61 /// Returns the version of the plugin API the plugin with the given id was compiled with.
62 fn plugin_api_version(&self, id: &Uuid) -> Option<String>;
63
64 // Statistics
65
66 /// Returns the count of all plugins.
67 fn count(&self) -> usize;
68
69 /// Returns the count of plugins with the given state.
70 fn count_by_state(&self, state: &PluginState) -> usize;
71
72 /// Returns a string withs stats abouts the states of the plugins
73 fn count_by_states(&self) -> String;
74
75 fn count_by_state_str(&self, state: &PluginState) -> String;
76
77 // Getter
78
79 /// Returns the path of the plugin with the given id.
80 fn get_plugin_path(&self, id: &Uuid) -> Option<String>;
81
82 /// Returns the state of the plugin with the given id.
83 fn get_plugin_state(&self, id: &Uuid) -> Option<PluginState>;
84
85 // Queries
86
87 /// Returns a list of ids of all plugins.
88 fn get_plugins(&self) -> Vec<Uuid>;
89
90 /// Returns a list of ids of plugins with the given state.
91 fn get_plugins_with_state(&self, state: PluginState) -> Vec<Uuid>;
92
93 /// Returns a list of ids of plugins which have one of the both states.
94 fn get_plugins_with_states(&self, state1: PluginState, state2: PluginState) -> Vec<Uuid>;
95
96 /// Returns a list of ids of plugins which doesn't have the given state.
97 fn get_plugins_not_having_state(&self, state: PluginState) -> Vec<Uuid>;
98
99 // Dependency management?
100 /// Returns the id of the plugin by a dependency coordinate.
101 fn get_plugin_by_dependency(&self, plugin_dependency: &PluginDependency) -> Option<Uuid>;
102
103 // Transitions
104
105 /// Deploys the dynamic linked library file from the plugin hot deploy folder into the
106 /// plugin installation folder.
107 ///
108 /// The target filename will contain a timestamp in order to avoid that a cached previous
109 /// version of the DLL will be loaded.
110 fn deploy_dll(&self, id: &Uuid) -> PluginTransitionResult;
111
112 /// Loads the dynamic linked library into memory.
113 fn load_dll(&self, id: &Uuid) -> PluginTransitionResult;
114
115 /// Loads the plugin declaration of the plugin with the given id.
116 ///
117 /// The plugin declaration contains the version of the rust compiler and the plugin API in
118 /// order to check for ABI compatibility.
119 ///
120 /// The plugin have to contain a symbol named 'plugin_declaration'.
121 fn load_plugin_declaration(&self, id: &Uuid) -> PluginTransitionResult;
122
123 /// Performs a compatibility check on the plugin with the given id.
124 ///
125 /// The compatibility check depends on the plugin declaration.
126 ///
127 /// If the plugin is incompatible corresponding error messages will appear and the plugin
128 /// will be uninstalled.
129 fn check_plugin_compatibility(&self, id: &Uuid) -> PluginTransitionResult;
130
131 /// Loads the dependencies of the plugin with the given id.
132 ///
133 /// The list of dependencies depends on the plugin declaration.
134 fn load_plugin_dependencies(&self, id: &Uuid) -> PluginTransitionResult;
135
136 // Dependency Management
137
138 /// Returns the state of the plugin with the given dependency coordinate.
139 fn get_dependency_state(&self, dependency: &PluginDependency) -> PluginState;
140
141 /// Returns true, if the plugin with the given id has one or multiple dependencies.
142 fn has_dependencies(&self, id: &Uuid) -> bool;
143
144 /// Returns a list of dependency coordinates for the plugin with the given id.
145 fn get_dependencies(&self, id: &Uuid) -> DashSet<PluginDependency>;
146
147 /// Returns true, if the plugin with the given id has unsatisfied dependencies.
148 fn has_unsatisfied_dependencies(&self, id: &Uuid) -> bool;
149
150 /// Returns a list of unsatisfied dependencies of the plugin with the given id.
151 fn get_unsatisfied_dependencies(&self, id: &Uuid) -> DashSet<PluginDependency>;
152
153 /// Returns a list of plugin ids which are dependents of the plugin with the given id.
154 fn get_dependents(&self, id: &Uuid) -> Vec<Uuid>;
155
156 // Transitions 2
157
158 /// Sets the given new state of the plugin with the given id.
159 fn set_state(&self, id: &Uuid, new_state: PluginState) -> PluginTransitionResult;
160
161 /// Calculates the state of the dependencies of the plugin with the given id.
162 fn resolve_dependencies_state(&self, id: &Uuid, refreshing: bool) -> PluginTransitionResult;
163
164 /// Constructs a plugin proxy object for the plugin with the given id.
165 ///
166 /// The plugin proxy makes sure it can't outlive the library it came from.
167 fn construct_proxy(&self, id: &Uuid, plugin_context: Arc<dyn PluginContext + Send + Sync>) -> PluginTransitionResult;
168
169 /// Registers providers of the plugin with the given id.
170 fn register(&self, id: &Uuid) -> PluginTransitionResult;
171
172 /// Calls the activate method of the plugin with the given id.
173 async fn activate(&self, id: &Uuid) -> PluginTransitionResult;
174
175 // Lifecycle management
176
177 /// Returns true, if all plugins are stopped.
178 ///
179 /// Plugins are considered as stopped if they are not starting, not active and not stopping.
180 fn are_all_stopped(&self) -> bool;
181
182 // Transitions 3
183
184 /// Calls the deactivate method of the plugin with the given id.
185 async fn deactivate(&self, id: &Uuid) -> PluginTransitionResult;
186
187 /// Unregisters the providers of the plugin with the given id.
188 fn unregister(&self, id: &Uuid) -> PluginTransitionResult;
189
190 /// Removes the plugin proxy of the plugin with the given id.
191 fn remove_proxy(&self, id: &Uuid) -> PluginTransitionResult;
192
193 /// Closes the library by dropping it.
194 fn unload_dll(&self, id: &Uuid) -> PluginTransitionResult;
195
196 /// Deletes the dynamically linked library file.
197 fn uninstall_dll(&self, id: &Uuid) -> PluginTransitionResult;
198
199 // High Level API
200
201 /// Start the plugin with the given id.
202 fn start(&self, id: &Uuid) -> Result<(), PluginStartError>;
203
204 /// Starts the plugin with the given file stem.
205 fn start_by_stem(&self, stem: &str) -> Result<(), PluginStartError>;
206
207 /// Starts all plugins which are dependent of the plugin with the given id and
208 /// have no unsatisfied dependencies.
209 fn start_dependent_with_satisfied_dependencies(&self, id: &Uuid) -> bool;
210
211 /// Stops the given plugin.
212 /// Recursively stops all plugins which depends on the stopped plugin.
213 fn stop(&self, id: &Uuid) -> Result<(), PluginStopError>;
214
215 /// Stops the plugin with the given file stem.
216 fn stop_by_stem(&self, stem: &str) -> Result<(), PluginStopError>;
217
218 /// Stops all plugins.
219 fn stop_all(&self);
220
221 /// Stops active plugins which have unsatisfied dependencies.
222 fn stop_active_with_unsatisfied_dependencies(&self) -> bool;
223
224 /// Uninstalls the plugin with the given id.
225 fn uninstall(&self, id: &Uuid) -> Result<(), PluginUninstallError>;
226
227 /// Redeploys the plugin with the given id.
228 fn redeploy(&self, id: &Uuid) -> Result<(), PluginDeployError>;
229
230 /// Disables the plugin with the given id.
231 fn disable(&self, id: &Uuid) -> Result<(), PluginDisableError>;
232}