Separate database connections from file-backed host information and document handoff
Verify databases and deploy Aster VM / verify-and-deploy (push) Successful in 7m25s
Verify databases and deploy Aster VM / verify-and-deploy (push) Successful in 7m25s
This commit is contained in:
@@ -1,14 +1,13 @@
|
||||
# Server 入口
|
||||
|
||||
[main.rs](main.rs) 包含当前所有宿主启动代码:
|
||||
[main.rs](main.rs) 包含所有宿主启动代码:
|
||||
|
||||
| 部分 | 职责 |
|
||||
| --- | --- |
|
||||
| AppState | 共享只读 Arc<Vm> 与可克隆的 Storage 连接池 |
|
||||
| main/run | 创建 VM、连接数据库、写 host.version、绑定地址、组装路由、服务结束后关闭存储 |
|
||||
| health | ping 数据库,返回 200/503 及后端标签 |
|
||||
| AppState | 共享只读 Arc<Vm> |
|
||||
| main/run | 创建空 VM、绑定地址、设置退出信号、写文件 host.version、组装路由;不创建数据库连接 |
|
||||
| health | 返回 200 和 status=ok,仅检查 HTTP 宿主可响应 |
|
||||
| bootstrap | 返回对象模型、type_system=false 和堆对象数量,不分配对象 |
|
||||
| shutdown_signal | Ctrl-C;Unix 上同时接受 SIGTERM,触发 Axum 优雅退出 |
|
||||
| tests | 关闭数据库池后健康状态变为 unavailable |
|
||||
|
||||
改变端点时同步 [运行指南](../../../docs/operations/README.md) 与 [smoke.py](../../../scripts/README.md);改变启动变量时同步 [配置](../../../docs/operations/configuration.md)。网络、信号和数据库逻辑不进入 Core。返回 [server](../README.md)。
|
||||
/health 不表示 PostgreSQL、特殊类查询或执行脚本正常;数据库检查应属于实际消费连接的对象。宿主独立于数据库的行为由远端 smoke.py 验证,见 [验证](../../../docs/operations/validation.md)。改变端点同步 [运行指南](../../../docs/operations/README.md) 和 [scripts](../../../scripts/README.md)。返回 [server](../README.md)。
|
||||
|
||||
@@ -3,21 +3,19 @@ use std::future::Future;
|
||||
use std::process::ExitCode;
|
||||
use std::sync::Arc;
|
||||
|
||||
use axum::{Json, Router, extract::State, http::StatusCode, routing::get};
|
||||
use axum::{Json, Router, extract::State, routing::get};
|
||||
use endor_core::Vm;
|
||||
use host::MetadataStore;
|
||||
use serde::Serialize;
|
||||
use storage::{Storage, StorageConfig};
|
||||
|
||||
#[derive(Clone)]
|
||||
struct AppState {
|
||||
vm: Arc<Vm>,
|
||||
storage: Storage,
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
struct Health {
|
||||
status: &'static str,
|
||||
storage: &'static str,
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
@@ -40,44 +38,23 @@ async fn main() -> ExitCode {
|
||||
|
||||
async fn run() -> Result<(), Box<dyn Error>> {
|
||||
let vm = Arc::new(Vm::new()?);
|
||||
let storage = Storage::connect(&StorageConfig::from_env()?).await?;
|
||||
storage
|
||||
.put("host.version", env!("CARGO_PKG_VERSION"))
|
||||
.await?;
|
||||
let address = std::env::var("ASTER_LISTEN").unwrap_or_else(|_| "127.0.0.1:8080".into());
|
||||
let listener = tokio::net::TcpListener::bind(&address).await?;
|
||||
let shutdown = shutdown_signal()?;
|
||||
MetadataStore::from_env()?.put("host.version", env!("CARGO_PKG_VERSION"))?;
|
||||
let app = Router::new()
|
||||
.route("/health", get(health))
|
||||
.route("/bootstrap", get(bootstrap))
|
||||
.with_state(AppState {
|
||||
vm,
|
||||
storage: storage.clone(),
|
||||
});
|
||||
println!(
|
||||
"Endor host listening on {} (storage={})",
|
||||
listener.local_addr()?,
|
||||
storage.backend_name()
|
||||
);
|
||||
.with_state(AppState { vm });
|
||||
println!("Endor host listening on {}", listener.local_addr()?);
|
||||
axum::serve(listener, app)
|
||||
.with_graceful_shutdown(shutdown)
|
||||
.await?;
|
||||
storage.close().await;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn health(State(state): State<AppState>) -> (StatusCode, Json<Health>) {
|
||||
let (code, status) = match state.storage.ping().await {
|
||||
Ok(()) => (StatusCode::OK, "ok"),
|
||||
Err(_) => (StatusCode::SERVICE_UNAVAILABLE, "storage_unavailable"),
|
||||
};
|
||||
(
|
||||
code,
|
||||
Json(Health {
|
||||
status,
|
||||
storage: state.storage.backend_name(),
|
||||
}),
|
||||
)
|
||||
async fn health() -> Json<Health> {
|
||||
Json(Health { status: "ok" })
|
||||
}
|
||||
|
||||
async fn bootstrap(State(state): State<AppState>) -> Json<BootstrapInfo> {
|
||||
@@ -98,25 +75,3 @@ fn shutdown_signal() -> std::io::Result<impl Future<Output = ()>> {
|
||||
let _ = tokio::signal::ctrl_c().await;
|
||||
})
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[tokio::test]
|
||||
async fn health_reports_unavailable_after_storage_closes() {
|
||||
let storage = Storage::connect(&StorageConfig::SqliteMemory)
|
||||
.await
|
||||
.unwrap();
|
||||
let state = AppState {
|
||||
vm: Arc::new(Vm::new().unwrap()),
|
||||
storage,
|
||||
};
|
||||
let (status, _) = health(State(state.clone())).await;
|
||||
assert_eq!(status, StatusCode::OK);
|
||||
state.storage.close().await;
|
||||
let (status, Json(body)) = health(State(state)).await;
|
||||
assert_eq!(status, StatusCode::SERVICE_UNAVAILABLE);
|
||||
assert_eq!(body.status, "storage_unavailable");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user