reactive_graph_runtime_impl/
runtime_impl.rs

1use std::sync::Arc;
2use std::sync::atomic::AtomicBool;
3use std::sync::atomic::Ordering;
4use std::time::Duration;
5
6use async_trait::async_trait;
7use log::debug;
8use log::info;
9use springtime_di::Component;
10use springtime_di::component_alias;
11use tokio::time::error::Elapsed;
12
13use reactive_graph_behaviour_service_api::BehaviourSystem;
14use reactive_graph_behaviour_service_api::EntityBehaviourManager;
15use reactive_graph_behaviour_service_api::EntityBehaviourRegistry;
16use reactive_graph_behaviour_service_api::EntityComponentBehaviourManager;
17use reactive_graph_behaviour_service_api::EntityComponentBehaviourRegistry;
18use reactive_graph_behaviour_service_api::RelationBehaviourManager;
19use reactive_graph_behaviour_service_api::RelationBehaviourRegistry;
20use reactive_graph_behaviour_service_api::RelationComponentBehaviourManager;
21use reactive_graph_behaviour_service_api::RelationComponentBehaviourRegistry;
22use reactive_graph_command_api::CommandManager;
23use reactive_graph_command_api::CommandSystem;
24use reactive_graph_command_api::CommandTypeSystemRegistrator;
25use reactive_graph_command_impl::CommandSystemImpl;
26use reactive_graph_config_api::ConfigManager;
27use reactive_graph_config_api::ConfigSystem;
28use reactive_graph_config_impl::ConfigSystemImpl;
29use reactive_graph_dynamic_graph_api::DynamicGraphQueryService;
30use reactive_graph_dynamic_graph_api::DynamicGraphSchemaBuilder;
31use reactive_graph_dynamic_graph_api::DynamicGraphSchemaManager;
32use reactive_graph_dynamic_graph_api::DynamicGraphSystem;
33use reactive_graph_dynamic_graph_api::SchemaBuilderContextManager;
34use reactive_graph_dynamic_graph_api::SchemaBuilderManager;
35use reactive_graph_flow_api::FlowSystem;
36use reactive_graph_flow_api::FlowTypeSystemRegistrator;
37use reactive_graph_flow_impl::flow_system_impl::FlowSystemImpl;
38use reactive_graph_graphql_api::GraphQLQueryService;
39use reactive_graph_graphql_api::GraphQLSchemaManager;
40use reactive_graph_graphql_api::GraphQLSystem;
41use reactive_graph_instance_system_api::EntityInstanceImportExportManager;
42use reactive_graph_instance_system_api::InstanceSystem;
43use reactive_graph_instance_system_api::RelationInstanceImportExportManager;
44use reactive_graph_lifecycle::Lifecycle;
45use reactive_graph_plugin_graphql_api::PluginGraphQLSystem;
46use reactive_graph_plugin_graphql_api::PluginQueryService;
47use reactive_graph_plugin_graphql_api::PluginSchemaManager;
48use reactive_graph_plugin_service_api::PluginContainerManager;
49use reactive_graph_plugin_service_api::PluginContextFactory;
50use reactive_graph_plugin_service_api::PluginRepositoryManager;
51use reactive_graph_plugin_service_api::PluginResolver;
52use reactive_graph_plugin_service_api::PluginSystem;
53use reactive_graph_reactive_service_api::ReactiveEntityManager;
54use reactive_graph_reactive_service_api::ReactiveFlowManager;
55use reactive_graph_reactive_service_api::ReactiveInstanceEventManager;
56use reactive_graph_reactive_service_api::ReactiveRelationManager;
57use reactive_graph_reactive_service_api::ReactiveSystem;
58use reactive_graph_remotes_api::InstanceService;
59use reactive_graph_remotes_api::RemotesManager;
60use reactive_graph_remotes_api::RemotesSystem;
61use reactive_graph_remotes_impl::RemotesSystemImpl;
62use reactive_graph_remotes_model::InstanceAddress;
63use reactive_graph_runtime_api::Runtime;
64use reactive_graph_runtime_graphql_api::RuntimeGraphQLSystem;
65use reactive_graph_runtime_graphql_api::RuntimeQueryService;
66use reactive_graph_runtime_graphql_api::RuntimeSchemaManager;
67use reactive_graph_runtime_service_api::RuntimeSystem;
68use reactive_graph_runtime_service_api::ShutdownManager;
69use reactive_graph_runtime_web_api::GraphQLServer;
70use reactive_graph_runtime_web_api::WebResourceManager;
71use reactive_graph_runtime_web_api::WebSystem;
72use reactive_graph_type_system_api::ComponentImportExportManager;
73use reactive_graph_type_system_api::ComponentManager;
74use reactive_graph_type_system_api::EntityTypeImportExportManager;
75use reactive_graph_type_system_api::EntityTypeManager;
76use reactive_graph_type_system_api::FlowTypeImportExportManager;
77use reactive_graph_type_system_api::FlowTypeManager;
78use reactive_graph_type_system_api::NamespaceTreeManager;
79use reactive_graph_type_system_api::NamespacedTypeManager;
80use reactive_graph_type_system_api::RelationTypeImportExportManager;
81use reactive_graph_type_system_api::RelationTypeManager;
82use reactive_graph_type_system_api::TypeSystemEventManager;
83use reactive_graph_type_system_api::TypeSystemProviderRegistry;
84use reactive_graph_type_system_api::TypeSystemSystem;
85
86pub struct RunningState(Arc<AtomicBool>);
87
88fn create_running_state() -> RunningState {
89    RunningState(Arc::new(AtomicBool::new(false)))
90}
91
92#[derive(Component)]
93pub struct RuntimeImpl {
94    #[component(default = "create_running_state")]
95    running: RunningState,
96    type_system_system: Arc<dyn TypeSystemSystem + Send + Sync>,
97    reactive_system: Arc<dyn ReactiveSystem + Send + Sync>,
98    behaviour_system: Arc<dyn BehaviourSystem + Send + Sync>,
99    instance_system: Arc<dyn InstanceSystem + Send + Sync>,
100    flow_system: Arc<FlowSystemImpl>,
101    command_system: Arc<CommandSystemImpl>,
102    runtime_system: Arc<dyn RuntimeSystem + Send + Sync>,
103    remotes_system: Arc<RemotesSystemImpl>,
104    config_system: Arc<ConfigSystemImpl>,
105    graphql_system: Arc<dyn GraphQLSystem + Send + Sync>,
106    dynamic_graph_system: Arc<dyn DynamicGraphSystem + Send + Sync>,
107    runtime_graphql_system: Arc<dyn RuntimeGraphQLSystem + Send + Sync>,
108    plugin_graphql_system: Arc<dyn PluginGraphQLSystem + Send + Sync>,
109    plugin_system: Arc<dyn PluginSystem + Send + Sync>,
110    web_system: Arc<dyn WebSystem + Send + Sync>,
111}
112
113impl RuntimeImpl {
114    async fn wait_for_started_internal(&self) {
115        // TODO: Add upper bound timeout (for example 30 sec)
116        while !self.is_running() {
117            tokio::time::sleep(Duration::from_millis(100)).await;
118        }
119    }
120}
121
122#[async_trait]
123#[component_alias]
124impl Runtime for RuntimeImpl {
125    async fn config(&self) {
126        self.config_system.init().await;
127    }
128
129    async fn run(&self) {
130        // Signal handling
131        let terminate = Arc::new(AtomicBool::new(false));
132        // This channel allows the main thread to stop the GraphQL server thread
133        let (graphql_server_stop_sender, graphql_server_stop_receiver) = crossbeam::channel::unbounded::<()>();
134        // This channel allows the GraphQL server thread to tell the main thread that it has been finished
135        let (graphql_server_stopped_sender, graphql_server_stopped_receiver) = crossbeam::channel::unbounded::<()>();
136        // Clone GraphQL server and move the reference into the GraphQL server thread
137        let graphql_server = self.web_system.get_graphql_server();
138        // GraphQL server thread: Create a new thread for the GraphQL server
139        // TODO: Use tokio task Builder to name the task
140        // tokio::task::Builder::new().name("rg_server").spawn()
141        let graphql_server_handle = tokio::spawn(async move {
142            // Run the GraphQL server
143            info!("Run the GraphQL server.");
144            graphql_server.serve(graphql_server_stop_receiver).await;
145            debug!("Successfully stopped GraphQL Server.");
146            // Tell the main thread, that the GraphQL server thread has finished
147            let _result = graphql_server_stopped_sender.send(());
148        });
149
150        {
151            self.running.0.store(true, Ordering::Relaxed);
152        }
153
154        {
155            let _r_sigint = signal_hook::flag::register(signal_hook::consts::SIGINT, Arc::clone(&terminate));
156            let _r_sigterm = signal_hook::flag::register(signal_hook::consts::SIGTERM, Arc::clone(&terminate));
157
158            let mut stopping = false;
159            while self.is_running() && !stopping && !terminate.load(Ordering::Relaxed) {
160                tokio::time::sleep(Duration::from_millis(100)).await;
161                let r = graphql_server_stopped_receiver.try_recv();
162                if r.is_ok() {
163                    debug!("Stopping the main thread");
164                    stopping = true;
165                }
166                if self.runtime_system.get_shutdown_manager().is_shutdown() {
167                    stopping = true;
168                }
169            }
170        } // Drop "running"
171
172        // Stop GraphQL server thread, if it is still running
173        debug!("Stopping the GraphQL server thread");
174        let _graphql_server_stop_result = graphql_server_stop_sender.send_timeout((), Duration::from_millis(100));
175
176        // Be sure that the GraphQL server thread is gone
177        let _ = graphql_server_handle.await;
178        info!("Bye.");
179
180        // Ensure the running state is now set to false even if the loop was terminated
181        // externally because the running state is checked from outside.
182        {
183            self.running.0.store(false, Ordering::Relaxed);
184        }
185    }
186
187    fn stop(&self) {
188        {
189            self.running.0.store(false, Ordering::Relaxed);
190        }
191    }
192
193    fn is_running(&self) -> bool {
194        self.running.0.load(Ordering::Relaxed)
195        // *self.running.0.read().unwrap().deref()
196    }
197
198    async fn wait_for_started(&self, timeout_duration: Duration) -> Result<(), Elapsed> {
199        tokio::time::timeout(timeout_duration, self.wait_for_started_internal()).await
200    }
201
202    async fn wait_for_stopped(&self) {
203        while self.is_running() {
204            tokio::time::sleep(Duration::from_millis(100)).await;
205        }
206    }
207
208    async fn wait_for_stopped_with_timeout(&self, timeout_duration: Duration) -> Result<(), Elapsed> {
209        tokio::time::timeout(timeout_duration, self.wait_for_stopped()).await
210    }
211
212    fn address(&self) -> InstanceAddress {
213        self.remotes_system.get_instance_service().get_instance_info().address()
214    }
215}
216
217impl TypeSystemSystem for RuntimeImpl {
218    fn get_component_manager(&self) -> Arc<dyn ComponentManager + Send + Sync> {
219        self.type_system_system.get_component_manager()
220    }
221
222    fn get_component_import_export_manager(&self) -> Arc<dyn ComponentImportExportManager + Send + Sync> {
223        self.type_system_system.get_component_import_export_manager()
224    }
225
226    fn get_entity_type_manager(&self) -> Arc<dyn EntityTypeManager + Send + Sync> {
227        self.type_system_system.get_entity_type_manager()
228    }
229
230    fn get_entity_type_import_export_manager(&self) -> Arc<dyn EntityTypeImportExportManager + Send + Sync> {
231        self.type_system_system.get_entity_type_import_export_manager()
232    }
233
234    fn get_flow_type_manager(&self) -> Arc<dyn FlowTypeManager + Send + Sync> {
235        self.type_system_system.get_flow_type_manager()
236    }
237
238    fn get_flow_type_import_export_manager(&self) -> Arc<dyn FlowTypeImportExportManager + Send + Sync> {
239        self.type_system_system.get_flow_type_import_export_manager()
240    }
241
242    fn get_namespace_tree_manager(&self) -> Arc<dyn NamespaceTreeManager + Send + Sync> {
243        self.type_system_system.get_namespace_tree_manager()
244    }
245
246    fn get_namespaced_type_manager(&self) -> Arc<dyn NamespacedTypeManager + Send + Sync> {
247        self.type_system_system.get_namespaced_type_manager()
248    }
249
250    fn get_relation_type_manager(&self) -> Arc<dyn RelationTypeManager + Send + Sync> {
251        self.type_system_system.get_relation_type_manager()
252    }
253
254    fn get_relation_type_import_export_manager(&self) -> Arc<dyn RelationTypeImportExportManager + Send + Sync> {
255        self.type_system_system.get_relation_type_import_export_manager()
256    }
257
258    fn get_type_system_event_manager(&self) -> Arc<dyn TypeSystemEventManager + Send + Sync> {
259        self.type_system_system.get_type_system_event_manager()
260    }
261
262    fn get_type_system_provider_registry(&self) -> Arc<dyn TypeSystemProviderRegistry + Send + Sync> {
263        self.type_system_system.get_type_system_provider_registry()
264    }
265}
266
267impl ReactiveSystem for RuntimeImpl {
268    fn get_reactive_entity_manager(&self) -> Arc<dyn ReactiveEntityManager + Send + Sync> {
269        self.reactive_system.get_reactive_entity_manager()
270    }
271
272    fn get_reactive_flow_manager(&self) -> Arc<dyn ReactiveFlowManager + Send + Sync> {
273        self.reactive_system.get_reactive_flow_manager()
274    }
275
276    fn get_reactive_relation_manager(&self) -> Arc<dyn ReactiveRelationManager + Send + Sync> {
277        self.reactive_system.get_reactive_relation_manager()
278    }
279
280    fn get_reactive_instance_event_manager(&self) -> Arc<dyn ReactiveInstanceEventManager + Send + Sync> {
281        self.reactive_system.get_reactive_instance_event_manager()
282    }
283
284    fn type_system_system(&self) -> Arc<dyn TypeSystemSystem + Send + Sync> {
285        self.reactive_system.type_system_system()
286    }
287
288    fn behaviour_system(&self) -> Arc<dyn BehaviourSystem + Send + Sync> {
289        self.reactive_system.behaviour_system()
290    }
291}
292
293impl BehaviourSystem for RuntimeImpl {
294    fn get_entity_behaviour_manager(&self) -> Arc<dyn EntityBehaviourManager + Send + Sync> {
295        self.behaviour_system.get_entity_behaviour_manager()
296    }
297
298    fn get_entity_behaviour_registry(&self) -> Arc<dyn EntityBehaviourRegistry + Send + Sync> {
299        self.behaviour_system.get_entity_behaviour_registry()
300    }
301
302    fn get_entity_component_behaviour_manager(&self) -> Arc<dyn EntityComponentBehaviourManager + Send + Sync> {
303        self.behaviour_system.get_entity_component_behaviour_manager()
304    }
305
306    fn get_entity_component_behaviour_registry(&self) -> Arc<dyn EntityComponentBehaviourRegistry + Send + Sync> {
307        self.behaviour_system.get_entity_component_behaviour_registry()
308    }
309
310    fn get_relation_behaviour_manager(&self) -> Arc<dyn RelationBehaviourManager + Send + Sync> {
311        self.behaviour_system.get_relation_behaviour_manager()
312    }
313
314    fn get_relation_behaviour_registry(&self) -> Arc<dyn RelationBehaviourRegistry + Send + Sync> {
315        self.behaviour_system.get_relation_behaviour_registry()
316    }
317
318    fn get_relation_component_behaviour_manager(&self) -> Arc<dyn RelationComponentBehaviourManager + Send + Sync> {
319        self.behaviour_system.get_relation_component_behaviour_manager()
320    }
321
322    fn get_relation_component_behaviour_registry(&self) -> Arc<dyn RelationComponentBehaviourRegistry + Send + Sync> {
323        self.behaviour_system.get_relation_component_behaviour_registry()
324    }
325
326    fn type_system_system(&self) -> Arc<dyn TypeSystemSystem + Send + Sync> {
327        self.behaviour_system.type_system_system()
328    }
329}
330
331impl InstanceSystem for RuntimeImpl {
332    fn get_entity_instance_import_export_manager(&self) -> Arc<dyn EntityInstanceImportExportManager + Send + Sync> {
333        self.instance_system.get_entity_instance_import_export_manager()
334    }
335
336    fn get_relation_instance_import_export_manager(&self) -> Arc<dyn RelationInstanceImportExportManager + Send + Sync> {
337        self.instance_system.get_relation_instance_import_export_manager()
338    }
339
340    fn reactive_system(&self) -> Arc<dyn ReactiveSystem + Send + Sync> {
341        self.reactive_system.clone()
342    }
343}
344
345impl FlowSystem for RuntimeImpl {
346    fn get_flow_type_system_registrator(&self) -> Arc<dyn FlowTypeSystemRegistrator + Send + Sync> {
347        self.flow_system.get_flow_type_system_registrator()
348    }
349}
350
351impl CommandSystem for RuntimeImpl {
352    fn get_command_manager(&self) -> Arc<dyn CommandManager + Send + Sync> {
353        self.command_system.get_command_manager()
354    }
355
356    fn get_command_type_system_registrator(&self) -> Arc<dyn CommandTypeSystemRegistrator + Send + Sync> {
357        self.command_system.get_command_type_system_registrator()
358    }
359
360    fn type_system_system(&self) -> Arc<dyn TypeSystemSystem + Send + Sync> {
361        self.command_system.type_system_system()
362    }
363
364    fn reactive_system(&self) -> Arc<dyn ReactiveSystem + Send + Sync> {
365        self.command_system.reactive_system()
366    }
367}
368
369impl ConfigSystem for RuntimeImpl {
370    fn get_config_manager(&self) -> Arc<dyn ConfigManager + Send + Sync> {
371        self.config_system.get_config_manager()
372    }
373}
374
375impl RemotesSystem for RuntimeImpl {
376    fn get_instance_service(&self) -> Arc<dyn InstanceService + Send + Sync> {
377        self.remotes_system.get_instance_service()
378    }
379
380    fn get_remotes_manager(&self) -> Arc<dyn RemotesManager + Send + Sync> {
381        self.remotes_system.get_remotes_manager()
382    }
383
384    fn config_system(&self) -> Arc<dyn ConfigSystem + Send + Sync> {
385        self.remotes_system.config_system()
386    }
387}
388
389impl RuntimeSystem for RuntimeImpl {
390    fn get_shutdown_manager(&self) -> Arc<dyn ShutdownManager + Send + Sync> {
391        self.runtime_system.get_shutdown_manager()
392    }
393}
394
395impl WebSystem for RuntimeImpl {
396    fn get_graphql_server(&self) -> Arc<dyn GraphQLServer + Send + Sync> {
397        self.web_system.get_graphql_server()
398    }
399
400    fn get_web_resource_manager(&self) -> Arc<dyn WebResourceManager + Send + Sync> {
401        self.web_system.get_web_resource_manager()
402    }
403
404    fn type_system_system(&self) -> Arc<dyn TypeSystemSystem + Send + Sync> {
405        self.web_system.type_system_system()
406    }
407
408    fn reactive_system(&self) -> Arc<dyn ReactiveSystem + Send + Sync> {
409        self.web_system.reactive_system()
410    }
411
412    fn config_system(&self) -> Arc<dyn ConfigSystem + Send + Sync> {
413        self.web_system.config_system()
414    }
415
416    fn runtime_graphql_system(&self) -> Arc<dyn RuntimeGraphQLSystem + Send + Sync> {
417        self.web_system.runtime_graphql_system()
418    }
419
420    fn plugin_graphql_system(&self) -> Arc<dyn PluginGraphQLSystem + Send + Sync> {
421        self.web_system.plugin_graphql_system()
422    }
423
424    fn dynamic_graph_system(&self) -> Arc<dyn DynamicGraphSystem + Send + Sync> {
425        self.web_system.dynamic_graph_system()
426    }
427
428    fn graphql_system(&self) -> Arc<dyn GraphQLSystem + Send + Sync> {
429        self.web_system.graphql_system()
430    }
431}
432
433impl GraphQLSystem for RuntimeImpl {
434    fn get_graphql_query_service(&self) -> Arc<dyn GraphQLQueryService + Send + Sync> {
435        self.graphql_system.get_graphql_query_service()
436    }
437
438    fn get_graphql_schema_manager(&self) -> Arc<dyn GraphQLSchemaManager + Send + Sync> {
439        self.graphql_system.get_graphql_schema_manager()
440    }
441}
442
443impl DynamicGraphSystem for RuntimeImpl {
444    fn get_dynamic_graph_query_service(&self) -> Arc<dyn DynamicGraphQueryService + Send + Sync> {
445        self.dynamic_graph_system.get_dynamic_graph_query_service()
446    }
447
448    fn get_dynamic_graph_schema_builder(&self) -> Arc<dyn DynamicGraphSchemaBuilder + Send + Sync> {
449        self.dynamic_graph_system.get_dynamic_graph_schema_builder()
450    }
451
452    fn get_dynamic_graph_schema_manager(&self) -> Arc<dyn DynamicGraphSchemaManager + Send + Sync> {
453        self.dynamic_graph_system.get_dynamic_graph_schema_manager()
454    }
455
456    fn get_schema_builder_context_manager(&self) -> Arc<dyn SchemaBuilderContextManager + Send + Sync> {
457        self.dynamic_graph_system.get_schema_builder_context_manager()
458    }
459
460    fn get_schema_builder_manager(&self) -> Arc<dyn SchemaBuilderManager + Send + Sync> {
461        self.dynamic_graph_system.get_schema_builder_manager()
462    }
463
464    fn type_system_system(&self) -> Arc<dyn TypeSystemSystem + Send + Sync> {
465        self.dynamic_graph_system.type_system_system()
466    }
467
468    fn reactive_system(&self) -> Arc<dyn ReactiveSystem + Send + Sync> {
469        self.dynamic_graph_system.reactive_system()
470    }
471}
472
473impl RuntimeGraphQLSystem for RuntimeImpl {
474    fn get_runtime_query_service(&self) -> Arc<dyn RuntimeQueryService + Send + Sync> {
475        self.runtime_graphql_system.get_runtime_query_service()
476    }
477
478    fn get_runtime_schema_manager(&self) -> Arc<dyn RuntimeSchemaManager + Send + Sync> {
479        self.runtime_graphql_system.get_runtime_schema_manager()
480    }
481}
482
483impl PluginGraphQLSystem for RuntimeImpl {
484    fn get_plugin_query_service(&self) -> Arc<dyn PluginQueryService + Send + Sync> {
485        self.plugin_graphql_system.get_plugin_query_service()
486    }
487
488    fn get_plugin_schema_manager(&self) -> Arc<dyn PluginSchemaManager + Send + Sync> {
489        self.plugin_graphql_system.get_plugin_schema_manager()
490    }
491}
492
493impl PluginSystem for RuntimeImpl {
494    fn get_plugin_context_factory(&self) -> Arc<dyn PluginContextFactory + Send + Sync> {
495        self.plugin_system.get_plugin_context_factory()
496    }
497
498    fn get_plugin_container_manager(&self) -> Arc<dyn PluginContainerManager + Send + Sync> {
499        self.plugin_system.get_plugin_container_manager()
500    }
501
502    fn get_plugin_repository_manager(&self) -> Arc<dyn PluginRepositoryManager + Send + Sync> {
503        self.plugin_system.get_plugin_repository_manager()
504    }
505
506    fn get_plugin_resolver(&self) -> Arc<dyn PluginResolver + Send + Sync> {
507        self.plugin_system.get_plugin_resolver()
508    }
509}
510
511#[async_trait]
512impl Lifecycle for RuntimeImpl {
513    async fn init(&self) {
514        // Order matters
515        self.type_system_system.init().await;
516        self.reactive_system.init().await;
517        self.behaviour_system.init().await;
518        self.instance_system.init().await;
519        self.flow_system.init().await;
520        //
521        self.runtime_system.init().await;
522        self.command_system.init().await;
523        // self.shutdown_manager.init().await;
524        self.remotes_system.init().await;
525        //
526        self.graphql_system.init().await;
527        self.dynamic_graph_system.init().await;
528        //
529        self.web_system.init().await;
530        // self.web_resource_manager.init().await;
531        // self.graphql_server.init().await;
532        //
533        self.plugin_system.init().await;
534    }
535
536    async fn post_init(&self) {
537        // Order matters
538        self.type_system_system.post_init().await;
539        self.reactive_system.post_init().await;
540        self.behaviour_system.post_init().await;
541        self.instance_system.post_init().await;
542        self.flow_system.post_init().await;
543        //
544        self.runtime_system.post_init().await;
545        self.command_system.post_init().await;
546        // self.shutdown_manager.post_init().await;
547        self.remotes_system.post_init().await;
548        //
549        self.graphql_system.post_init().await;
550        self.dynamic_graph_system.post_init().await;
551        //
552        self.web_system.post_init().await;
553        // self.web_resource_manager.post_init().await;
554        // self.graphql_server.post_init().await;
555        //
556        self.plugin_system.post_init().await;
557    }
558
559    async fn pre_shutdown(&self) {
560        // Reverse order matters
561        self.plugin_system.pre_shutdown().await;
562        //
563        // self.graphql_server.pre_shutdown().await;
564        // self.web_resource_manager.pre_shutdown().await;
565        self.web_system.pre_shutdown().await;
566        //
567        self.dynamic_graph_system.pre_shutdown().await;
568        self.graphql_system.pre_shutdown().await;
569        //
570        self.remotes_system.pre_shutdown().await;
571        // self.shutdown_manager.pre_shutdown().await;
572        self.command_system.pre_shutdown().await;
573        self.runtime_system.pre_shutdown().await;
574        //
575        self.flow_system.pre_shutdown().await;
576        self.instance_system.pre_shutdown().await;
577        self.behaviour_system.pre_shutdown().await;
578        self.reactive_system.pre_shutdown().await;
579        self.type_system_system.pre_shutdown().await;
580    }
581
582    async fn shutdown(&self) {
583        // Reverse order matters
584        self.plugin_system.shutdown().await;
585        //
586        // self.graphql_server.shutdown().await;
587        // self.web_resource_manager.shutdown().await;
588        self.web_system.shutdown().await;
589        //
590        self.dynamic_graph_system.shutdown().await;
591        self.graphql_system.shutdown().await;
592        //
593        self.remotes_system.shutdown().await;
594        // self.shutdown_manager.shutdown().await;
595        self.command_system.shutdown().await;
596        self.runtime_system.shutdown().await;
597        //
598        self.flow_system.shutdown().await;
599        self.instance_system.shutdown().await;
600        self.behaviour_system.shutdown().await;
601        self.reactive_system.shutdown().await;
602        self.type_system_system.shutdown().await;
603    }
604}
605
606#[cfg(test)]
607mod tests {
608    use std::time::Duration;
609
610    use log::LevelFilter;
611    use log4rs::Config;
612    use log4rs::append::console::ConsoleAppender;
613    use log4rs::config::Appender;
614    use log4rs::config::Root;
615
616    use crate::get_runtime;
617
618    /// This starts the runtime in an async environment.
619    ///
620    /// The runtime will be started including GraphQL server and fully
621    /// initialized. After 2 seconds the runtime will be stopped.
622    #[tokio::test(flavor = "multi_thread")]
623    async fn test_run() {
624        let stdout = ConsoleAppender::builder().build();
625        let config = Config::builder()
626            .appender(Appender::builder().build("stdout", Box::new(stdout)))
627            .build(Root::builder().appender("stdout").build(LevelFilter::Trace))
628            .expect("Failed to create logger");
629        if let Err(error) = log4rs::init_config(config) {
630            eprintln!("Failed to configure logger: {}", error);
631        }
632        let rt = get_runtime();
633        let runtime = rt.clone();
634        tokio::spawn(async move {
635            let runtime = runtime;
636            runtime.init().await;
637            runtime.post_init().await;
638            runtime.run().await;
639            runtime.pre_shutdown().await;
640            runtime.shutdown().await;
641        });
642        tokio::time::sleep(Duration::from_secs(2)).await;
643        rt.stop();
644    }
645}