Skip to main content

reifydb_sub_api/
subsystem.rs

1// SPDX-License-Identifier: Apache-2.0
2// Copyright (c) 2025 ReifyDB
3
4use std::any::Any;
5
6use reifydb_core::{interface::version::HasVersion, util::ioc::IocContainer};
7use reifydb_transaction::interceptor::builder::InterceptorBuilder;
8use reifydb_type::Result;
9
10pub trait Subsystem: Any + HasVersion {
11	fn name(&self) -> &'static str;
12
13	fn start(&mut self) -> Result<()>;
14
15	fn shutdown(&mut self) -> Result<()>;
16
17	fn is_running(&self) -> bool;
18
19	fn health_status(&self) -> HealthStatus;
20
21	fn as_any(&self) -> &dyn Any;
22
23	fn as_any_mut(&mut self) -> &mut dyn Any;
24}
25
26pub trait SubsystemFactory: Send {
27	fn provide_interceptors(&self, builder: InterceptorBuilder, _ioc: &IocContainer) -> InterceptorBuilder {
28		builder
29	}
30
31	fn create(self: Box<Self>, ioc: &IocContainer) -> Result<Box<dyn Subsystem>>;
32}
33
34#[derive(Debug, Clone, PartialEq)]
35pub enum HealthStatus {
36	Healthy,
37	Warning {
38		description: String,
39	},
40	Degraded {
41		description: String,
42	},
43	Failed {
44		description: String,
45	},
46	Unknown,
47}
48
49impl HealthStatus {
50	pub fn is_healthy(&self) -> bool {
51		matches!(self, HealthStatus::Healthy)
52	}
53
54	pub fn is_failed(&self) -> bool {
55		matches!(self, HealthStatus::Failed { .. })
56	}
57
58	pub fn description(&self) -> &str {
59		match self {
60			HealthStatus::Healthy => "Healthy",
61			HealthStatus::Warning {
62				description: message,
63			} => message,
64			HealthStatus::Degraded {
65				description: message,
66			} => message,
67			HealthStatus::Failed {
68				description: message,
69			} => message,
70			HealthStatus::Unknown => "Unknown",
71		}
72	}
73}