Skip to content

Commit

Permalink
feat: Allow configuring the max duration of health checks (#264)
Browse files Browse the repository at this point in the history
Closes #263
  • Loading branch information
spencewenski authored Jul 2, 2024
1 parent 7b28414 commit bfb90e7
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 4 deletions.
8 changes: 7 additions & 1 deletion src/api/cli/roadster/health.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,13 @@ where
_cli: &RoadsterCli,
#[allow(unused_variables)] state: &S,
) -> RoadsterResult<bool> {
let duration = Duration::from_millis(self.max_duration.unwrap_or(10000));
let duration = self
.max_duration
.map(Duration::from_millis)
.unwrap_or_else(|| {
let context = AppContext::from_ref(state);
context.config().health_check.max_duration.startup
});
let health = health_check(state, Some(duration)).await?;
let health = serde_json::to_string_pretty(&health)?;
info!("\n{health}");
Expand Down
8 changes: 7 additions & 1 deletion src/api/http/health.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,13 @@ where
S: Clone + Send + Sync + 'static,
AppContext: FromRef<S>,
{
let duration = Duration::from_millis(query.max_duration.unwrap_or(1000));
let duration = query
.max_duration
.map(Duration::from_millis)
.unwrap_or_else(|| {
let context = AppContext::from_ref(&state);
context.config().health_check.max_duration.startup
});
Ok(Json(health_check(&state, Some(duration)).await?))
}

Expand Down
5 changes: 5 additions & 0 deletions src/config/health_check/default.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
[health-check.max-duration]
startup = 60000
api = 1000
cli = 10000

[health-check.database]

[health-check.sidekiq]
28 changes: 26 additions & 2 deletions src/config/health_check/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ use crate::util::serde_util::default_true;
use axum::extract::FromRef;
use config::{FileFormat, FileSourceString};
use serde_derive::{Deserialize, Serialize};
use serde_with::serde_as;
use std::collections::BTreeMap;
use std::time::Duration;
use validator::Validate;

pub fn default_config() -> config::File<FileSourceString, FileFormat> {
Expand All @@ -17,6 +19,7 @@ pub fn default_config() -> config::File<FileSourceString, FileFormat> {
pub struct HealthCheck {
#[serde(default = "default_true")]
pub default_enable: bool,
pub max_duration: MaxDuration,
#[cfg(feature = "db-sql")]
pub database: HealthCheckConfig<()>,
#[cfg(feature = "sidekiq")]
Expand Down Expand Up @@ -50,7 +53,7 @@ pub struct HealthCheck {
pub custom: BTreeMap<String, HealthCheckConfig<CustomConfig>>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[derive(Debug, Clone, Validate, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
#[non_exhaustive]
pub struct CommonConfig {
Expand All @@ -77,7 +80,7 @@ impl CommonConfig {
}
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[derive(Debug, Clone, Validate, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
#[non_exhaustive]
pub struct HealthCheckConfig<T> {
Expand All @@ -87,6 +90,27 @@ pub struct HealthCheckConfig<T> {
pub custom: T,
}

/// The maximum duration to wait for health checks to succeed before timing out and assuming
/// the checks failed.
#[serde_as]
#[derive(Debug, Clone, Validate, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
#[non_exhaustive]
pub struct MaxDuration {
/// The maximum time (in milliseconds) to wait when running checks on app startup.
#[serde_as(as = "serde_with::DurationMilliSeconds")]
pub startup: Duration,
/// The maximum time (in milliseconds) to wait when running checks via a web API (HTTP, gRPC, etc).
/// In the default `_health` HTTP endpoint, this can be overridden via the `maxDuration`
/// query parameter.
#[serde_as(as = "serde_with::DurationMilliSeconds")]
pub api: Duration,
/// The maximum time (in milliseconds) to wait when running checks via the CLI. This can be
/// overridden via the `-d/--max-duration` CLI arg.
#[serde_as(as = "serde_with::DurationMilliSeconds")]
pub cli: Duration,
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ shutdown-on-error = true
[health-check]
default-enable = true

[health-check.max-duration]
startup = 60000
api = 1000
cli = 10000

[health-check.database]

[health-check.sidekiq]
Expand Down

0 comments on commit bfb90e7

Please sign in to comment.