reactive_graph_runtime_service_impl/
shutdown_manager_impl.rs

1use std::sync::Arc;
2use std::sync::atomic::AtomicBool;
3use std::sync::atomic::Ordering;
4
5use crate::command::shutdown::shutdown_command;
6use async_trait::async_trait;
7use reactive_graph_command_model::reactive_graph::command::command::COMMAND;
8use reactive_graph_lifecycle::Lifecycle;
9use reactive_graph_reactive_model_api::ReactivePropertyContainer;
10use reactive_graph_reactive_service_api::ReactiveEntityManager;
11use reactive_graph_runtime_model::reactive_graph::runtime::shutdown::SHUTDOWN;
12use reactive_graph_runtime_service_api::ShutdownManager;
13use reactive_graph_runtime_service_api::UUID_SHUTDOWN;
14use reactive_graph_type_system_api::EntityTypeManager;
15use springtime_di::Component;
16use springtime_di::component_alias;
17
18fn create_shutdown_state() -> Arc<AtomicBool> {
19    Arc::new(AtomicBool::new(false))
20}
21
22#[derive(Component)]
23pub struct ShutdownManagerImpl {
24    entity_type_manager: Arc<dyn EntityTypeManager + Send + Sync>,
25    reactive_entity_manager: Arc<dyn ReactiveEntityManager + Send + Sync>,
26
27    #[component(default = "create_shutdown_state")]
28    shutdown_state: Arc<AtomicBool>,
29}
30
31#[async_trait]
32#[component_alias]
33impl ShutdownManager for ShutdownManagerImpl {
34    fn do_shutdown(&self) {
35        self.shutdown_state.store(true, Ordering::Relaxed);
36    }
37
38    fn is_shutdown(&self) -> bool {
39        self.shutdown_state.load(Ordering::Relaxed)
40    }
41}
42
43#[async_trait]
44impl Lifecycle for ShutdownManagerImpl {
45    async fn init(&self) {
46        // let shutdown_state = self.shutdown_state.clone();
47        // let shutdown_command = shutdown_command(shutdown_state);
48        // let _ = self.reactive_entity_manager.register_reactive_instance(shutdown_command.get_instance());
49    }
50
51    async fn post_init(&self) {
52        let _ = self.entity_type_manager.add_component(&SHUTDOWN, &COMMAND);
53        let shutdown_state = self.shutdown_state.clone();
54        let shutdown_command = shutdown_command(shutdown_state);
55        let _ = self.reactive_entity_manager.register_reactive_instance(shutdown_command.get_instance());
56    }
57
58    async fn shutdown(&self) {
59        // Disconnect reactive streams of the shutdown handler
60        if let Some(shutdown_handler) = self.reactive_entity_manager.get(UUID_SHUTDOWN) {
61            shutdown_handler.remove_all_observers();
62        }
63    }
64}