crates_docs/cli/health_cmd.rs
1//! Health check command implementation
2
3use crate::tools::health::HealthCheckToolImpl;
4use std::path::Path;
5
6/// Run the `health` CLI command.
7///
8/// Performs real health checks against the server's internal state and the
9/// external services it depends on (docs.rs, crates.io), then prints a report.
10///
11/// Returns an error (so the process exits with a non-zero status) when the
12/// overall status is not healthy. This makes the command usable as a container
13/// or orchestrator health probe (e.g. the Docker Compose `healthcheck`).
14///
15/// Recognized `check_type` values: `all`, `external`, `internal`, `docs_rs`,
16/// `crates_io`. Unknown values produce a degraded (non-healthy) report.
17pub async fn run_health_command(
18 config_path: &Path,
19 check_type: &str,
20 verbose: bool,
21) -> Result<(), Box<dyn std::error::Error>> {
22 // Honor the global `--config` flag: initialize the shared HTTP client from
23 // the configured performance settings (user-agent, timeouts, pool) so the
24 // external probes behave like the running server. Falls back to defaults
25 // when the file is absent, and ignores re-initialization races.
26 if config_path.exists() {
27 if let Ok(app_config) = crate::config::AppConfig::from_file(config_path) {
28 let _ = crate::utils::init_global_http_client(&app_config.performance);
29 }
30 }
31
32 let tool = HealthCheckToolImpl::new();
33 let (report, is_healthy) = tool.run_check_report(check_type, verbose).await;
34
35 println!("{report}");
36
37 if is_healthy {
38 Ok(())
39 } else {
40 Err(
41 format!("Health check did not report a healthy status (check_type: {check_type})")
42 .into(),
43 )
44 }
45}