1use crate::{CamelError, MetricsCollector};
2use async_trait::async_trait;
3use serde::{Deserialize, Serialize};
4use std::sync::Arc;
5
6#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
8pub enum ServiceStatus {
9 Stopped,
10 Started,
11 Failed,
12}
13
14#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
16pub enum HealthStatus {
17 Healthy,
18 Degraded,
20 Unhealthy,
21}
22
23#[async_trait]
39pub trait Lifecycle: Send + Sync {
40 fn name(&self) -> &str;
42
43 async fn start(&mut self) -> Result<(), CamelError>;
45
46 async fn stop(&mut self) -> Result<(), CamelError>;
48
49 fn as_metrics_collector(&self) -> Option<Arc<dyn MetricsCollector>> {
51 None
52 }
53
54 fn as_function_invoker(&self) -> Option<Arc<dyn crate::function::FunctionInvoker>> {
55 None
56 }
57
58 fn status(&self) -> ServiceStatus {
60 ServiceStatus::Stopped
61 }
62}
63
64#[cfg(test)]
65mod tests {
66 use super::*;
67
68 struct TestService;
69
70 #[async_trait]
71 impl Lifecycle for TestService {
72 fn name(&self) -> &str {
73 "test"
74 }
75
76 async fn start(&mut self) -> Result<(), CamelError> {
77 Ok(())
78 }
79
80 async fn stop(&mut self) -> Result<(), CamelError> {
81 Ok(())
82 }
83 }
84
85 #[tokio::test]
86 async fn test_lifecycle_trait() {
87 let mut service = TestService;
88 assert_eq!(service.name(), "test");
89 service.start().await.unwrap();
90 service.stop().await.unwrap();
91 }
92
93 #[test]
94 fn test_default_status_is_stopped() {
95 let service = TestService;
96 assert_eq!(service.status(), ServiceStatus::Stopped);
97 }
98}