-
Notifications
You must be signed in to change notification settings - Fork 5
feat(worker): return workflow completed result on decision processing #54
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
shijiesheng
merged 5 commits into
cadence-workflow:main
from
shijiesheng:replay-aware-with-result
Nov 24, 2025
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
cf54981
feat(worker): return workflow completed result on decision processing
shijiesheng 9d94446
fix until WorkflowDefinition
shijiesheng c8d58bc
fix some workflow instance bugs
shijiesheng c338014
revert unwanted changes
shijiesheng 26407e0
remove is_started method
shijiesheng File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,38 @@ | ||
| from asyncio import Task | ||
| from typing import Any, Optional | ||
| from cadence._internal.workflow.deterministic_event_loop import DeterministicEventLoop | ||
| from cadence.api.v1.common_pb2 import Payload | ||
| from cadence.data_converter import DataConverter | ||
| from cadence.workflow import WorkflowDefinition | ||
|
|
||
|
|
||
| class WorkflowInstance: | ||
| def __init__(self, workflow_definition: WorkflowDefinition): | ||
| def __init__( | ||
| self, workflow_definition: WorkflowDefinition, data_converter: DataConverter | ||
| ): | ||
| self._definition = workflow_definition | ||
| self._instance = workflow_definition.cls().__init__() | ||
| self._data_converter = data_converter | ||
| self._instance = workflow_definition.cls() # construct a new workflow object | ||
| self._loop = DeterministicEventLoop() | ||
| self._task: Optional[Task] = None | ||
|
|
||
| async def run(self, *args): | ||
| run_method = self._definition.get_run_method(self._instance) | ||
| return run_method(*args) | ||
| def start(self, input: Payload): | ||
| if self._task is None: | ||
| run_method = self._definition.get_run_method(self._instance) | ||
| # TODO handle multiple inputs | ||
| workflow_input = self._data_converter.from_data(input, [Any]) | ||
| self._task = self._loop.create_task(run_method(*workflow_input)) | ||
|
|
||
| def run_once(self): | ||
| self._loop.run_until_yield() | ||
|
|
||
| def is_done(self) -> bool: | ||
| return self._task is not None and self._task.done() | ||
|
|
||
| # TODO: consider cache result to avoid multiple data conversions | ||
| def get_result(self) -> Payload: | ||
| if self._task is None: | ||
| raise RuntimeError("Workflow is not started yet") | ||
| result = self._task.result() | ||
| # TODO: handle result with multiple outputs | ||
| return self._data_converter.to_data([result]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| #!/usr/bin/env python3 | ||
| from typing import List | ||
| import pytest | ||
| from cadence.api.v1.common_pb2 import Payload | ||
| from cadence.api.v1.history_pb2 import ( | ||
| DecisionTaskCompletedEventAttributes, | ||
| DecisionTaskScheduledEventAttributes, | ||
| DecisionTaskStartedEventAttributes, | ||
| HistoryEvent, | ||
| WorkflowExecutionCompletedEventAttributes, | ||
| WorkflowExecutionStartedEventAttributes, | ||
| ) | ||
| from cadence._internal.workflow.workflow_engine import WorkflowEngine | ||
| from cadence import workflow | ||
| from cadence.data_converter import DefaultDataConverter | ||
| from cadence.workflow import WorkflowInfo, WorkflowDefinition, WorkflowDefinitionOptions | ||
|
|
||
|
|
||
| class TestWorkflow: | ||
| @workflow.run | ||
| async def echo(self, input_data): | ||
| return f"echo: {input_data}" | ||
|
|
||
|
|
||
| class TestWorkflowEngine: | ||
| """Unit tests for WorkflowEngine.""" | ||
|
|
||
| @pytest.fixture | ||
| def echo_workflow_definition(self) -> WorkflowDefinition: | ||
| """Create a mock workflow definition.""" | ||
| workflow_opts = WorkflowDefinitionOptions(name="test_workflow") | ||
| return WorkflowDefinition.wrap(TestWorkflow, workflow_opts) | ||
|
|
||
| @pytest.fixture | ||
| def simple_workflow_events(self) -> List[HistoryEvent]: | ||
| return [ | ||
| HistoryEvent( | ||
| event_id=1, | ||
| workflow_execution_started_event_attributes=WorkflowExecutionStartedEventAttributes( | ||
| input=Payload(data=b'"test-input"') | ||
| ), | ||
| ), | ||
| HistoryEvent( | ||
| event_id=2, | ||
| decision_task_scheduled_event_attributes=DecisionTaskScheduledEventAttributes(), | ||
| ), | ||
| HistoryEvent( | ||
| event_id=3, | ||
| decision_task_started_event_attributes=DecisionTaskStartedEventAttributes( | ||
| scheduled_event_id=2 | ||
| ), | ||
| ), | ||
| HistoryEvent( | ||
| event_id=4, | ||
| decision_task_completed_event_attributes=DecisionTaskCompletedEventAttributes( | ||
| scheduled_event_id=2, | ||
| ), | ||
| ), | ||
| HistoryEvent( | ||
| event_id=5, | ||
| workflow_execution_completed_event_attributes=WorkflowExecutionCompletedEventAttributes( | ||
| result=Payload(data=b'"echo: test-input"') | ||
| ), | ||
| ), | ||
| ] | ||
|
|
||
| def test_process_simple_workflow( | ||
| self, | ||
| echo_workflow_definition: WorkflowDefinition, | ||
| simple_workflow_events: List[HistoryEvent], | ||
| ): | ||
| workflow_engine = create_workflow_engine(echo_workflow_definition) | ||
| decision_result = workflow_engine.process_decision(simple_workflow_events[:3]) | ||
| assert len(decision_result.decisions) == 1 | ||
| assert decision_result.decisions[ | ||
| 0 | ||
| ].complete_workflow_execution_decision_attributes.result == Payload( | ||
| data=b'"echo: test-input"' | ||
| ) | ||
|
|
||
|
|
||
| def create_workflow_engine(workflow_definition: WorkflowDefinition) -> WorkflowEngine: | ||
| """Create workflow engine.""" | ||
| return WorkflowEngine( | ||
| info=WorkflowInfo( | ||
| workflow_type="test_workflow", | ||
| workflow_domain="test-domain", | ||
| workflow_id="test-workflow-id", | ||
| workflow_run_id="test-run-id", | ||
| workflow_task_list="test-task-list", | ||
| data_converter=DefaultDataConverter(), | ||
| ), | ||
| workflow_definition=workflow_definition, | ||
| ) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
override so I could steal the activated context rather than exposed
WorkflowContext. Maybe this is not necessary but I'm trying to avoid direct access to self._context inside WorkflowEngine.