-
Notifications
You must be signed in to change notification settings - Fork 209
NEXUS-484: Support UpdateWorkflow as a Nexus Operation #1631
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -138,6 +138,25 @@ class CancelWorkflowRunOptions: | |
| """The ID of the workflow to cancel.""" | ||
|
|
||
|
|
||
| @dataclass(frozen=True) | ||
| class CancelUpdateWorkflowOptions: | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing run ID |
||
| """Options for cancelling the workflow update backing a Nexus operation. | ||
|
|
||
| These options are built by :py:class:`TemporalOperationHandler` and passed to | ||
| :py:meth:`TemporalOperationHandler.cancel_workflow_update`. | ||
|
|
||
| .. warning:: | ||
| This API is experimental and unstable. | ||
| """ | ||
|
|
||
| workflow_id: str | ||
| """The ID of the workflow where the update is running.""" | ||
| update_id: str | ||
| """The ID of the update to cancel.""" | ||
| run_id: str | ||
| """The workflow runID that accepted the update""" | ||
|
|
||
|
|
||
| class TemporalOperationHandler(OperationHandler[InputT, OutputT], ABC): | ||
| """Operation handler for Nexus operations that interact with Temporal. | ||
| Implementations override the start_operation method. | ||
|
|
@@ -190,6 +209,15 @@ async def cancel(self, ctx: CancelOperationContext, token: str) -> None: | |
| workflow_id=operation_token.workflow_id | ||
| ) | ||
| await self.cancel_workflow_run(cancel_ctx, options) | ||
| case OperationTokenType.UPDATE_WORKFLOW: | ||
| assert operation_token.update_id is not None | ||
| assert operation_token.run_id is not None | ||
| cancel_options = CancelUpdateWorkflowOptions( | ||
| workflow_id=operation_token.workflow_id, | ||
| update_id=operation_token.update_id, | ||
| run_id=operation_token.run_id, | ||
| ) | ||
| await self.cancel_workflow_update(cancel_ctx, cancel_options) | ||
|
|
||
| async def cancel_workflow_run( | ||
| self, | ||
|
|
@@ -205,3 +233,23 @@ async def cancel_workflow_run( | |
| options.workflow_id | ||
| ) | ||
| await workflow_handle.cancel() | ||
|
|
||
| async def cancel_workflow_update( | ||
| self, | ||
| ctx: TemporalCancelOperationContext, # pyright: ignore[reportUnusedParameter] | ||
| options: CancelUpdateWorkflowOptions, # pyright: ignore[reportUnusedParameter] | ||
| ) -> None: | ||
| """Cancels the workflow update backing the Nexus operation. Cancellation is not natively supported for update-workflow Nexus operations. | ||
| Inherit a TemporalOperationHandler and override this method to run cancellable workflow updates. | ||
|
|
||
|
|
||
| .. warning:: | ||
| This API is experimental and unstable. | ||
| """ | ||
| raise HandlerError( | ||
| """ | ||
| Cancellation is not natively supported for update-workflow Nexus operations. | ||
| Inherit a TemporalOperationHandler and override this method to run cancellable workflow updates. | ||
| """, | ||
| type=HandlerErrorType.NOT_IMPLEMENTED, | ||
| ) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,12 +15,13 @@ | |
| overload, | ||
| ) | ||
|
|
||
| from nexusrpc import HandlerError, HandlerErrorType | ||
| from nexusrpc import HandlerError, HandlerErrorType, OperationError, OperationErrorState | ||
| from nexusrpc.handler import StartOperationResultAsync, StartOperationResultSync | ||
| from typing_extensions import Self | ||
|
|
||
| import temporalio.common | ||
| from temporalio.nexus._operation_context import ( | ||
| _start_nexus_backed_workflow_update, | ||
| _start_nexus_backing_workflow, | ||
| _TemporalStartOperationContext, | ||
| ) | ||
|
|
@@ -33,8 +34,11 @@ | |
| SelfType, | ||
| ) | ||
|
|
||
| from ._token import OperationToken, OperationTokenType | ||
|
|
||
| if TYPE_CHECKING: | ||
| import temporalio.client | ||
| import temporalio.workflow | ||
|
|
||
|
|
||
| _ResultT = TypeVar("_ResultT") | ||
|
|
@@ -279,6 +283,91 @@ async def start_workflow( | |
| """ | ||
| ... | ||
|
|
||
| # Overload for no-param update | ||
| @overload | ||
| async def start_workflow_update( | ||
| self, | ||
| workflow_id: str, | ||
| update: temporalio.workflow.UpdateMethodMultiParam[[Any], ReturnType], | ||
| *, | ||
| update_id: str | None = None, | ||
| rpc_metadata: Mapping[str, str | bytes] = {}, | ||
| rpc_timeout: timedelta | None = None, | ||
| run_id: str | None = None, | ||
| first_execution_run_id: str | None = None, | ||
| ) -> TemporalOperationResult[ReturnType]: ... | ||
|
|
||
| # Overload for single-param update | ||
| @overload | ||
| async def start_workflow_update( | ||
| self, | ||
| workflow_id: str, | ||
| update: temporalio.workflow.UpdateMethodMultiParam[ | ||
| [Any, ParamType], ReturnType | ||
| ], | ||
| arg: ParamType, | ||
| *, | ||
| update_id: str | None = None, | ||
| rpc_metadata: Mapping[str, str | bytes] = {}, | ||
| rpc_timeout: timedelta | None = None, | ||
| run_id: str | None = None, | ||
| first_execution_run_id: str | None = None, | ||
| ) -> TemporalOperationResult[ReturnType]: ... | ||
|
|
||
| # Overload for multi-param update | ||
| @overload | ||
| async def start_workflow_update( | ||
| self, | ||
| workflow_id: str, | ||
| update: temporalio.workflow.UpdateMethodMultiParam[MultiParamSpec, ReturnType], | ||
| *, | ||
| args: MultiParamSpec.args, # type: ignore | ||
| update_id: str | None = None, | ||
| rpc_metadata: Mapping[str, str | bytes] = {}, | ||
| rpc_timeout: timedelta | None = None, | ||
| run_id: str | None = None, | ||
| first_execution_run_id: str | None = None, | ||
| ) -> TemporalOperationResult[ReturnType]: ... | ||
|
|
||
| # Overload for string-name update | ||
| @overload | ||
| async def start_workflow_update( | ||
| self, | ||
| workflow_id: str, | ||
| update: str, | ||
| arg: Any = temporalio.common._arg_unset, | ||
| *, | ||
| args: Sequence[Any] = [], | ||
| update_id: str | None = None, | ||
| result_type: type[ReturnType] | None = None, | ||
| rpc_metadata: Mapping[str, str | bytes] = {}, | ||
| rpc_timeout: timedelta | None = None, | ||
| run_id: str | None = None, | ||
| first_execution_run_id: str | None = None, | ||
| ) -> TemporalOperationResult[ReturnType]: ... | ||
|
|
||
| @abstractmethod | ||
| async def start_workflow_update( | ||
| self, | ||
| workflow_id: str, | ||
| update: str | Callable, | ||
| arg: Any = temporalio.common._arg_unset, | ||
| *, | ||
| args: Sequence[Any] = [], | ||
| update_id: str | None = None, | ||
| result_type: type | None = None, | ||
| rpc_metadata: Mapping[str, str | bytes] = {}, | ||
| rpc_timeout: timedelta | None = None, | ||
| run_id: str | None = None, | ||
| first_execution_run_id: str | None = None, | ||
|
VegetarianOrc marked this conversation as resolved.
|
||
| ) -> TemporalOperationResult[Any]: | ||
| """Start a workflow update as the backing asynchronous Nexus operation. | ||
|
|
||
| .. warning:: | ||
| This API is experimental and unstable. | ||
| """ | ||
| ... | ||
|
|
||
|
|
||
| class _TemporalNexusClient(TemporalNexusClient): # pyright: ignore[reportUnusedClass] | ||
| """Nexus-aware wrapper around a Temporal Client. | ||
|
|
@@ -377,3 +466,55 @@ async def start_workflow( | |
| ) | ||
|
|
||
| return TemporalOperationResult.async_token(wf_handle.to_token()) | ||
|
|
||
| async def start_workflow_update( | ||
| self, | ||
| workflow_id: str, | ||
| update: str | Callable, | ||
| arg: Any = temporalio.common._arg_unset, | ||
| *, | ||
| args: Sequence[Any] = [], | ||
| update_id: str | None = None, | ||
| result_type: type | None = None, | ||
| rpc_metadata: Mapping[str, str | bytes] = {}, | ||
| rpc_timeout: timedelta | None = None, | ||
| run_id: str | None = None, | ||
| first_execution_run_id: str | None = None, | ||
| ) -> TemporalOperationResult[Any]: | ||
| """Start a workflow update as the backing asynchronous Nexus operation.""" | ||
| if not self._temporal_context.nexus_context.callback_url: | ||
| raise HandlerError( | ||
| "callback URL is required for a workflow update Nexus operation", | ||
| type=HandlerErrorType.BAD_REQUEST, | ||
| ) | ||
| with self._reserve_async_start(): | ||
| update_handle = await _start_nexus_backed_workflow_update( | ||
| temporal_context=self._temporal_context, | ||
| workflow_id=workflow_id, | ||
| update=update, | ||
| arg=arg, | ||
| args=args, | ||
| update_id=update_id, | ||
| result_type=result_type, | ||
| rpc_metadata=rpc_metadata, | ||
| rpc_timeout=rpc_timeout, | ||
| run_id=run_id, | ||
| first_execution_run_id=first_execution_run_id, | ||
| ) | ||
| # If the update has already completed, return the result synchronously | ||
| if update_handle._known_outcome is not None: | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If this case happens should we clear the flag set in
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agree with leaving as-is - how it ended - sync/async- isnt strictly related to how it started - and its always starts async because we only support wait for Accepted |
||
| try: | ||
| result = await update_handle.result() | ||
| except temporalio.client.WorkflowUpdateFailedError as err: | ||
| raise OperationError( | ||
| str(err), state=OperationErrorState.FAILED | ||
| ) from err | ||
| return TemporalOperationResult.sync(result) | ||
| token = OperationToken( | ||
| type=OperationTokenType.UPDATE_WORKFLOW, | ||
| namespace=update_handle._client.namespace, | ||
| workflow_id=update_handle.workflow_id, | ||
| update_id=update_handle.id, | ||
| run_id=update_handle.workflow_run_id, | ||
| ).encode() | ||
| return TemporalOperationResult.async_token(token) | ||
Uh oh!
There was an error while loading. Please reload this page.