openlark_client/traits/
service.rs1use crate::Result;
6
7pub trait ServiceTrait: Send + Sync {
16 fn name(&self) -> &'static str;
18
19 fn version(&self) -> &'static str {
21 "1.0.0"
22 }
23
24 fn description(&self) -> &'static str {
26 "OpenLark Service"
27 }
28
29 async fn health_check(&self) -> Result<bool>;
31
32 async fn start(&self) -> Result<()> {
34 tracing::info!("服务 '{}' 启动", self.name());
35 Ok(())
36 }
37
38 async fn stop(&self) -> Result<()> {
40 tracing::info!("服务 '{}' 停止", self.name());
41 Ok(())
42 }
43}
44
45pub trait ServiceLifecycle: Send + Sync {
49 async fn start(&self) -> Result<()> {
51 tracing::info!("服务启动");
52 Ok(())
53 }
54
55 async fn stop(&self) -> Result<()> {
57 tracing::info!("服务停止");
58 Ok(())
59 }
60
61 async fn restart(&self) -> Result<()> {
63 tracing::info!("服务重启");
64 self.stop().await?;
65 self.start().await
66 }
67
68 async fn health_check(&self) -> Result<bool> {
70 Ok(true)
71 }
72}
73
74#[cfg(test)]
75#[allow(unused_imports)]
76mod tests {
77 use super::*;
78
79 struct TestService {
80 name: &'static str,
81 }
82
83 impl ServiceTrait for TestService {
84 fn name(&self) -> &'static str {
85 self.name
86 }
87
88 async fn health_check(&self) -> Result<bool> {
89 Ok(true)
90 }
91 }
92
93 #[tokio::test]
94 async fn test_service_trait() {
95 let service = TestService {
96 name: "test_service",
97 };
98
99 assert_eq!(service.name(), "test_service");
100 assert_eq!(service.version(), "1.0.0");
101 assert_eq!(service.description(), "OpenLark Service");
102
103 let health = service.health_check().await;
104 assert!(health.is_ok());
105 assert!(health.unwrap());
106 }
107}