release: v1.3.5 - merge Phase 1 (health/queue API + timeout_at self-heal) onto v1.3.4 base
- Add /api/v1/health/queue queue-health endpoint (public, like /health/detailed) - Add timeout_at column + index (migration 20261012_add_command_timeout.sql) - consume() stamps timeout_at; pipeline_worker self-heals timed-out commands every 5m - Bump version 1.3.4 -> 1.3.5
This commit is contained in:
@@ -700,9 +700,87 @@ async fn check_http(url: &str) -> ServiceStatus {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize)]
|
||||
pub struct QueueHealth {
|
||||
pub status: String,
|
||||
pub stuck_commands: i64,
|
||||
pub pending_commands: i64,
|
||||
pub processing_commands: i64,
|
||||
pub completed_last_hour: i64,
|
||||
pub failed_last_hour: i64,
|
||||
pub avg_processing_time_ms: Option<f64>,
|
||||
}
|
||||
|
||||
pub async fn check_queue_health(pool: &sqlx::PgPool) -> Result<QueueHealth, sqlx::Error> {
|
||||
let stuck_commands: i64 = sqlx::query_scalar(
|
||||
"SELECT COUNT(*) FROM command_queue
|
||||
WHERE status = 'processing'
|
||||
AND processing_started_at < NOW() - INTERVAL '1 hour'",
|
||||
)
|
||||
.fetch_one(pool)
|
||||
.await?;
|
||||
|
||||
let pending_commands: i64 =
|
||||
sqlx::query_scalar("SELECT COUNT(*) FROM command_queue WHERE status = 'pending'")
|
||||
.fetch_one(pool)
|
||||
.await?;
|
||||
|
||||
let processing_commands: i64 =
|
||||
sqlx::query_scalar("SELECT COUNT(*) FROM command_queue WHERE status = 'processing'")
|
||||
.fetch_one(pool)
|
||||
.await?;
|
||||
|
||||
let completed_last_hour: i64 = sqlx::query_scalar(
|
||||
"SELECT COUNT(*) FROM command_queue
|
||||
WHERE status = 'completed'
|
||||
AND processing_completed_at > NOW() - INTERVAL '1 hour'",
|
||||
)
|
||||
.fetch_one(pool)
|
||||
.await?;
|
||||
|
||||
let failed_last_hour: i64 = sqlx::query_scalar(
|
||||
"SELECT COUNT(*) FROM command_queue
|
||||
WHERE status = 'failed'
|
||||
AND processing_completed_at > NOW() - INTERVAL '1 hour'",
|
||||
)
|
||||
.fetch_one(pool)
|
||||
.await?;
|
||||
|
||||
let avg_processing_time_ms: Option<f64> = sqlx::query_scalar(
|
||||
"SELECT AVG(EXTRACT(EPOCH FROM (processing_completed_at - processing_started_at)) * 1000)::double precision
|
||||
FROM command_queue
|
||||
WHERE status = 'completed'
|
||||
AND processing_completed_at > NOW() - INTERVAL '1 hour'",
|
||||
)
|
||||
.fetch_one(pool)
|
||||
.await?;
|
||||
|
||||
let status = if stuck_commands > 0 { "unhealthy" } else { "healthy" };
|
||||
|
||||
Ok(QueueHealth {
|
||||
status: status.to_string(),
|
||||
stuck_commands,
|
||||
pending_commands,
|
||||
processing_commands,
|
||||
completed_last_hour,
|
||||
failed_last_hour,
|
||||
avg_processing_time_ms,
|
||||
})
|
||||
}
|
||||
|
||||
pub async fn health_queue(
|
||||
State(state): State<AppState>,
|
||||
) -> Result<Json<QueueHealth>, (StatusCode, String)> {
|
||||
match check_queue_health(state.db.pool()).await {
|
||||
Ok(health) => Ok(Json(health)),
|
||||
Err(e) => Err((StatusCode::INTERNAL_SERVER_ERROR, e.to_string())),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn health_routes() -> Router<AppState> {
|
||||
Router::new()
|
||||
.route("/health", get(health))
|
||||
.route("/health/detailed", get(health_detailed))
|
||||
.route("/health/consistency", get(health_consistency))
|
||||
.route("/health/queue", get(health_queue))
|
||||
}
|
||||
|
||||
+5
-1
@@ -20,7 +20,7 @@ use super::docs;
|
||||
use super::face_filter;
|
||||
use super::files;
|
||||
use super::health;
|
||||
use super::health::{health, health_consistency, health_detailed};
|
||||
use super::health::{health, health_consistency, health_detailed, health_queue};
|
||||
use super::identities;
|
||||
use super::identity_api;
|
||||
use super::identity_binding;
|
||||
@@ -173,6 +173,10 @@ pub async fn start_server(host: &str, port: u16) -> anyhow::Result<()> {
|
||||
.route(
|
||||
"/api/v1/health/consistency",
|
||||
axum::routing::get(health_consistency),
|
||||
)
|
||||
.route(
|
||||
"/api/v1/health/queue",
|
||||
axum::routing::get(health_queue),
|
||||
);
|
||||
|
||||
let app = Router::new()
|
||||
|
||||
@@ -31,7 +31,39 @@ async fn main() -> Result<()> {
|
||||
.context("Failed to connect to database")?;
|
||||
|
||||
info!("Connected to database");
|
||||
|
||||
|
||||
// 启动超时监控任务:每 5 分钟重置超时(processing 超过 timeout_at)的命令
|
||||
let db_monitor = db.clone();
|
||||
tokio::spawn(async move {
|
||||
let mut interval = tokio::time::interval(Duration::from_secs(300));
|
||||
loop {
|
||||
interval.tick().await;
|
||||
match sqlx::query(
|
||||
"UPDATE command_queue
|
||||
SET status = 'pending',
|
||||
consumer_id = NULL,
|
||||
consumer_group = NULL,
|
||||
processing_started_at = NULL,
|
||||
timeout_at = NULL,
|
||||
retry_count = retry_count + 1
|
||||
WHERE status = 'processing'
|
||||
AND timeout_at IS NOT NULL
|
||||
AND timeout_at < NOW()
|
||||
AND retry_count < max_retries",
|
||||
)
|
||||
.execute(&db_monitor)
|
||||
.await
|
||||
{
|
||||
Ok(result) => {
|
||||
if result.rows_affected() > 0 {
|
||||
info!("Reset {} timed out commands", result.rows_affected());
|
||||
}
|
||||
}
|
||||
Err(e) => error!("Failed to reset timed out commands: {}", e),
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// 创建 Pipeline Worker
|
||||
let worker = PipelineWorker::new(db);
|
||||
|
||||
|
||||
@@ -61,6 +61,7 @@ impl PipelineWorker {
|
||||
max_messages: 5,
|
||||
timeout_ms: 5000,
|
||||
auto_commit: false,
|
||||
timeout_seconds: 3600,
|
||||
},
|
||||
).await?;
|
||||
|
||||
|
||||
@@ -103,7 +103,8 @@ impl CommandQueue {
|
||||
SET status = 'processing',
|
||||
consumer_group = $1,
|
||||
consumer_id = $2,
|
||||
processing_started_at = NOW()
|
||||
processing_started_at = NOW(),
|
||||
timeout_at = NOW() + ($5::bigint * INTERVAL '1 second')
|
||||
WHERE message_id IN (
|
||||
SELECT message_id
|
||||
FROM command_queue
|
||||
@@ -127,6 +128,7 @@ impl CommandQueue {
|
||||
options.consumer_id,
|
||||
queue_name,
|
||||
options.max_messages as i64,
|
||||
options.timeout_seconds,
|
||||
)
|
||||
.fetch_all(&self.db)
|
||||
.await?;
|
||||
|
||||
@@ -241,10 +241,13 @@ pub struct ConsumeOptions {
|
||||
pub timeout_ms: i32,
|
||||
#[serde(default)]
|
||||
pub auto_commit: bool,
|
||||
#[serde(default = "default_timeout_seconds")]
|
||||
pub timeout_seconds: i64,
|
||||
}
|
||||
|
||||
fn default_max_messages() -> i32 { 10 }
|
||||
fn default_timeout_ms() -> i32 { 5000 }
|
||||
fn default_timeout_seconds() -> i64 { 3600 }
|
||||
|
||||
/// Billing 上下文
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
|
||||
Reference in New Issue
Block a user