Skip to main content

saluki_core/health/
worker.rs

1use async_trait::async_trait;
2use saluki_api::{DynamicRoute, EndpointType};
3use saluki_error::generic_error;
4
5use super::HealthRegistry;
6use crate::runtime::{state::DataspaceRegistry, InitializationError, ProcessShutdown, Supervisable, SupervisorFuture};
7
8/// A worker that runs the health registry.
9///
10/// This is the only way to run the health registry's liveness probing event loop. The worker
11/// implements [`Supervisable`], so it should be added to a [`Supervisor`][crate::runtime::Supervisor]
12/// to be managed as part of a supervision tree.
13pub struct HealthRegistryWorker {
14    health_registry: HealthRegistry,
15}
16
17impl HealthRegistryWorker {
18    pub(super) fn new(health_registry: HealthRegistry) -> Self {
19        Self { health_registry }
20    }
21}
22
23#[async_trait]
24impl Supervisable for HealthRegistryWorker {
25    fn name(&self) -> &str {
26        "health-registry"
27    }
28
29    async fn initialize(&self, process_shutdown: ProcessShutdown) -> Result<SupervisorFuture, InitializationError> {
30        let runner = self.health_registry.clone().into_runner()?;
31
32        let health_routes = DynamicRoute::http(EndpointType::Unprivileged, self.health_registry.api_handler());
33
34        Ok(Box::pin(async move {
35            // Register our API routes before we actually start running.
36            DataspaceRegistry::try_current()
37                .ok_or_else(|| generic_error!("Dataspace not available."))?
38                .assert(health_routes, "health-registry-api");
39
40            runner.run(process_shutdown).await;
41
42            Ok(())
43        }))
44    }
45}