Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,25 @@ def save_synchronously(self, execution: WorkflowNodeExecution) -> None:
if execution.id not in execution_ids:
execution_ids.append(execution.id)

@override
def save_execution_data(self, execution: WorkflowNodeExecution) -> None:
"""Persist offloaded node execution data synchronously and refresh the cache.

``WorkflowNodeExecutionRepository`` declares this method and the workflow
persistence layer calls it on every node completion
(``core/app/workflow/layers/persistence.py``), so the Celery backend must
implement it too. The SQLAlchemy repository mutates the domain model in
place (``set_truncated_inputs`` / ``set_truncated_outputs`` / ...), so the
cached instance is refreshed afterwards to keep reads consistent.
"""

self._sql_repository.save_execution_data(execution)
self._execution_cache[execution.id] = execution
if execution.workflow_execution_id:
execution_ids = self._workflow_execution_mapping.setdefault(execution.workflow_execution_id, [])
if execution.id not in execution_ids:
execution_ids.append(execution.id)

@override
def get_by_workflow_execution(
self,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -436,3 +436,41 @@ def test_ordering_functionality(self, mock_task, sqlite_session_factory, account
assert len(result) == 2
assert result[0].index == 2
assert result[1].index == 1

@pytest.mark.parametrize("workflow_state", ["uncached", "cached", "standalone"])
@patch("core.repositories.celery_workflow_node_execution_repository.save_workflow_node_execution_task")
def test_save_execution_data_delegates_and_refreshes_cache(
self, mock_task, sqlite_session_factory, account, sample_workflow_node_execution, workflow_state
):
"""`save_execution_data` is part of the repository protocol and must exist.

The workflow persistence layer calls it on every node completion, so the
Celery backend cannot omit it.
"""
repo = CeleryWorkflowNodeExecutionRepository(
session_factory=sqlite_session_factory,
tenant_id=RESOURCE_TENANT_ID,
user=account,
app_id="test-app",
triggered_from=WorkflowNodeExecutionTriggeredFrom.WORKFLOW_RUN,
)
if workflow_state == "standalone":
sample_workflow_node_execution = sample_workflow_node_execution.model_copy(
update={"workflow_execution_id": None}
)
elif workflow_state == "cached":
repo.save(sample_workflow_node_execution)

with patch.object(repo._sql_repository, "save_execution_data") as save_execution_data:
repo.save_execution_data(sample_workflow_node_execution)

save_execution_data.assert_called_once_with(sample_workflow_node_execution)
# The SQL repository mutates the domain model in place, so the cache must
# still expose the very same instance afterwards.
assert repo._execution_cache[sample_workflow_node_execution.id] is sample_workflow_node_execution
if workflow_state == "standalone":
assert repo._workflow_execution_mapping == {}
else:
assert repo._workflow_execution_mapping[sample_workflow_node_execution.workflow_execution_id] == [
sample_workflow_node_execution.id
]
Loading