Skip to content

Commit e9cbf1f

Browse files
authored
feat(health): 健康检查添加网络检查与数据库检查 (#23)
1 parent 07284b0 commit e9cbf1f

4 files changed

Lines changed: 76 additions & 7 deletions

File tree

src/health/db_checker.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
use crate::core::db::DatabaseManager;
2+
use log::error;
3+
4+
pub fn check_db() -> Result<bool, String> {
5+
let db = DatabaseManager::new()
6+
.map_err(|e| {
7+
let err_msg = format!("Failed to create database manager: {}", e);
8+
error!("{}", err_msg);
9+
err_msg
10+
})?;
11+
12+
db.health_check()
13+
.map_err(|e| {
14+
let err_msg = format!("Database health check failed: {}", e);
15+
error!("{}", err_msg);
16+
err_msg
17+
})?;
18+
19+
Ok(true)
20+
}

src/health/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
mod system_checker;
2-
mod network_checker;
3-
mod db_checker;
2+
pub(crate) mod network_checker;
3+
pub(crate) mod db_checker;
44
mod config_checker;

src/health/network_checker.rs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
use std::net::{TcpStream, ToSocketAddrs};
2+
use std::time::Duration;
3+
use log::{info, warn, error};
4+
5+
pub fn check_network() -> bool {
6+
let targets = [
7+
"1.1.1.1:443", // Cloudflare
8+
"8.8.8.8:53", // Google DNS
9+
"223.5.5.5:53", // AliDNS
10+
"180.76.76.76:53", // Baidu DNS
11+
];
12+
13+
info!("Starting network connectivity check");
14+
15+
for target in targets {
16+
if can_connect(target, Duration::from_secs(3)) {
17+
info!("Network check passed, successfully connected to {}", target);
18+
return true;
19+
} else {
20+
warn!("Failed to connect to {}", target);
21+
}
22+
}
23+
24+
error!("All network targets unreachable, network check failed");
25+
false
26+
}
27+
28+
fn can_connect(addr: &str, timeout: Duration) -> bool {
29+
let addrs = match addr.to_socket_addrs() {
30+
Ok(addrs) => addrs,
31+
Err(e) => {
32+
warn!("Failed to resolve address {}: {}", addr, e);
33+
return false;
34+
}
35+
};
36+
37+
for addr in addrs {
38+
if TcpStream::connect_timeout(&addr, timeout).is_ok() {
39+
return true;
40+
}
41+
}
42+
43+
false
44+
}

src/lib.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
#[macro_use]
22
extern crate rust_i18n;
33

4-
use crate::core::db::DatabaseManager;
54
use crate::pages::layout::App;
6-
use log::info;
5+
use log::{error, info};
76
use ratatui_kit::crossterm::event::{Event, KeyCode, KeyEventKind};
87
use ratatui_kit::prelude::RouterProvider;
98
use ratatui_kit::{AnyElement, ElementExt, Hooks, UseEvents, UseRouter, component, element};
@@ -31,8 +30,11 @@ pub async fn run() -> anyhow::Result<()> {
3130
info!("{}", t!("logger_is_initialized"));
3231
info!("{}", t!("test_message", name = "OmegaCode"));
3332
info!("{}", t!("current_locale", locale_name = "en"));
34-
let db = DatabaseManager::new()?;
35-
db.health_check()?;
33+
34+
if !health_check() {
35+
error!("Health check failed");
36+
return Err(anyhow::anyhow!("Health check failed"));
37+
}
3638

3739
element!(Root)
3840
.into_any()
@@ -45,7 +47,6 @@ pub async fn run() -> anyhow::Result<()> {
4547

4648
#[component]
4749
pub fn Root(_hooks: Hooks) -> impl Into<AnyElement<'static>> {
48-
// 严格按照官方文档:RouterProvider 放最顶层
4950
element!(
5051
RouterProvider(
5152
routes: app_routes(),
@@ -54,4 +55,8 @@ pub fn Root(_hooks: Hooks) -> impl Into<AnyElement<'static>> {
5455
)
5556
}
5657

58+
fn health_check() -> bool {
59+
health::network_checker::check_network() && health::db_checker::check_db().unwrap()
60+
}
61+
5762

0 commit comments

Comments
 (0)