您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

repository_registry.py 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. """
  2. Registry for repository implementations.
  3. This module is responsible for registering factory functions with the repository factory.
  4. """
  5. import logging
  6. from collections.abc import Mapping
  7. from typing import Any
  8. from sqlalchemy.orm import sessionmaker
  9. from configs import dify_config
  10. from core.repository.repository_factory import RepositoryFactory
  11. from extensions.ext_database import db
  12. from repositories.workflow_node_execution import SQLAlchemyWorkflowNodeExecutionRepository
  13. logger = logging.getLogger(__name__)
  14. # Storage type constants
  15. STORAGE_TYPE_RDBMS = "rdbms"
  16. STORAGE_TYPE_HYBRID = "hybrid"
  17. def register_repositories() -> None:
  18. """
  19. Register repository factory functions with the RepositoryFactory.
  20. This function reads configuration settings to determine which repository
  21. implementations to register.
  22. """
  23. # Configure WorkflowNodeExecutionRepository factory based on configuration
  24. workflow_node_execution_storage = dify_config.WORKFLOW_NODE_EXECUTION_STORAGE
  25. # Check storage type and register appropriate implementation
  26. if workflow_node_execution_storage == STORAGE_TYPE_RDBMS:
  27. # Register SQLAlchemy implementation for RDBMS storage
  28. logger.info("Registering WorkflowNodeExecution repository with RDBMS storage")
  29. RepositoryFactory.register_workflow_node_execution_factory(create_workflow_node_execution_repository)
  30. elif workflow_node_execution_storage == STORAGE_TYPE_HYBRID:
  31. # Hybrid storage is not yet implemented
  32. raise NotImplementedError("Hybrid storage for WorkflowNodeExecution repository is not yet implemented")
  33. else:
  34. # Unknown storage type
  35. raise ValueError(
  36. f"Unknown storage type '{workflow_node_execution_storage}' for WorkflowNodeExecution repository. "
  37. f"Supported types: {STORAGE_TYPE_RDBMS}"
  38. )
  39. def create_workflow_node_execution_repository(params: Mapping[str, Any]) -> SQLAlchemyWorkflowNodeExecutionRepository:
  40. """
  41. Create a WorkflowNodeExecutionRepository instance using SQLAlchemy implementation.
  42. This factory function creates a repository for the RDBMS storage type.
  43. Args:
  44. params: Parameters for creating the repository, including:
  45. - tenant_id: Required. The tenant ID for multi-tenancy.
  46. - app_id: Optional. The application ID for filtering.
  47. - session_factory: Optional. A SQLAlchemy sessionmaker instance. If not provided,
  48. a new sessionmaker will be created using the global database engine.
  49. Returns:
  50. A WorkflowNodeExecutionRepository instance
  51. Raises:
  52. ValueError: If required parameters are missing
  53. """
  54. # Extract required parameters
  55. tenant_id = params.get("tenant_id")
  56. if tenant_id is None:
  57. raise ValueError("tenant_id is required for WorkflowNodeExecution repository with RDBMS storage")
  58. # Extract optional parameters
  59. app_id = params.get("app_id")
  60. # Use the session_factory from params if provided, otherwise create one using the global db engine
  61. session_factory = params.get("session_factory")
  62. if session_factory is None:
  63. # Create a sessionmaker using the same engine as the global db session
  64. session_factory = sessionmaker(bind=db.engine)
  65. # Create and return the repository
  66. return SQLAlchemyWorkflowNodeExecutionRepository(
  67. session_factory=session_factory, tenant_id=tenant_id, app_id=app_id
  68. )