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