pub mod client; pub mod consumer; pub mod error; pub mod producer; pub mod types; pub mod worker; pub use client::VLMClient; pub use consumer::VLMConsumer; pub use error::VLMError; pub use producer::VLMProducer; pub use types::{ VLMMessage, VLMQueueStatus, VLMRequest, VLMRequestType, VLMResult, VLMStats, VLMStatus, }; pub use worker::{start_workers, VLMWorker}; use sqlx::PgPool; use std::sync::Arc; use once_cell::sync::OnceCell; /// Global VLM Producer instance static VLM_PRODUCER: OnceCell> = OnceCell::new(); /// Initialize global VLM Producer pub fn init_producer(db: PgPool) { let producer = Arc::new(VLMProducer::new(db)); let _ = VLM_PRODUCER.set(producer); } /// Get global VLM Producer pub fn get_producer() -> Option> { VLM_PRODUCER.get().cloned() } /// 初始化 VLM 队列系统 pub async fn init(db: PgPool) -> Result<(), error::VLMError> { // 1. 创建表(如果不存在) create_tables_if_not_exists(&db).await?; // 2. 启动 Workers let num_workers = std::env::var("MOMENTRY_VLM_MAX_CONCURRENT") .ok() .and_then(|s| s.parse().ok()) .unwrap_or(3); worker::start_workers(db, num_workers, 2).await; Ok(()) } /// 创建表(如果不存在) async fn create_tables_if_not_exists(db: &PgPool) -> Result<(), error::VLMError> { // 检查表是否存在 let table_exists: bool = sqlx::query_scalar( r#" SELECT EXISTS ( SELECT 1 FROM information_schema.tables WHERE table_name = 'vlm_requests' ) "#, ) .fetch_one(db) .await?; if !table_exists { // 读取迁移文件并执行 tracing::info!("Creating VLM queue tables..."); // 这里简化处理,实际应该使用 sqlx-cli 运行迁移 // 暂时使用硬编码 SQL(从迁移文件提取) let sql = include_str!("../../../migrations/20261011_vlm_queue.sql"); // 分割 SQL 语句并逐个执行 for statement in sql.split(';') { let statement = statement.trim(); if !statement.is_empty() { sqlx::query(statement).execute(db).await?; } } tracing::info!("VLM queue tables created successfully"); } Ok(()) }