63fbbe1225
Major Changes: - Pipeline Queue integration completed (internal.file + pipeline.commands) - Fixed old job_worker database compatibility with new command_queue table - Compiled Swift OCR binary (was missing, causing empty OCR results) - Added deep scan functionality through re-scan API Fixes: - Changed CommandJob.id from i32 to i64 for database compatibility - Added UUID generation in create_command (message_id field) - Fixed claim_pending_commands to query queue_name = 'internal.file' - Fixed field mappings: command_id → message_id, job_type → command_type - Added support for 'processing' status in CommandJobStatus Queue System: - internal.file queue: for register/unregister/re-scan commands (job_worker) - pipeline.commands queue: for vectorize/store_asrx/etc commands (pipeline_worker) Documentation: - Added comprehensive queue system documentation - Created M4 workspace status reports - Updated AGENTS.md with queue system details
268 lines
9.6 KiB
PL/PgSQL
268 lines
9.6 KiB
PL/PgSQL
-- ============================================
|
|
-- MarkBase Command Queue Schema
|
|
-- ============================================
|
|
-- Version: 1.0
|
|
-- Date: 2026-10-11
|
|
-- Description: 统一命令队列表结构
|
|
-- ============================================
|
|
|
|
-- ============================================
|
|
-- 1. 命令队列表
|
|
-- ============================================
|
|
CREATE TABLE IF NOT EXISTS command_queue (
|
|
-- 消息 ID
|
|
id BIGSERIAL PRIMARY KEY,
|
|
message_id VARCHAR(36) NOT NULL UNIQUE,
|
|
|
|
-- 队列信息
|
|
queue_name VARCHAR(255) NOT NULL,
|
|
queue_type VARCHAR(50) NOT NULL, -- 'internal' | 'external' | 'agent' | 'system'
|
|
|
|
-- 命令信息
|
|
command_type VARCHAR(100) NOT NULL,
|
|
command_category VARCHAR(100),
|
|
command_action VARCHAR(100),
|
|
|
|
-- 消息内容
|
|
key VARCHAR(255),
|
|
value JSONB NOT NULL,
|
|
|
|
-- 优先级
|
|
priority SMALLINT DEFAULT 2, -- 0=Critical, 1=High, 2=Normal, 3=Low
|
|
|
|
-- Billing 信息
|
|
request_id VARCHAR(36),
|
|
key_id VARCHAR(36),
|
|
billing_context JSONB,
|
|
token_usage JSONB,
|
|
cost_usd DECIMAL(10, 6),
|
|
|
|
-- 状态管理
|
|
status VARCHAR(20) DEFAULT 'pending', -- 'pending' | 'processing' | 'completed' | 'failed'
|
|
consumer_group VARCHAR(255),
|
|
consumer_id VARCHAR(255),
|
|
|
|
-- 重试机制
|
|
retry_count INTEGER DEFAULT 0,
|
|
max_retries INTEGER DEFAULT 3,
|
|
last_error TEXT,
|
|
|
|
-- 时间戳
|
|
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
|
|
scheduled_at TIMESTAMP WITH TIME ZONE,
|
|
processing_started_at TIMESTAMP WITH TIME ZONE,
|
|
processing_completed_at TIMESTAMP WITH TIME ZONE,
|
|
|
|
-- 约束
|
|
CONSTRAINT valid_queue_type CHECK (queue_type IN ('internal', 'external', 'agent', 'system')),
|
|
CONSTRAINT valid_status CHECK (status IN ('pending', 'processing', 'completed', 'failed')),
|
|
CONSTRAINT valid_priority CHECK (priority >= 0 AND priority <= 3)
|
|
);
|
|
|
|
-- 创建索引
|
|
CREATE INDEX idx_command_queue_name ON command_queue(queue_name, priority, created_at);
|
|
CREATE INDEX idx_command_status ON command_queue(status, created_at);
|
|
CREATE INDEX idx_command_consumer ON command_queue(consumer_group, consumer_id);
|
|
CREATE INDEX idx_command_type ON command_queue(command_type, created_at);
|
|
CREATE INDEX idx_command_billing ON command_queue(request_id, key_id);
|
|
CREATE INDEX idx_command_scheduled ON command_queue(scheduled_at, status);
|
|
|
|
-- ============================================
|
|
-- 2. 命令执行日志表
|
|
-- ============================================
|
|
CREATE TABLE IF NOT EXISTS command_execution_log (
|
|
id BIGSERIAL PRIMARY KEY,
|
|
message_id VARCHAR(36) NOT NULL,
|
|
|
|
-- 执行信息
|
|
handler VARCHAR(255),
|
|
action VARCHAR(255),
|
|
|
|
-- 执行结果
|
|
status VARCHAR(20), -- 'success' | 'error' | 'timeout'
|
|
result JSONB,
|
|
error_message TEXT,
|
|
|
|
-- 性能指标
|
|
duration_ms INTEGER,
|
|
tokens_used INTEGER,
|
|
cost_usd DECIMAL(10, 6),
|
|
|
|
-- 时间戳
|
|
executed_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
|
|
|
|
-- 约束
|
|
FOREIGN KEY (message_id) REFERENCES command_queue(message_id) ON DELETE CASCADE,
|
|
CONSTRAINT valid_exec_status CHECK (status IN ('success', 'error', 'timeout'))
|
|
);
|
|
|
|
CREATE INDEX idx_exec_log_message ON command_execution_log(message_id, executed_at);
|
|
CREATE INDEX idx_exec_log_status ON command_execution_log(status, executed_at);
|
|
CREATE INDEX idx_exec_log_handler ON command_execution_log(handler, executed_at);
|
|
|
|
-- ============================================
|
|
-- 3. Agent 订阅表
|
|
-- ============================================
|
|
CREATE TABLE IF NOT EXISTS agent_subscriptions (
|
|
id SERIAL PRIMARY KEY,
|
|
agent_id VARCHAR(255) NOT NULL,
|
|
queue_name VARCHAR(255) NOT NULL,
|
|
consumer_group VARCHAR(255) NOT NULL,
|
|
|
|
-- 订阅状态
|
|
active BOOLEAN DEFAULT true,
|
|
last_message_at TIMESTAMP WITH TIME ZONE,
|
|
message_count BIGINT DEFAULT 0,
|
|
|
|
-- 配置
|
|
auto_commit BOOLEAN DEFAULT true,
|
|
max_batch_size INTEGER DEFAULT 10,
|
|
timeout_ms INTEGER DEFAULT 5000,
|
|
|
|
-- 时间戳
|
|
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
|
|
updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
|
|
|
|
-- 约束
|
|
UNIQUE(agent_id, queue_name)
|
|
);
|
|
|
|
CREATE INDEX idx_agent_subs_agent ON agent_subscriptions(agent_id);
|
|
CREATE INDEX idx_agent_subs_queue ON agent_subscriptions(queue_name);
|
|
|
|
-- ============================================
|
|
-- 4. 队列路由配置表
|
|
-- ============================================
|
|
CREATE TABLE IF NOT EXISTS queue_routes (
|
|
id SERIAL PRIMARY KEY,
|
|
command_type VARCHAR(100) NOT NULL,
|
|
command_category VARCHAR(100),
|
|
|
|
-- 路由信息
|
|
queue_name VARCHAR(255) NOT NULL,
|
|
handler VARCHAR(255) NOT NULL,
|
|
priority SMALLINT DEFAULT 2,
|
|
billing_required BOOLEAN DEFAULT true,
|
|
|
|
-- 配置
|
|
max_retries INTEGER DEFAULT 3,
|
|
timeout_ms INTEGER DEFAULT 30000,
|
|
|
|
-- 状态
|
|
active BOOLEAN DEFAULT true,
|
|
|
|
-- 时间戳
|
|
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
|
|
updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
|
|
|
|
-- 约束
|
|
UNIQUE(command_type, command_category)
|
|
);
|
|
|
|
CREATE INDEX idx_queue_routes_type ON queue_routes(command_type);
|
|
CREATE INDEX idx_queue_routes_queue ON queue_routes(queue_name);
|
|
|
|
-- ============================================
|
|
-- 5. 插入默认路由配置
|
|
-- ============================================
|
|
INSERT INTO queue_routes (command_type, command_category, queue_name, handler, priority, billing_required) VALUES
|
|
-- 内部命令
|
|
('internal', 'video_registration', 'internal.video', 'video_registration_handler', 2, true),
|
|
('internal', 'file_processing', 'internal.file', 'file_processing_handler', 2, true),
|
|
('internal', 'file_deletion', 'internal.file', 'file_deletion_handler', 3, true),
|
|
('internal', 'embedding_generation', 'internal.data', 'embedding_handler', 2, true),
|
|
('internal', 'index_rebuild', 'internal.data', 'index_rebuild_handler', 3, true),
|
|
('internal', 'cache_clear', 'internal.system', 'cache_handler', 3, false),
|
|
('internal', 'worker_restart', 'internal.system', 'worker_handler', 1, false),
|
|
('internal', 'service_health_check', 'internal.system', 'health_check_handler', 2, false),
|
|
('internal', 'configuration_update', 'internal.system', 'config_handler', 1, false),
|
|
('internal', 'database_migration', 'internal.system', 'migration_handler', 0, false),
|
|
('internal', 'vector_sync', 'internal.data', 'vector_sync_handler', 2, true),
|
|
('internal', 'identity_merging', 'internal.data', 'identity_merge_handler', 3, true),
|
|
|
|
-- 外部命令
|
|
('external', 'api_request', 'external.api', 'api_request_handler', 1, true),
|
|
('external', 'user_command', 'external.user', 'user_command_handler', 1, true),
|
|
('external', 'webhook_trigger', 'external.webhook', 'webhook_handler', 2, true),
|
|
('external', 'integration_sync', 'external.integration', 'integration_handler', 3, true),
|
|
('external', 'external_api_call', 'external.api', 'external_api_handler', 2, true),
|
|
|
|
-- Agent 命令
|
|
('agent', 'agent_task', 'agent.tasks', 'agent_task_handler', 0, false),
|
|
('agent', 'agent_query', 'agent.queries', 'agent_query_handler', 0, false),
|
|
('agent', 'agent_response', 'agent.responses', 'agent_response_handler', 0, false),
|
|
('agent', 'agent_message', 'agent.messages', 'agent_message_handler', 0, false),
|
|
('agent', 'agent_broadcast', 'agent.broadcasts', 'agent_broadcast_handler', 0, false),
|
|
('agent', 'agent_subscribe', 'agent.subscriptions', 'agent_subscribe_handler', 0, false)
|
|
|
|
ON CONFLICT (command_type, command_category) DO NOTHING;
|
|
|
|
-- ============================================
|
|
-- 6. 队列统计视图
|
|
-- ============================================
|
|
CREATE OR REPLACE VIEW queue_statistics AS
|
|
SELECT
|
|
queue_name,
|
|
queue_type,
|
|
COUNT(*) as total_messages,
|
|
COUNT(*) FILTER (WHERE status = 'pending') as pending_count,
|
|
COUNT(*) FILTER (WHERE status = 'processing') as processing_count,
|
|
COUNT(*) FILTER (WHERE status = 'completed') as completed_count,
|
|
COUNT(*) FILTER (WHERE status = 'failed') as failed_count,
|
|
AVG(EXTRACT(EPOCH FROM (processing_completed_at - processing_started_at)) * 1000) FILTER (WHERE status = 'completed') as avg_duration_ms,
|
|
MIN(created_at) as oldest_message,
|
|
MAX(created_at) as newest_message
|
|
FROM command_queue
|
|
GROUP BY queue_name, queue_type
|
|
ORDER BY queue_name;
|
|
|
|
-- ============================================
|
|
-- 7. 性能优化触发器
|
|
-- ============================================
|
|
CREATE OR REPLACE FUNCTION update_command_timestamp()
|
|
RETURNS TRIGGER AS $$
|
|
BEGIN
|
|
NEW.updated_at = NOW();
|
|
RETURN NEW;
|
|
END;
|
|
$$ LANGUAGE plpgsql;
|
|
|
|
CREATE TRIGGER update_agent_subscriptions_timestamp
|
|
BEFORE UPDATE ON agent_subscriptions
|
|
FOR EACH ROW
|
|
EXECUTE FUNCTION update_command_timestamp();
|
|
|
|
CREATE TRIGGER update_queue_routes_timestamp
|
|
BEFORE UPDATE ON queue_routes
|
|
FOR EACH ROW
|
|
EXECUTE FUNCTION update_command_timestamp();
|
|
|
|
-- ============================================
|
|
-- 8. 清理过期消息存储过程
|
|
-- ============================================
|
|
CREATE OR REPLACE PROCEDURE cleanup_old_commands(
|
|
days_to_keep INTEGER DEFAULT 30
|
|
)
|
|
LANGUAGE plpgsql
|
|
AS $$
|
|
BEGIN
|
|
-- 删除执行日志
|
|
DELETE FROM command_execution_log
|
|
WHERE executed_at < NOW() - (days_to_keep || ' days')::INTERVAL
|
|
AND message_id IN (
|
|
SELECT message_id FROM command_queue
|
|
WHERE status IN ('completed', 'failed')
|
|
);
|
|
|
|
-- 删除已完成的消息
|
|
DELETE FROM command_queue
|
|
WHERE status IN ('completed', 'failed')
|
|
AND processing_completed_at < NOW() - (days_to_keep || ' days')::INTERVAL;
|
|
|
|
RAISE NOTICE 'Cleaned up commands older than % days', days_to_keep;
|
|
END;
|
|
$$;
|
|
|
|
-- ============================================
|
|
-- Schema 创建完成
|
|
-- ============================================ |