Skip to content

Commit

Permalink
chore: Minor improvement to initializing health checks in state (#593)
Browse files Browse the repository at this point in the history
  • Loading branch information
spencewenski authored Jan 31, 2025
1 parent 5ec55a7 commit 77497e9
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 16 deletions.
2 changes: 2 additions & 0 deletions src/app/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,8 @@ impl AppContext {
self.inner.metadata()
}

/// Returns the [`HealthCheck`]s that were registered in the [`HealthCheckRegistry`], or
/// an empty [`Vec`] if no [`HealthCheck`]s were registered.
pub fn health_checks(&self) -> Vec<Arc<dyn HealthCheck>> {
self.inner.health_checks()
}
Expand Down
24 changes: 8 additions & 16 deletions src/app/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,13 +259,10 @@ where
service_registry,
lifecycle_handler_registry,
} = prepare_without_cli(app, &state).await?;
before_app(
&state,
Some(health_check_registry),
&service_registry,
&lifecycle_handler_registry,
)
.await?;

let context = AppContext::from_ref(&state);
context.set_health_checks(health_check_registry)?;
before_app(&state, &service_registry, &lifecycle_handler_registry).await?;

Ok(state)
}
Expand Down Expand Up @@ -409,7 +406,6 @@ where
/// Run the app's initialization logic (lifecycle handlers, health checks, etc).
async fn before_app<A, S>(
state: &S,
health_check_registry: Option<HealthCheckRegistry>,
service_registry: &ServiceRegistry<A, S>,
lifecycle_handler_registry: &LifecycleHandlerRegistry<A, S>,
) -> RoadsterResult<()>
Expand All @@ -429,13 +425,9 @@ where
info!(name=%handler.name(), "Running AppLifecycleHandler::before_health_checks");
handler.before_health_checks(state).await?;
}
let checks = if let Some(health_check_registry) = health_check_registry {
health_check_registry.checks()
} else {
let context = AppContext::from_ref(state);
context.health_checks()
};
crate::service::runner::health_checks(checks).await?;

let context = AppContext::from_ref(state);
crate::service::runner::health_checks(context.health_checks()).await?;

info!("Running AppLifecycleHandler::before_services");
for handler in lifecycle_handlers.iter() {
Expand Down Expand Up @@ -467,7 +459,7 @@ where
let context = AppContext::from_ref(&state);
context.set_health_checks(health_check_registry)?;

before_app(&state, None, &service_registry, &lifecycle_handler_registry).await?;
before_app(&state, &service_registry, &lifecycle_handler_registry).await?;

let result = crate::service::runner::run(app, service_registry, &state).await;
if let Err(err) = result {
Expand Down

0 comments on commit 77497e9

Please sign in to comment.