Skip to content

Self-evolving Context Database for AI Agents. Unify Agent Memory, Knowledge RAG and Skills.

refactor: decouple QueueManager from VikingDBManager (#149)

kkkwjx36c58d2d9377
diff --git a/openviking/service/core.py b/openviking/service/core.py
index d025d6632..7c525ef8d 100644
--- a/openviking/service/core.py
+++ b/openviking/service/core.py
@@ -22,6 +22,7 @@ from openviking.session.compressor import SessionCompressor
from openviking.session.user_id import UserIdentifier
from openviking.storage import VikingDBManager
from openviking.storage.collection_schemas import init_context_collection
+from openviking.storage.queuefs.queue_manager import QueueManager, init_queue_manager
from openviking.storage.transaction import TransactionManager, init_transaction_manager
from openviking.storage.viking_fs import VikingFS, init_viking_fs
from openviking.utils import get_logger
@@ -65,6 +66,7 @@ class OpenVikingService:
# Infrastructure
self._agfs_manager: Optional[AGFSManager] = None
self._agfs_url: Optional[str] = None
+ self._queue_manager: Optional[QueueManager] = None
self._vikingdb_manager: Optional[VikingDBManager] = None
self._viking_fs: Optional[VikingFS] = None
self._embedder: Optional[Any] = None
@@ -104,10 +106,24 @@ class OpenVikingService:
else:
self._agfs_url = config.agfs.url
+ # Initialize QueueManager
+ if self._agfs_url:
+ self._queue_manager = init_queue_manager(
+ agfs_url=self._agfs_url,
+ timeout=config.agfs.timeout,
+ )
+ else:
+ logger.warning("AGFS URL not configured, skipping queue manager initialization")
+
+ # Initialize VikingDBManager with QueueManager
self._vikingdb_manager = VikingDBManager(
- vectordb_config=config.vectordb, agfs_config=config.agfs
+ vectordb_config=config.vectordb, queue_manager=self._queue_manager
)
+ # Configure queues if QueueManager is available
+ if self._queue_manager:
+ self._queue_manager.setup_standard_queues(self._vikingdb_manager)
+
# Initialize TransactionManager
self._transaction_manager = init_transaction_manager(agfs_config=config.agfs)
@@ -245,6 +261,11 @@ class OpenVikingService:
self._transaction_manager.stop()
self._transaction_manager = None
+ if self._queue_manager:
+ self._queue_manager.stop()
+ self._queue_manager = None
+ logger.info("Queue manager stopped")
+
if self._vikingdb_manager:
await self._vikingdb_manager.close()
self._vikingdb_manager = None
diff --git a/openviking/storage/queuefs/queue_manager.py b/openviking/storage/queuefs/queue_manager.py
index 35974b863..5e98dc66c 100644
--- a/openviking/storage/queuefs/queue_manager.py
+++ b/openviking/storage/queuefs/queue_manager.py
@@ -94,6 +94,42 @@ class QueueManager:
logger.info("[QueueManager] Started")
+ def setup_standard_queues(self, vikingdb_interface: Any) -> None:
+ """
+ Setup standard queues (Embedding and Semantic) with their handlers.
+
+ This method initializes the EmbeddingQueue with TextEmbeddingHandler
+ and the SemanticQueue with SemanticProcessor, then ensures the
+ queue manager is started.
+
+ Args:
+ vikingdb_interface: VikingDBInterface instance for handlers to write results.
+ """
+ # Import handlers here to avoid circular dependencies
+ from openviking.storage.collection_schemas import TextEmbeddingHandler
+ from openviking.storage.queuefs import SemanticProcessor
+
+ # Embedding Queue
+ embedding_handler = TextEmbeddingHandler(vikingdb_interface)
+ self.get_queue(
+ self.EMBEDDING,
+ dequeue_handler=embedding_handler,
+ allow_create=True,
+ )
+ logger.info("Embedding queue initialized with TextEmbeddingHandler")
+
+ # Semantic Queue
+ semantic_processor = SemanticProcessor()
+ self.get_queue(
+ self.SEMANTIC,
+ dequeue_handler=semantic_processor,
+ allow_create=True,
+ )
+ logger.info("Semantic queue initialized with SemanticProcessor")
+
+ # Start QueueManager processing
+ self.start()
+
def _start_queue_worker(self, queue: NamedQueue) -> None:
"""Start a dedicated worker thread for a queue if not already running."""
if queue.name in self._queue_threads:
diff --git a/openviking/storage/vikingdb_manager.py b/openviking/storage/vikingdb_manager.py
index 3c9e8fa18..c814eda66 100644
--- a/openviking/storage/vikingdb_manager.py
+++ b/openviking/storage/vikingdb_manager.py
@@ -6,13 +6,11 @@ VikingDB Manager class that extends VikingVectorIndexBackend with queue manageme
from typing import Optional
-from openviking.storage.collection_schemas import TextEmbeddingHandler
from openviking.storage.queuefs.embedding_msg import EmbeddingMsg
from openviking.storage.queuefs.embedding_queue import EmbeddingQueue
-from openviking.storage.queuefs.queue_manager import init_queue_manager
+from openviking.storage.queuefs.queue_manager import QueueManager
from openviking.storage.viking_vector_index_backend import VikingVectorIndexBackend
from openviking.utils import get_logger
-from openviking.utils.config.agfs_config import AGFSConfig
from openviking.utils.config.vectordb_config import VectorDBBackendConfig
logger = get_logger(__name__)
@@ -23,29 +21,26 @@ class VikingDBManager(VikingVectorIndexBackend):
VikingDB Manager that extends VikingVectorIndexBackend with queue management capabilities.
This class provides all the functionality of VikingVectorIndexBackend plus:
- - Queue manager initialization and management
+ - Queue manager integration (via injection)
- Embedding queue integration
- Background processing capabilities
Usage:
# In-memory mode with queue management
- manager = VikingDBManager()
-
- # Local persistent storage with queue management
- manager = VikingDBManager(path="./data/vikingdb", agfs_url="http://localhost:8080")
+ manager = VikingDBManager(vectordb_config=..., queue_manager=qm)
"""
def __init__(
self,
vectordb_config: VectorDBBackendConfig,
- agfs_config: AGFSConfig,
+ queue_manager: Optional[QueueManager] = None,
):
"""
Initialize VikingDB Manager.
Args:
vectordb_config: Configuration object for VectorDB backend.
- agfs_config: Configuration object for AGFS (Agent Global File System).
+ queue_manager: QueueManager instance.
"""
# Initialize the base VikingVectorIndexBackend without queue management
super().__init__(
@@ -53,75 +48,15 @@ class VikingDBManager(VikingVectorIndexBackend):
)
# Queue management specific attributes
- self.agfs_url = agfs_config.url
- self.agfs_timeout = agfs_config.timeout
- self._queue_manager = None
- self._embedding_handler = None
- self._semantic_processor = None
+ self._queue_manager = queue_manager
self._closing = False
- # Initialize queue manager if AGFS URL is provided
- self._init_queue_manager()
- if self._queue_manager:
- self._init_embedding_queue()
- self._init_semantic_queue()
- self._queue_manager.start()
-
- def _init_queue_manager(self):
- """Initialize queue manager for background processing."""
- if not self.agfs_url:
- logger.warning("AGFS URL not configured, skipping queue manager initialization")
- return
- self._queue_manager = init_queue_manager(
- agfs_url=self.agfs_url,
- timeout=self.agfs_timeout,
- )
-
- def _init_embedding_queue(self):
- """Initialize embedding queue with TextEmbeddingHandler."""
- if not self._queue_manager:
- logger.warning("Queue manager not initialized, skipping embedding queue setup")
- return
-
- # Create TextEmbeddingHandler instance with self (VikingDBInterface)
- self._embedding_handler = TextEmbeddingHandler(self)
-
- # Get embedding queue with the handler, allow creation if not exists
- self._queue_manager.get_queue(
- self._queue_manager.EMBEDDING,
- dequeue_handler=self._embedding_handler,
- allow_create=True,
- )
- logger.info("Embedding queue initialized with TextEmbeddingHandler")
-
- def _init_semantic_queue(self):
- """Initialize semantic queue with SemanticProcessor, semantic queue is used to get abstract and summary of context data."""
- if not self._queue_manager:
- logger.warning("Queue manager not initialized, skipping semantic queue setup")
- return
-
- from openviking.storage.queuefs import SemanticProcessor
-
- # Create SemanticProcessor instance
- self._semantic_processor = SemanticProcessor()
-
- # Get semantic queue with the handler, allow creation if not exists
- self._queue_manager.get_queue(
- self._queue_manager.SEMANTIC,
- dequeue_handler=self._semantic_processor,
- allow_create=True,
- )
- logger.info("Semantic queue initialized with SemanticProcessor")
-
async def close(self) -> None:
- """Close storage connection and release resources, including queue manager."""
+ """Close storage connection and release resources."""
self._closing = True
try:
- # Close should stop queue processing immediately.
- if self._queue_manager:
- self._queue_manager.stop()
- self._queue_manager = None
- logger.info("Queue manager stopped")
+ # We do NOT stop the queue manager here as it is an injected dependency
+ # and should be managed by the creator (OpenVikingService).
# Then close the base backend
await super().close()