667e279768
- 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
87 lines
2.7 KiB
Rust
87 lines
2.7 KiB
Rust
// Pipeline Worker - 独立进程,处理 command_queue 中的 Pipeline 命令
|
|
|
|
use anyhow::{Context, Result};
|
|
use momentry_core::core::command::pipeline::PipelineWorker;
|
|
use momentry_core::core::config;
|
|
use sqlx::PgPool;
|
|
use std::time::Duration;
|
|
use tracing::{error, info};
|
|
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
|
|
|
|
#[tokio::main]
|
|
async fn main() -> Result<()> {
|
|
// 初始化日志
|
|
tracing_subscriber::registry()
|
|
.with(
|
|
tracing_subscriber::fmt::layer()
|
|
.with_target(false)
|
|
.with_thread_ids(false)
|
|
)
|
|
.init();
|
|
|
|
info!("Pipeline Worker starting...");
|
|
|
|
// 加载环境变量
|
|
dotenv::dotenv().ok();
|
|
|
|
// 连接数据库
|
|
let database_url = config::DATABASE_URL.clone();
|
|
let db = PgPool::connect(&database_url)
|
|
.await
|
|
.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);
|
|
|
|
// 启动 worker
|
|
info!("Pipeline Worker started, polling for commands...");
|
|
|
|
// 主循环
|
|
loop {
|
|
match worker.start().await {
|
|
Ok(_) => {
|
|
// start() 会无限循环,除非出错
|
|
info!("Worker loop completed unexpectedly");
|
|
}
|
|
Err(e) => {
|
|
error!("Worker error: {}", e);
|
|
error!("Restarting in 5 seconds...");
|
|
tokio::time::sleep(Duration::from_secs(5)).await;
|
|
}
|
|
}
|
|
}
|
|
} |