use crate::Result;
pub trait ServiceTrait: Send + Sync {
fn name(&self) -> &'static str;
fn version(&self) -> &'static str {
"1.0.0"
}
fn description(&self) -> &'static str {
"OpenLark Service"
}
async fn health_check(&self) -> Result<bool>;
async fn start(&self) -> Result<()> {
tracing::info!("服务 '{}' 启动", self.name());
Ok(())
}
async fn stop(&self) -> Result<()> {
tracing::info!("服务 '{}' 停止", self.name());
Ok(())
}
}
pub trait ServiceLifecycle: Send + Sync {
async fn start(&self) -> Result<()> {
tracing::info!("服务启动");
Ok(())
}
async fn stop(&self) -> Result<()> {
tracing::info!("服务停止");
Ok(())
}
async fn restart(&self) -> Result<()> {
tracing::info!("服务重启");
self.stop().await?;
self.start().await
}
async fn health_check(&self) -> Result<bool> {
Ok(true)
}
}
#[cfg(test)]
#[allow(unused_imports)]
mod tests {
use super::*;
struct TestService {
name: &'static str,
}
impl ServiceTrait for TestService {
fn name(&self) -> &'static str {
self.name
}
async fn health_check(&self) -> Result<bool> {
Ok(true)
}
}
#[tokio::test]
async fn test_service_trait() {
let service = TestService {
name: "test_service",
};
assert_eq!(service.name(), "test_service");
assert_eq!(service.version(), "1.0.0");
assert_eq!(service.description(), "OpenLark Service");
let health = service.health_check().await;
assert!(health.is_ok());
assert!(health.unwrap());
}
}