reifydb_sub_api/
subsystem.rs

1// Copyright (c) reifydb.com 2025
2// This file is licensed under the AGPL-3.0-or-later, see license.md file
3
4use std::any::Any;
5
6use reifydb_core::{
7	interceptor::StandardInterceptorBuilder,
8	interface::{CommandTransaction, version::HasVersion},
9	ioc::IocContainer,
10};
11
12/// Uniform interface that all subsystems must implement
13///
14/// This trait provides a consistent lifecycle and monitoring interface
15/// for all subsystems managed by the Database.
16pub trait Subsystem: Send + Sync + Any + HasVersion {
17	/// Get the unique name of this subsystem
18	fn name(&self) -> &'static str;
19	/// Start the subsystem
20	///
21	/// This method should initialize the subsystem and start any background
22	/// threads or processes. It should be idempotent - calling start() on
23	/// an already running subsystem should succeed without side effects.
24	fn start(&mut self) -> reifydb_core::Result<()>;
25	/// Shutdown the subsystem
26	///
27	/// This method should gracefully shut down the subsystem and clean up
28	/// any resources. This is a terminal operation - once shutdown, the
29	/// subsystem cannot be restarted. It should be idempotent - calling
30	/// shutdown() on an already shutdown subsystem should succeed without
31	/// side effects.
32	fn shutdown(&mut self) -> reifydb_core::Result<()>;
33
34	/// Check if the subsystem is currently running
35	fn is_running(&self) -> bool;
36
37	/// Get the current health status of the subsystem
38	///
39	/// This should provide information about the subsystem's operational
40	/// status and any errors or warnings.
41	fn health_status(&self) -> HealthStatus;
42
43	/// Get a reference to self as Any for downcasting
44	fn as_any(&self) -> &dyn Any;
45
46	/// Get a mutable reference to self as Any for downcasting
47	fn as_any_mut(&mut self) -> &mut dyn Any;
48}
49
50/// Factory trait for creating subsystems with IoC support
51pub trait SubsystemFactory<CT: CommandTransaction> {
52	fn provide_interceptors(
53		&self,
54		builder: StandardInterceptorBuilder<CT>,
55		_ioc: &IocContainer,
56	) -> StandardInterceptorBuilder<CT> {
57		builder
58	}
59
60	fn create(self: Box<Self>, ioc: &IocContainer) -> reifydb_core::Result<Box<dyn Subsystem>>;
61}
62
63#[derive(Debug, Clone, PartialEq)]
64pub enum HealthStatus {
65	Healthy,
66	Warning {
67		description: String,
68	},
69	Degraded {
70		description: String,
71	},
72	Failed {
73		description: String,
74	},
75	Unknown,
76}
77
78impl HealthStatus {
79	pub fn is_healthy(&self) -> bool {
80		matches!(self, HealthStatus::Healthy)
81	}
82
83	pub fn is_failed(&self) -> bool {
84		matches!(self, HealthStatus::Failed { .. })
85	}
86
87	pub fn description(&self) -> &str {
88		match self {
89			HealthStatus::Healthy => "Healthy",
90			HealthStatus::Warning {
91				description: message,
92			} => message,
93			HealthStatus::Degraded {
94				description: message,
95			} => message,
96			HealthStatus::Failed {
97				description: message,
98			} => message,
99			HealthStatus::Unknown => "Unknown",
100		}
101	}
102}