use crate::{QueueConfig, RuntimeBuilder, worker_registry::WorkerConfig};
pub struct ComponentRegistry<DT> {
pub module_path: &'static str,
pub type_name: &'static str,
pub definition: fn() -> ComponentDefinition<DT>,
}
pub enum ComponentDefinition<DT> {
Queue(QueueConfig),
Worker(WorkerConfig<DT>),
}
pub trait RegisterComponents {
type Context: Clone + Send + Sync + 'static;
fn register_components(runtime: RuntimeBuilder<Self::Context>)
-> RuntimeBuilder<Self::Context>;
}
pub use inventory::collect as create_component_registry;
pub use inventory::submit as register_component;
pub use inventory::iter as iterate_components;
impl<DT> ComponentRegistry<DT>
where
DT: Clone + Send + Sync + 'static,
{
pub fn register_components(
mut runtime: RuntimeBuilder<DT>,
items: impl Iterator<Item = &'static Self>,
) -> RuntimeBuilder<DT> {
for component in items {
tracing::info!(
"Registering {}::{}",
component.module_path,
component.type_name
);
match (component.definition)() {
ComponentDefinition::Queue(q) => runtime = runtime.queue_with(q),
ComponentDefinition::Worker(w) => runtime = runtime.worker_with(w),
}
}
runtime
}
}