2024-07-16 21:33:28 +00:00
|
|
|
use std::{any::Any, collections::BTreeMap, fmt::Write, sync::Arc};
|
2024-07-04 03:26:19 +00:00
|
|
|
|
|
|
|
use async_trait::async_trait;
|
|
|
|
use conduit::{utils::string::split_once_infallible, Result, Server};
|
|
|
|
use database::Database;
|
|
|
|
|
|
|
|
#[async_trait]
|
2024-07-16 21:33:28 +00:00
|
|
|
pub(crate) trait Service: Any + Send + Sync {
|
2024-07-04 03:26:19 +00:00
|
|
|
/// Implement the construction of the service instance. Services are
|
|
|
|
/// generally singletons so expect this to only be called once for a
|
|
|
|
/// service type. Note that it may be called again after a server reload,
|
|
|
|
/// but the prior instance will have been dropped first. Failure will
|
|
|
|
/// shutdown the server with an error.
|
|
|
|
fn build(args: Args<'_>) -> Result<Arc<impl Service>>
|
|
|
|
where
|
|
|
|
Self: Sized;
|
|
|
|
|
2024-07-11 21:00:30 +00:00
|
|
|
/// Implement the service's worker loop. The service manager spawns a
|
|
|
|
/// task and calls this function after all services have been built.
|
|
|
|
async fn worker(self: Arc<Self>) -> Result<()> { Ok(()) }
|
2024-07-04 03:26:19 +00:00
|
|
|
|
2024-07-11 21:00:30 +00:00
|
|
|
/// Interrupt the service. This is sent to initiate a graceful shutdown.
|
|
|
|
/// The service worker should return from its work loop.
|
2024-07-04 03:26:19 +00:00
|
|
|
fn interrupt(&self) {}
|
|
|
|
|
|
|
|
/// Clear any caches or similar runtime state.
|
|
|
|
fn clear_cache(&self) {}
|
|
|
|
|
|
|
|
/// Memory usage report in a markdown string.
|
|
|
|
fn memory_usage(&self, _out: &mut dyn Write) -> Result<()> { Ok(()) }
|
|
|
|
|
|
|
|
/// Return the name of the service.
|
|
|
|
/// i.e. `crate::service::make_name(std::module_path!())`
|
|
|
|
fn name(&self) -> &str;
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) struct Args<'a> {
|
|
|
|
pub(crate) server: &'a Arc<Server>,
|
|
|
|
pub(crate) db: &'a Arc<Database>,
|
2024-07-16 22:00:54 +00:00
|
|
|
pub(crate) service: &'a Map,
|
2024-07-04 03:26:19 +00:00
|
|
|
}
|
|
|
|
|
2024-07-16 21:33:28 +00:00
|
|
|
pub(crate) type Map = BTreeMap<String, MapVal>;
|
|
|
|
pub(crate) type MapVal = (Arc<dyn Service>, Arc<dyn Any + Send + Sync>);
|
|
|
|
|
|
|
|
pub(crate) fn get<T: Any + Send + Sync>(map: &Map, name: &str) -> Option<Arc<T>> {
|
|
|
|
map.get(name).map(|(_, s)| {
|
|
|
|
s.clone()
|
|
|
|
.downcast::<T>()
|
|
|
|
.expect("Service must be correctly downcast.")
|
|
|
|
})
|
|
|
|
}
|
2024-07-04 03:26:19 +00:00
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub(crate) fn make_name(module_path: &str) -> &str { split_once_infallible(module_path, "::").1 }
|