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 Unhealthy,
19}
20
21#[async_trait]
37pub trait Lifecycle: Send + Sync {
38 fn name(&self) -> &str;
40
41 async fn start(&mut self) -> Result<(), CamelError>;
43
44 async fn stop(&mut self) -> Result<(), CamelError>;
46
47 fn as_metrics_collector(&self) -> Option<Arc<dyn MetricsCollector>> {
49 None
50 }
51
52 fn as_function_invoker(&self) -> Option<Arc<dyn crate::function::FunctionInvoker>> {
53 None
54 }
55
56 fn status(&self) -> ServiceStatus {
58 ServiceStatus::Stopped
59 }
60}
61
62#[cfg(test)]
63mod tests {
64 use super::*;
65
66 struct TestService;
67
68 #[async_trait]
69 impl Lifecycle for TestService {
70 fn name(&self) -> &str {
71 "test"
72 }
73
74 async fn start(&mut self) -> Result<(), CamelError> {
75 Ok(())
76 }
77
78 async fn stop(&mut self) -> Result<(), CamelError> {
79 Ok(())
80 }
81 }
82
83 #[tokio::test]
84 async fn test_lifecycle_trait() {
85 let mut service = TestService;
86 assert_eq!(service.name(), "test");
87 service.start().await.unwrap();
88 service.stop().await.unwrap();
89 }
90
91 #[test]
92 fn test_default_status_is_stopped() {
93 let service = TestService;
94 assert_eq!(service.status(), ServiceStatus::Stopped);
95 }
96}