|
2 | 2 |
|
3 | 3 | from abc import ABC, abstractmethod |
4 | 4 | from collections.abc import Awaitable, Callable |
| 5 | +from dataclasses import dataclass |
5 | 6 | from typing import Any |
6 | 7 |
|
7 | 8 | from nexusrpc import ( |
@@ -123,6 +124,22 @@ async def _cancel_workflow( |
123 | 124 | await client_workflow_handle.cancel(**kwargs) |
124 | 125 |
|
125 | 126 |
|
| 127 | +@dataclass(frozen=True) |
| 128 | +class CancelWorkflowRunOptions: |
| 129 | + """Options for cancelling the workflow backing a Nexus operation. |
| 130 | +
|
| 131 | + These options are built by :py:class:`TemporalNexusOperationHandler` and passed to |
| 132 | + :py:meth:`TemporalNexusOperationHandler.cancel_workflow_run`. Fields may be added in |
| 133 | + the future; overrides should consume the fields they need. |
| 134 | +
|
| 135 | + .. warning:: |
| 136 | + This API is experimental and unstable. |
| 137 | + """ |
| 138 | + |
| 139 | + workflow_id: str |
| 140 | + """The ID of the workflow to cancel.""" |
| 141 | + |
| 142 | + |
126 | 143 | class TemporalNexusOperationHandler(OperationHandler[InputT, OutputT], ABC): |
127 | 144 | """Operation handler for Nexus operations that interact with Temporal. |
128 | 145 | Implementations override the start_operation method. |
@@ -175,19 +192,24 @@ async def cancel(self, ctx: CancelOperationContext, token: str) -> None: |
175 | 192 | ) |
176 | 193 | match operation_token.type: |
177 | 194 | case OperationTokenType.WORKFLOW: |
178 | | - await self.cancel_workflow_run(cancel_ctx, operation_token.workflow_id) |
| 195 | + options = CancelWorkflowRunOptions( |
| 196 | + workflow_id=operation_token.workflow_id |
| 197 | + ) |
| 198 | + await self.cancel_workflow_run(cancel_ctx, options) |
179 | 199 |
|
180 | 200 | async def cancel_workflow_run( |
181 | 201 | self, |
182 | 202 | ctx: TemporalNexusCancelOperationContext, # pyright: ignore[reportUnusedParameter] |
183 | | - workflow_id: str, |
184 | | - ): |
185 | | - """Cancels the workflow identified by workflow_id. |
| 203 | + options: CancelWorkflowRunOptions, |
| 204 | + ) -> None: |
| 205 | + """Cancels the workflow backing the Nexus operation. |
186 | 206 |
|
187 | 207 | .. warning:: |
188 | 208 | This API is experimental and unstable. |
189 | 209 | """ |
190 | | - workflow_handle = temporalio.nexus.client().get_workflow_handle(workflow_id) |
| 210 | + workflow_handle = temporalio.nexus.client().get_workflow_handle( |
| 211 | + options.workflow_id |
| 212 | + ) |
191 | 213 | await workflow_handle.cancel() |
192 | 214 |
|
193 | 215 |
|
|
0 commit comments