Skip to content

Commit f0452d0

Browse files
committed
Respond to upstream: tctc.start_workflow -> WorkflowOperationToken
1 parent ad25b20 commit f0452d0

3 files changed

Lines changed: 26 additions & 31 deletions

File tree

hello_nexus/basic/handler/service_handler.py

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@
2424
sync_operation_handler,
2525
)
2626
from temporalio.nexus.handler import (
27-
NexusStartWorkflowRequest,
2827
TemporalNexusOperationContext,
28+
WorkflowOperationToken,
2929
)
3030

3131
from hello_nexus.basic.handler.db_client import MyDBClient
@@ -45,24 +45,25 @@ def __init__(self, connected_db_client: MyDBClient):
4545
self.connected_db_client = connected_db_client
4646

4747
# This is a nexus operation that is backed by a Temporal workflow. The start method
48-
# starts a workflow, and returns a nexus operation token that the handler can use to
49-
# obtain a workflow handle (for example if a cancel request is subsequently sent by
50-
# the caller). The Temporal server takes care of delivering the workflow result to the
51-
# calling workflow. The task queue defaults to the task queue being used by the Nexus
52-
# worker.
48+
# starts a workflow, and returns a nexus operation token synchronously. Meanwhile,
49+
# the workflow executes in the background, and the Temporal server takes care of
50+
# delivering the eventual workflow result (success or failure) to the calling
51+
# workflow.
52+
#
53+
# The token will be used by the caller if it subsequently wants to cancel the Nexus
54+
# operation.
5355
@temporalio.nexus.handler.workflow_run_operation_handler
5456
async def my_workflow_run_operation(
5557
self, ctx: StartOperationContext, input: MyInput
56-
) -> NexusStartWorkflowRequest[MyOutput]:
58+
) -> WorkflowOperationToken[MyOutput]:
5759
# You could use self.connected_db_client here.
5860
tctx = TemporalNexusOperationContext.current()
59-
return NexusStartWorkflowRequest(
60-
tctx.client.start_workflow(
61-
WorkflowStartedByNexusOperation.run,
62-
input,
63-
id=str(uuid.uuid4()),
64-
task_queue=tctx.task_queue,
65-
)
61+
return await tctx.start_workflow(
62+
WorkflowStartedByNexusOperation.run,
63+
input,
64+
id=str(uuid.uuid4()),
65+
client=tctx.client,
66+
task_queue=tctx.task_queue,
6667
)
6768

6869
# This is a sync operation. That means that unlike the workflow run operation above,

hello_nexus/basic/handler/service_handler_with_operation_handler_classes.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ class directly.
3636
)
3737
from temporalio.nexus.handler import (
3838
TemporalNexusOperationContext,
39-
WorkflowOperationToken,
4039
)
4140

4241
from hello_nexus.basic.handler.db_client import MyDBClient
@@ -133,18 +132,14 @@ async def start(
133132
self, ctx: StartOperationContext, input: MyInput
134133
) -> StartOperationResultAsync:
135134
tctx = TemporalNexusOperationContext.current()
136-
wf_handle = await tctx.client.start_workflow(
135+
token = await tctx.start_workflow(
137136
WorkflowStartedByNexusOperation.run,
138137
input,
139138
id=str(uuid.uuid4()),
139+
client=tctx.client,
140140
task_queue=tctx.task_queue,
141141
)
142-
# TODO(prerelease) It must be possible to start "normal" workflows in here, and
143-
# then finish up with a "nexusified" workflow. It should not be possible to
144-
# construct a Nexus token for a non-nexusified workflow.
145-
146-
token = WorkflowOperationToken.from_workflow_handle(wf_handle).encode()
147-
return StartOperationResultAsync(token)
142+
return StartOperationResultAsync(token.encode())
148143

149144
async def cancel(self, ctx: CancelOperationContext, token: str) -> None:
150145
return await temporalio.nexus.handler.cancel_workflow(ctx, token)

hello_nexus/without_service_definition/app.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
from temporalio import workflow
1414
from temporalio.client import Client
1515
from temporalio.nexus.handler import (
16-
NexusStartWorkflowRequest,
1716
TemporalNexusOperationContext,
17+
WorkflowOperationToken,
1818
workflow_run_operation_handler,
1919
)
2020
from temporalio.worker import UnsandboxedWorkflowRunner, Worker
@@ -47,15 +47,14 @@ class MyNexusServiceHandler:
4747
@workflow_run_operation_handler
4848
async def my_workflow_run_operation(
4949
self, ctx: StartOperationContext, name: str
50-
) -> NexusStartWorkflowRequest[str]:
50+
) -> WorkflowOperationToken[str]:
5151
tctx = TemporalNexusOperationContext.current()
52-
return NexusStartWorkflowRequest(
53-
tctx.client.start_workflow(
54-
HandlerWorkflow.run,
55-
name,
56-
id=str(uuid.uuid4()),
57-
task_queue=tctx.task_queue,
58-
)
52+
return await tctx.start_workflow(
53+
HandlerWorkflow.run,
54+
name,
55+
id=str(uuid.uuid4()),
56+
client=tctx.client,
57+
task_queue=tctx.task_queue,
5958
)
6059

6160

0 commit comments

Comments
 (0)