use async_trait::async_trait;
use std::sync::Arc;
use super::Service;
#[cfg(unix)]
use crate::server::ListenFds;
use crate::server::ShutdownWatch;
#[async_trait]
pub trait BackgroundService {
async fn start(&self, mut shutdown: ShutdownWatch);
}
pub struct GenBackgroundService<A> {
name: String,
task: Arc<A>,
pub threads: Option<usize>,
}
impl<A> GenBackgroundService<A> {
pub fn new(name: String, task: Arc<A>) -> Self {
Self {
name,
task,
threads: Some(1),
}
}
pub fn task(&self) -> Arc<A> {
self.task.clone()
}
}
#[async_trait]
impl<A> Service for GenBackgroundService<A>
where
A: BackgroundService + Send + Sync + 'static,
{
async fn start_service(
&mut self,
#[cfg(unix)] _fds: Option<ListenFds>,
shutdown: ShutdownWatch,
) {
self.task.start(shutdown).await;
}
fn name(&self) -> &str {
&self.name
}
fn threads(&self) -> Option<usize> {
self.threads
}
}
pub fn background_service<SV>(name: &str, task: SV) -> GenBackgroundService<SV> {
GenBackgroundService::new(format!("BG {name}"), Arc::new(task))
}