Skip to main content

mecha10_core/context/
mod.rs

1//! Node execution context
2//!
3//! Provides a high-level API for nodes to interact with the framework,
4//! including type-safe pub/sub, configuration access, and lifecycle management.
5//!
6//! This module has been refactored into smaller sub-modules for better organization:
7//! - `infra_config`: Infrastructure configuration types
8//! - `receiver`: Message receiver wrapper
9//! - `builder`: Builder for constructing a `Context`
10//! - `lifecycle`: Construction, instance naming, shutdown, logging span
11//! - `config`: Infrastructure configuration loading (Redis URLs, control plane)
12//! - `node_config`: Node-level configuration loading with environment fallback
13//! - `pubsub`: Type-safe and raw publish/subscribe methods
14//! - `aggregation`: Pattern-based topic discovery and aggregated subscriptions
15//! - `topics`: Convenience helpers for well-known topic categories
16//! - `state_mgmt`: Access to the per-node persistent state manager
17
18mod aggregation;
19mod builder;
20mod config;
21mod infra_config;
22mod lifecycle;
23mod node_config;
24mod pubsub;
25mod receiver;
26mod state_mgmt;
27mod topics;
28
29pub use builder::ContextBuilder;
30pub use infra_config::{BridgeConfig, InfrastructureConfig, MinioConfig, MlflowConfig, PostgresConfig, RedisConfig};
31pub use node_config::split_base_and_custom;
32pub use receiver::Receiver;
33
34use std::sync::Arc;
35use tokio::sync::RwLock;
36
37pub struct Context {
38    node_id: String,
39
40    instance: Option<String>,
41
42    #[cfg(feature = "messaging")]
43    message_bus: Arc<RwLock<mecha10_messaging::MessageBus>>,
44
45    #[cfg(feature = "messaging")]
46    control_plane_bus: Arc<RwLock<Option<mecha10_messaging::MessageBus>>>,
47
48    #[cfg(feature = "messaging")]
49    redis_bridge: Arc<RwLock<Option<crate::redis_bridge::RedisBridge>>>,
50
51    shutdown: Arc<RwLock<bool>>,
52
53    state_manager: Arc<RwLock<Option<Arc<crate::state::ConcreteStateManager>>>>,
54}
55impl Clone for Context {
56    fn clone(&self) -> Self {
57        Self {
58            node_id: self.node_id.clone(),
59            instance: self.instance.clone(),
60            #[cfg(feature = "messaging")]
61            message_bus: Arc::clone(&self.message_bus),
62            #[cfg(feature = "messaging")]
63            control_plane_bus: Arc::clone(&self.control_plane_bus),
64            #[cfg(feature = "messaging")]
65            redis_bridge: Arc::clone(&self.redis_bridge),
66            shutdown: Arc::clone(&self.shutdown),
67            state_manager: Arc::clone(&self.state_manager),
68        }
69    }
70}