-
Notifications
You must be signed in to change notification settings - Fork 12
feat: define child workflow state machine #99
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
Open
timl3136
wants to merge
2
commits into
cadence-workflow:main
Choose a base branch
from
timl3136:child-wf-1
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
188 changes: 188 additions & 0 deletions
188
cadence/_internal/workflow/statemachine/child_workflow_execution_state_machine.py
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,188 @@ | ||
| from __future__ import annotations | ||
|
|
||
| from cadence._internal.workflow.statemachine.decision_state_machine import ( | ||
| BaseDecisionStateMachine, | ||
| DecisionFuture, | ||
| DecisionId, | ||
| DecisionState, | ||
| DecisionType, | ||
| ) | ||
| from cadence._internal.workflow.statemachine.event_dispatcher import EventDispatcher | ||
| from cadence._internal.workflow.statemachine.nondeterminism import ( | ||
| record_immediate_cancel, | ||
| ) | ||
| from cadence.api.v1 import decision, history | ||
| from cadence.api.v1.common_pb2 import Payload, WorkflowExecution | ||
| from cadence.error import ( | ||
| ChildWorkflowExecutionCanceled, | ||
| ChildWorkflowExecutionFailed, | ||
| ChildWorkflowExecutionTerminated, | ||
| ChildWorkflowExecutionTimedOut, | ||
| StartChildWorkflowExecutionFailed, | ||
| ) | ||
|
|
||
| # Default id_attr is "initiated_event_id" because the majority of child workflow events | ||
| # reference the state machine by the event ID of the InitiatedEvent. handle_initiated is | ||
| # the exception — it uses "workflow_id" directly and then registers the event ID as an alias. | ||
| child_workflow_events = EventDispatcher("initiated_event_id") | ||
|
|
||
|
|
||
| class ChildWorkflowExecutionStateMachine(BaseDecisionStateMachine): | ||
| """State machine for StartChildWorkflowExecution and child close events.""" | ||
|
|
||
| request: decision.StartChildWorkflowExecutionDecisionAttributes | ||
| execution: DecisionFuture[WorkflowExecution] | ||
| result: DecisionFuture[Payload] | ||
| _run_id: str | None | ||
|
|
||
| def __init__( | ||
| self, | ||
| request: decision.StartChildWorkflowExecutionDecisionAttributes, | ||
| execution: DecisionFuture[WorkflowExecution], | ||
| result: DecisionFuture[Payload], | ||
| ) -> None: | ||
| super().__init__() | ||
| self.request = request | ||
| self.execution = execution | ||
| self.result = result | ||
| self._run_id = None | ||
|
|
||
| def get_id(self) -> DecisionId: | ||
| return DecisionId(DecisionType.CHILD_WORKFLOW, self.request.workflow_id) | ||
|
|
||
| def get_decision(self) -> decision.Decision | None: | ||
| if self.state is DecisionState.REQUESTED: | ||
| return decision.Decision( | ||
| start_child_workflow_execution_decision_attributes=self.request | ||
| ) | ||
| if self.state is DecisionState.CANCELED_AFTER_REQUESTED: | ||
| return record_immediate_cancel(self.request) | ||
| if self.state in ( | ||
| DecisionState.CANCELED_AFTER_RECORDED, | ||
| DecisionState.CANCELED_AFTER_STARTED, | ||
| ): | ||
| return decision.Decision( | ||
| request_cancel_external_workflow_execution_decision_attributes=decision.RequestCancelExternalWorkflowExecutionDecisionAttributes( | ||
| domain=self.request.domain, | ||
| workflow_execution=WorkflowExecution( | ||
| workflow_id=self.request.workflow_id, | ||
| run_id=self._run_id or "", | ||
| ), | ||
| child_workflow_only=True, | ||
| ) | ||
| ) | ||
| return None | ||
|
|
||
| def request_cancel(self) -> bool: | ||
| if self.state is DecisionState.REQUESTED: | ||
| self._transition(DecisionState.CANCELED_AFTER_REQUESTED) | ||
| self.execution.force_cancel() | ||
| self.result.force_cancel() | ||
| return True | ||
|
|
||
| if self.state is DecisionState.RECORDED: | ||
| self._transition(DecisionState.CANCELED_AFTER_RECORDED) | ||
| self.execution.force_cancel() | ||
| return True | ||
|
|
||
| if self.state is DecisionState.STARTED: | ||
| # We have a run_id at this point; use CANCELED_AFTER_STARTED so the | ||
| # cancel decision includes it, which avoids a potential race where we | ||
| # try to cancel before the server has finished processing the start. | ||
| self._transition(DecisionState.CANCELED_AFTER_STARTED) | ||
| return True | ||
|
|
||
| return False | ||
|
|
||
| @child_workflow_events.event("workflow_id", event_id_is_alias=True) | ||
| def handle_initiated( | ||
| self, _: history.StartChildWorkflowExecutionInitiatedEventAttributes | ||
| ) -> None: | ||
| self._transition(DecisionState.RECORDED) | ||
|
|
||
| @child_workflow_events.event() | ||
| def handle_initiation_failed( | ||
| self, event: history.StartChildWorkflowExecutionFailedEventAttributes | ||
| ) -> None: | ||
| self._transition(DecisionState.COMPLETED) | ||
| exc = StartChildWorkflowExecutionFailed( | ||
| f"start child failed: {event.cause}", | ||
| cause=event.cause, | ||
| workflow_id=event.workflow_id, | ||
| ) | ||
| self.execution.set_exception(exc) | ||
| self.result.set_exception(exc) | ||
|
|
||
| @child_workflow_events.event() | ||
| def handle_started( | ||
| self, event: history.ChildWorkflowExecutionStartedEventAttributes | ||
| ) -> None: | ||
| self._transition(DecisionState.STARTED) | ||
| self._run_id = event.workflow_execution.run_id | ||
| self.execution.set_result(event.workflow_execution) | ||
|
|
||
| @child_workflow_events.event() | ||
| def handle_completed( | ||
| self, event: history.ChildWorkflowExecutionCompletedEventAttributes | ||
| ) -> None: | ||
| self._transition(DecisionState.COMPLETED) | ||
| self.result.set_result(event.result) | ||
|
|
||
| @child_workflow_events.event() | ||
| def handle_failed( | ||
| self, event: history.ChildWorkflowExecutionFailedEventAttributes | ||
| ) -> None: | ||
| self._transition(DecisionState.COMPLETED) | ||
| self.result.set_exception( | ||
| ChildWorkflowExecutionFailed( | ||
| event.failure.reason, | ||
| failure=event.failure, | ||
| ) | ||
| ) | ||
|
|
||
| @child_workflow_events.event() | ||
| def handle_canceled( | ||
| self, event: history.ChildWorkflowExecutionCanceledEventAttributes | ||
| ) -> None: | ||
| self._transition(DecisionState.COMPLETED) | ||
| self.result.set_exception( | ||
| ChildWorkflowExecutionCanceled( | ||
| "child workflow canceled", details=event.details | ||
| ) | ||
| ) | ||
|
|
||
| @child_workflow_events.event() | ||
| def handle_timed_out( | ||
| self, event: history.ChildWorkflowExecutionTimedOutEventAttributes | ||
| ) -> None: | ||
| self._transition(DecisionState.COMPLETED) | ||
| self.result.set_exception( | ||
| ChildWorkflowExecutionTimedOut( | ||
| f"child workflow timed out: {event.timeout_type}", | ||
| timeout_type=int(event.timeout_type), | ||
| ) | ||
| ) | ||
|
|
||
| @child_workflow_events.event() | ||
| def handle_terminated( | ||
| self, event: history.ChildWorkflowExecutionTerminatedEventAttributes | ||
| ) -> None: | ||
| self._transition(DecisionState.COMPLETED) | ||
| self.result.set_exception(ChildWorkflowExecutionTerminated()) | ||
|
|
||
| # RequestCancelExternalWorkflowExecution events reference the child workflow by | ||
| # workflow_execution.workflow_id (a nested field), not by a bare string id. | ||
| # The dispatcher resolves dotted paths, so "workflow_execution.workflow_id" extracts | ||
| # the correct key for the alias lookup. event_id_is_alias=True registers this event's | ||
| # ID so that the subsequent handle_cancel_failed can look it up via initiated_event_id. | ||
| @child_workflow_events.event("workflow_execution.workflow_id", event_id_is_alias=True) | ||
| def handle_cancel_initiated( | ||
| self, _: history.RequestCancelExternalWorkflowExecutionInitiatedEventAttributes | ||
| ) -> None: | ||
| self._transition(DecisionState.CANCELLATION_RECORDED) | ||
|
|
||
| @child_workflow_events.event() | ||
| def handle_cancel_failed( | ||
| self, _: history.RequestCancelExternalWorkflowExecutionFailedEventAttributes | ||
| ) -> None: | ||
| self._transition(DecisionState.STARTED) |
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
Oops, something went wrong.
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.
The expectation value shouldn't be CANCEL here and we need to add Expectation building for the other cancellation events.