Skip to content

Commit 589e39b

Browse files
committed
move to an options dataclass for cancel_workflow_run to future proof against new options
1 parent e687fc6 commit 589e39b

3 files changed

Lines changed: 36 additions & 8 deletions

File tree

temporalio/nexus/__init__.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,16 @@
2424
wait_for_worker_shutdown,
2525
wait_for_worker_shutdown_sync,
2626
)
27-
from ._operation_handlers import TemporalNexusOperationHandler
27+
from ._operation_handlers import (
28+
CancelWorkflowRunOptions,
29+
TemporalNexusOperationHandler,
30+
)
2831
from ._temporal_client import TemporalNexusClient, TemporalOperationResult
2932
from ._token import WorkflowHandle
3033

3134
__all__ = (
3235
"workflow_run_operation",
36+
"CancelWorkflowRunOptions",
3337
"Info",
3438
"LoggerAdapter",
3539
"NexusCallback",

temporalio/nexus/_operation_handlers.py

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from abc import ABC, abstractmethod
44
from collections.abc import Awaitable, Callable
5+
from dataclasses import dataclass
56
from typing import Any
67

78
from nexusrpc import (
@@ -123,6 +124,22 @@ async def _cancel_workflow(
123124
await client_workflow_handle.cancel(**kwargs)
124125

125126

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+
126143
class TemporalNexusOperationHandler(OperationHandler[InputT, OutputT], ABC):
127144
"""Operation handler for Nexus operations that interact with Temporal.
128145
Implementations override the start_operation method.
@@ -175,19 +192,24 @@ async def cancel(self, ctx: CancelOperationContext, token: str) -> None:
175192
)
176193
match operation_token.type:
177194
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)
179199

180200
async def cancel_workflow_run(
181201
self,
182202
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.
186206
187207
.. warning::
188208
This API is experimental and unstable.
189209
"""
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+
)
191213
await workflow_handle.cancel()
192214

193215

tests/nexus/test_temporal_operation.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,10 +205,12 @@ async def start_operation(
205205

206206
@override
207207
async def cancel_workflow_run(
208-
self, ctx: nexus.TemporalNexusCancelOperationContext, workflow_id: str
208+
self,
209+
ctx: nexus.TemporalNexusCancelOperationContext,
210+
options: nexus.CancelWorkflowRunOptions,
209211
):
210212
# get a handle to the workflow
211-
handle = nexus.client().get_workflow_handle(workflow_id)
213+
handle = nexus.client().get_workflow_handle(options.workflow_id)
212214

213215
# cancel the workflow
214216
await handle.cancel()

0 commit comments

Comments
 (0)