feat: implement Phase 5 Resource Registry & Heartbeat
This commit is contained in:
@@ -41,9 +41,9 @@ pub mod sync_db;
|
||||
|
||||
pub use mongodb_db::MongoDb;
|
||||
pub use postgres_db::{
|
||||
Bm25Result, CreateApiKeyConfig, HybridSearchResult, MonitorJob, MonitorJobStats,
|
||||
MonitorJobStatus, PostgresDb, ProcessorJobStatus, ProcessorResult, ProcessorType, VideoRecord,
|
||||
VideoStatus,
|
||||
Bm25Result, CandidateRecord, CreateApiKeyConfig, FileRecord, HybridSearchResult, MonitorJob,
|
||||
MonitorJobStats, MonitorJobStatus, PostgresDb, ProcessorJobStatus, ProcessorResult,
|
||||
ProcessorType, ResourceRecord, VideoRecord, VideoStatus,
|
||||
};
|
||||
pub use qdrant_db::{QdrantDb, VectorPayload};
|
||||
pub use redis_client::{
|
||||
|
||||
@@ -15,6 +15,19 @@ use crate::core::text::{
|
||||
tokenizer::{contains_chinese, tokenize_chinese_text},
|
||||
};
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, sqlx::FromRow)]
|
||||
pub struct ResourceRecord {
|
||||
pub resource_id: String,
|
||||
pub resource_type: String,
|
||||
pub category: String,
|
||||
pub capabilities: Option<serde_json::Value>,
|
||||
pub config: Option<serde_json::Value>,
|
||||
pub metadata: Option<serde_json::Value>,
|
||||
pub status: String,
|
||||
pub last_heartbeat: Option<chrono::DateTime<chrono::Utc>>,
|
||||
pub created_at: Option<chrono::DateTime<chrono::Utc>>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, sqlx::FromRow)]
|
||||
pub struct IdentityRecord {
|
||||
pub id: i32,
|
||||
@@ -1805,6 +1818,59 @@ impl PostgresDb {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn register_resource(&self, resource: ResourceRecord) -> Result<()> {
|
||||
sqlx::query(
|
||||
"INSERT INTO resources (resource_id, resource_type, category, capabilities, config, metadata, status, last_heartbeat)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7, NOW())
|
||||
ON CONFLICT (resource_id) DO UPDATE SET
|
||||
resource_type = EXCLUDED.resource_type,
|
||||
category = EXCLUDED.category,
|
||||
capabilities = EXCLUDED.capabilities,
|
||||
config = EXCLUDED.config,
|
||||
metadata = EXCLUDED.metadata,
|
||||
status = EXCLUDED.status,
|
||||
last_heartbeat = NOW()"
|
||||
)
|
||||
.bind(resource.resource_id)
|
||||
.bind(resource.resource_type)
|
||||
.bind(resource.category)
|
||||
.bind(resource.capabilities)
|
||||
.bind(resource.config)
|
||||
.bind(resource.metadata)
|
||||
.bind(resource.status)
|
||||
.execute(&self.pool)
|
||||
.await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn heartbeat_resource(&self, resource_id: &str, status: &str) -> Result<()> {
|
||||
sqlx::query(
|
||||
"UPDATE resources SET status = $1, last_heartbeat = NOW() WHERE resource_id = $2"
|
||||
)
|
||||
.bind(status)
|
||||
.bind(resource_id)
|
||||
.execute(&self.pool)
|
||||
.await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn deregister_resource(&self, resource_id: &str) -> Result<()> {
|
||||
sqlx::query(
|
||||
"DELETE FROM resources WHERE resource_id = $1"
|
||||
)
|
||||
.bind(resource_id)
|
||||
.execute(&self.pool)
|
||||
.await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn list_resources(&self) -> Result<Vec<ResourceRecord>> {
|
||||
let rows = sqlx::query_as("SELECT * FROM resources ORDER BY last_heartbeat DESC")
|
||||
.fetch_all(&self.pool)
|
||||
.await?;
|
||||
Ok(rows)
|
||||
}
|
||||
|
||||
pub async fn list_people(&self, limit: i32, offset: i64) -> Result<Vec<IdentityRecord>> {
|
||||
let query = r#"
|
||||
SELECT id, uuid, name, metadata, created_at
|
||||
|
||||
Reference in New Issue
Block a user