reactive_graph_plugin_graphql_schema/mutation/
mod.rs

1use std::sync::Arc;
2
3use async_graphql::Context;
4use async_graphql::Error;
5use async_graphql::Object;
6use async_graphql::Result;
7
8use reactive_graph_plugin_service_api::PluginContainerManager;
9use reactive_graph_plugin_service_api::PluginResolver;
10
11use crate::query::GraphQLPlugin;
12
13pub struct PluginMutation;
14
15/// Mutations for the plugin system.
16#[Object(name = "Mutation")]
17impl PluginMutation {
18    /// Stops the plugin with the given name.
19    async fn stop(&self, context: &Context<'_>, name: String) -> Result<GraphQLPlugin> {
20        let plugin_container_manager = context.data::<Arc<dyn PluginContainerManager + Send + Sync>>()?;
21        let plugin_resolver = context.data::<Arc<dyn PluginResolver + Send + Sync>>()?;
22        let id = plugin_container_manager.get_id(&name).ok_or_else(|| Error::new("Plugin with name not found"))?;
23        // Stop plugin
24        plugin_container_manager
25            .stop(&id)
26            .map_err(|e| Error::new(format!("Failed to start {}: {:?}", &id, e)))?;
27        // Make all transitions until the plugin and all dependent plugins have stopped
28        plugin_resolver.resolve_until_idle().await;
29        plugin_resolver.transition_to_fallback_states().await;
30        Ok(GraphQLPlugin { id })
31    }
32
33    /// Starts the plugin with the given name.
34    async fn start(&self, context: &Context<'_>, name: String) -> Result<GraphQLPlugin> {
35        let plugin_container_manager = context.data::<Arc<dyn PluginContainerManager + Send + Sync>>()?;
36        let plugin_resolver = context.data::<Arc<dyn PluginResolver + Send + Sync>>()?;
37        let id = plugin_container_manager.get_id(&name).ok_or_else(|| Error::new("Plugin with name not found"))?;
38        // Start plugin
39        plugin_container_manager
40            .start(&id)
41            .map_err(|e| Error::new(format!("Failed to start {}: {:?}", &id, e)))?;
42        // Make all transitions until the plugin has started
43        plugin_resolver.resolve_until_idle().await;
44        // Start dependent plugins
45        while plugin_container_manager.start_dependent_with_satisfied_dependencies(&id) {
46            // Resolve until all dependent plugins are started
47            plugin_resolver.resolve_until_idle().await;
48        }
49        plugin_resolver.transition_to_fallback_states().await;
50        Ok(GraphQLPlugin { id })
51    }
52
53    /// Restarts the plugin with the given name.
54    async fn restart(&self, context: &Context<'_>, name: String) -> Result<GraphQLPlugin> {
55        let plugin_container_manager = context.data::<Arc<dyn PluginContainerManager + Send + Sync>>()?;
56        let plugin_resolver = context.data::<Arc<dyn PluginResolver + Send + Sync>>()?;
57        let id = plugin_container_manager.get_id(&name).ok_or_else(|| Error::new("Plugin with name not found"))?;
58        // Stop plugin
59        plugin_container_manager
60            .stop(&id)
61            .map_err(|e| Error::new(format!("Failed to start {}: {:?}", &id, e)))?;
62        // Make all transitions until the plugin and all dependent plugins have stopped
63        plugin_resolver.resolve_until_idle().await;
64        // Start plugin
65        plugin_container_manager
66            .start(&id)
67            .map_err(|e| Error::new(format!("Failed to start {}: {:?}", &id, e)))?;
68        // Make all transitions until the plugin has started
69        plugin_resolver.resolve_until_idle().await;
70        // Start dependent plugins
71        while plugin_container_manager.start_dependent_with_satisfied_dependencies(&id) {
72            // Resolve until all dependent plugins are started
73            plugin_resolver.resolve_until_idle().await;
74        }
75        plugin_resolver.transition_to_fallback_states().await;
76        Ok(GraphQLPlugin { id })
77    }
78
79    /// Uninstalls the plugin with the given name.
80    async fn uninstall(&self, context: &Context<'_>, name: String) -> Result<bool> {
81        let plugin_container_manager = context.data::<Arc<dyn PluginContainerManager + Send + Sync>>()?;
82        let plugin_resolver = context.data::<Arc<dyn PluginResolver + Send + Sync>>()?;
83        let id = plugin_container_manager.get_id(&name).ok_or_else(|| Error::new("Plugin with name not found"))?;
84        // plugin_container_manager.set_state(&id)
85        plugin_container_manager
86            .uninstall(&id)
87            .map_err(|e| Error::new(format!("Failed to uninstall {}: {:?}", &id, e)))?;
88        plugin_resolver.resolve_until_idle().await;
89        plugin_resolver.transition_to_fallback_states().await;
90        Ok(true)
91    }
92
93    /// Redeploys a plugin which is already installed, resolved or active.
94    async fn redeploy(&self, context: &Context<'_>, name: String) -> Result<GraphQLPlugin> {
95        let plugin_container_manager = context.data::<Arc<dyn PluginContainerManager + Send + Sync>>()?;
96        let plugin_resolver = context.data::<Arc<dyn PluginResolver + Send + Sync>>()?;
97        let id = plugin_container_manager.get_id(&name).ok_or_else(|| Error::new("Plugin with name not found"))?;
98        plugin_container_manager
99            .redeploy(&id)
100            .map_err(|e| Error::new(format!("Failed to start {}: {:?}", &id, e)))?;
101        plugin_resolver.resolve_until_idle().await;
102        plugin_resolver.transition_to_fallback_states().await;
103        Ok(GraphQLPlugin { id })
104    }
105}