You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

api_workflow_run_repository.py 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. """
  2. API WorkflowRun Repository Protocol
  3. This module defines the protocol for service-layer WorkflowRun operations.
  4. The repository provides an abstraction layer for WorkflowRun database operations
  5. used by service classes, separating service-layer concerns from core domain logic.
  6. Key Features:
  7. - Paginated workflow run queries with filtering
  8. - Bulk deletion operations with OSS backup support
  9. - Multi-tenant data isolation
  10. - Expired record cleanup with data retention
  11. - Service-layer specific query patterns
  12. Usage:
  13. This protocol should be used by service classes that need to perform
  14. WorkflowRun database operations. It provides a clean interface that
  15. hides implementation details and supports dependency injection.
  16. Example:
  17. ```python
  18. from repositories.dify_api_repository_factory import DifyAPIRepositoryFactory
  19. session_maker = sessionmaker(bind=db.engine, expire_on_commit=False)
  20. repo = DifyAPIRepositoryFactory.create_api_workflow_run_repository(session_maker)
  21. # Get paginated workflow runs
  22. runs = repo.get_paginated_workflow_runs(
  23. tenant_id="tenant-123",
  24. app_id="app-456",
  25. triggered_from="debugging",
  26. limit=20
  27. )
  28. ```
  29. """
  30. from collections.abc import Sequence
  31. from datetime import datetime
  32. from typing import Optional, Protocol
  33. from core.workflow.repositories.workflow_execution_repository import WorkflowExecutionRepository
  34. from libs.infinite_scroll_pagination import InfiniteScrollPagination
  35. from models.workflow import WorkflowRun
  36. class APIWorkflowRunRepository(WorkflowExecutionRepository, Protocol):
  37. """
  38. Protocol for service-layer WorkflowRun repository operations.
  39. This protocol defines the interface for WorkflowRun database operations
  40. that are specific to service-layer needs, including pagination, filtering,
  41. and bulk operations with data backup support.
  42. """
  43. def get_paginated_workflow_runs(
  44. self,
  45. tenant_id: str,
  46. app_id: str,
  47. triggered_from: str,
  48. limit: int = 20,
  49. last_id: Optional[str] = None,
  50. ) -> InfiniteScrollPagination:
  51. """
  52. Get paginated workflow runs with filtering.
  53. Retrieves workflow runs for a specific app and trigger source with
  54. cursor-based pagination support. Used primarily for debugging and
  55. workflow run listing in the UI.
  56. Args:
  57. tenant_id: Tenant identifier for multi-tenant isolation
  58. app_id: Application identifier
  59. triggered_from: Filter by trigger source (e.g., "debugging", "app-run")
  60. limit: Maximum number of records to return (default: 20)
  61. last_id: Cursor for pagination - ID of the last record from previous page
  62. Returns:
  63. InfiniteScrollPagination object containing:
  64. - data: List of WorkflowRun objects
  65. - limit: Applied limit
  66. - has_more: Boolean indicating if more records exist
  67. Raises:
  68. ValueError: If last_id is provided but the corresponding record doesn't exist
  69. """
  70. ...
  71. def get_workflow_run_by_id(
  72. self,
  73. tenant_id: str,
  74. app_id: str,
  75. run_id: str,
  76. ) -> Optional[WorkflowRun]:
  77. """
  78. Get a specific workflow run by ID.
  79. Retrieves a single workflow run with tenant and app isolation.
  80. Used for workflow run detail views and execution tracking.
  81. Args:
  82. tenant_id: Tenant identifier for multi-tenant isolation
  83. app_id: Application identifier
  84. run_id: Workflow run identifier
  85. Returns:
  86. WorkflowRun object if found, None otherwise
  87. """
  88. ...
  89. def get_expired_runs_batch(
  90. self,
  91. tenant_id: str,
  92. before_date: datetime,
  93. batch_size: int = 1000,
  94. ) -> Sequence[WorkflowRun]:
  95. """
  96. Get a batch of expired workflow runs for cleanup.
  97. Retrieves workflow runs created before the specified date for
  98. cleanup operations. Used by scheduled tasks to remove old data
  99. while maintaining data retention policies.
  100. Args:
  101. tenant_id: Tenant identifier for multi-tenant isolation
  102. before_date: Only return runs created before this date
  103. batch_size: Maximum number of records to return
  104. Returns:
  105. Sequence of WorkflowRun objects to be processed for cleanup
  106. """
  107. ...
  108. def delete_runs_by_ids(
  109. self,
  110. run_ids: Sequence[str],
  111. ) -> int:
  112. """
  113. Delete workflow runs by their IDs.
  114. Performs bulk deletion of workflow runs by ID. This method should
  115. be used after backing up the data to OSS storage for retention.
  116. Args:
  117. run_ids: Sequence of workflow run IDs to delete
  118. Returns:
  119. Number of records actually deleted
  120. Note:
  121. This method performs hard deletion. Ensure data is backed up
  122. to OSS storage before calling this method for compliance with
  123. data retention policies.
  124. """
  125. ...
  126. def delete_runs_by_app(
  127. self,
  128. tenant_id: str,
  129. app_id: str,
  130. batch_size: int = 1000,
  131. ) -> int:
  132. """
  133. Delete all workflow runs for a specific app.
  134. Performs bulk deletion of all workflow runs associated with an app.
  135. Used during app cleanup operations. Processes records in batches
  136. to avoid memory issues and long-running transactions.
  137. Args:
  138. tenant_id: Tenant identifier for multi-tenant isolation
  139. app_id: Application identifier
  140. batch_size: Number of records to process in each batch
  141. Returns:
  142. Total number of records deleted across all batches
  143. Note:
  144. This method performs hard deletion without backup. Use with caution
  145. and ensure proper data retention policies are followed.
  146. """
  147. ...