Skip to content

Commit feabf24

Browse files
committed
Respond to upstream: use factories instead of decorators
1 parent f0452d0 commit feabf24

3 files changed

Lines changed: 68 additions & 58 deletions

File tree

Lines changed: 35 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
"""
2-
This file demonstrates how to define operation handlers by using the "shorthand"
3-
decorators sync_operation_handler and workflow_run_operation_handler. In this style you
4-
implement the `start` method only. workflow_run_operation_handler implements `cancel` for
5-
you automatically, but apart from that, the other operation methods (`fetch_info`,
6-
`fetch_result`, and `cancel` for sync_operation_handler) are all automatically created
7-
with "raise NotImplementedError" implementations.
2+
This file demonstrates how to define operation handlers by using a "shorthand" style in
3+
which you implement the `start` method only. WorkflowRunOperationHandler implements
4+
`cancel` for you automatically, but apart from that, the other operation methods
5+
(`fetch_info`, `fetch_result`, and `cancel` for SyncOperationHandler) are all
6+
automatically created with "raise NotImplementedError" implementations.
87
98
See hello_nexus/basic/handler/service_handler_with_operation_handler_classes.py for the
109
alternative "fully manual" style where you implement an OperationHandler class directly.
@@ -14,18 +13,17 @@
1413

1514
import uuid
1615

17-
import nexusrpc.handler
18-
import temporalio.common
19-
import temporalio.nexus
20-
import temporalio.nexus.handler
2116
from nexusrpc.handler import (
17+
OperationHandler,
2218
StartOperationContext,
19+
SyncOperationHandler,
20+
operation_handler,
2321
service_handler,
24-
sync_operation_handler,
2522
)
2623
from temporalio.nexus.handler import (
27-
TemporalNexusOperationContext,
24+
TemporalOperationContext,
2825
WorkflowOperationToken,
26+
WorkflowRunOperationHandler,
2927
)
3028

3129
from hello_nexus.basic.handler.db_client import MyDBClient
@@ -52,27 +50,33 @@ def __init__(self, connected_db_client: MyDBClient):
5250
#
5351
# The token will be used by the caller if it subsequently wants to cancel the Nexus
5452
# operation.
55-
@temporalio.nexus.handler.workflow_run_operation_handler
56-
async def my_workflow_run_operation(
57-
self, ctx: StartOperationContext, input: MyInput
58-
) -> WorkflowOperationToken[MyOutput]:
59-
# You could use self.connected_db_client here.
60-
tctx = TemporalNexusOperationContext.current()
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,
67-
)
53+
@operation_handler
54+
def my_workflow_run_operation(
55+
self,
56+
) -> OperationHandler[MyInput, MyOutput]:
57+
async def start(
58+
ctx: StartOperationContext, input: MyInput
59+
) -> WorkflowOperationToken[MyOutput]:
60+
# You could use self.connected_db_client here.
61+
tctx = TemporalOperationContext.current()
62+
return await tctx.start_workflow(
63+
WorkflowStartedByNexusOperation.run,
64+
input,
65+
id=str(uuid.uuid4()),
66+
)
67+
68+
return WorkflowRunOperationHandler(start)
6869

6970
# This is a sync operation. That means that unlike the workflow run operation above,
7071
# in this case the `start` method returns the final operation result. Sync operations
7172
# are free to make arbitrary network calls, or perform CPU-bound computations. Total
7273
# execution duration must not exceed 10s.
73-
@sync_operation_handler
74-
async def my_sync_operation(
75-
self, ctx: nexusrpc.handler.StartOperationContext, input: MyInput
76-
) -> MyOutput:
77-
# You could use self.connected_db_client here.
78-
return MyOutput(message=f"Hello {input.name} from sync operation!")
74+
@operation_handler
75+
def my_sync_operation(
76+
self,
77+
) -> OperationHandler[MyInput, MyOutput]:
78+
async def start(ctx: StartOperationContext, input: MyInput) -> MyOutput:
79+
# You could use self.connected_db_client here.
80+
return MyOutput(message=f"Hello {input.name} from sync operation!")
81+
82+
return SyncOperationHandler(start)

hello_nexus/basic/handler/service_handler_with_operation_handler_classes.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ class directly.
2020

2121
import uuid
2222

23-
import temporalio.common
24-
import temporalio.nexus.handler
2523
from nexusrpc.handler import (
2624
CancelOperationContext,
2725
FetchOperationInfoContext,
@@ -35,7 +33,8 @@ class directly.
3533
service_handler,
3634
)
3735
from temporalio.nexus.handler import (
38-
TemporalNexusOperationContext,
36+
TemporalOperationContext,
37+
cancel_operation,
3938
)
4039

4140
from hello_nexus.basic.handler.db_client import MyDBClient
@@ -131,18 +130,16 @@ class MyWorkflowRunOperation(OperationHandler[MyInput, MyOutput]):
131130
async def start(
132131
self, ctx: StartOperationContext, input: MyInput
133132
) -> StartOperationResultAsync:
134-
tctx = TemporalNexusOperationContext.current()
133+
tctx = TemporalOperationContext.current()
135134
token = await tctx.start_workflow(
136135
WorkflowStartedByNexusOperation.run,
137136
input,
138137
id=str(uuid.uuid4()),
139-
client=tctx.client,
140-
task_queue=tctx.task_queue,
141138
)
142139
return StartOperationResultAsync(token.encode())
143140

144141
async def cancel(self, ctx: CancelOperationContext, token: str) -> None:
145-
return await temporalio.nexus.handler.cancel_workflow(ctx, token)
142+
return await cancel_operation(token)
146143

147144
async def fetch_info(
148145
self, ctx: FetchOperationInfoContext, token: str

hello_nexus/without_service_definition/app.py

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,18 @@
99
import uuid
1010
from typing import Optional
1111

12-
from nexusrpc.handler import StartOperationContext, service_handler
12+
from nexusrpc.handler import (
13+
OperationHandler,
14+
StartOperationContext,
15+
operation_handler,
16+
service_handler,
17+
)
1318
from temporalio import workflow
1419
from temporalio.client import Client
1520
from temporalio.nexus.handler import (
16-
TemporalNexusOperationContext,
21+
TemporalOperationContext,
1722
WorkflowOperationToken,
18-
workflow_run_operation_handler,
23+
WorkflowRunOperationHandler,
1924
)
2025
from temporalio.worker import UnsandboxedWorkflowRunner, Worker
2126
from temporalio.workflow import NexusClient
@@ -37,25 +42,29 @@ async def run(self, message: str) -> str:
3742

3843

3944
# Here we define a nexus service by providing a service handler implementation without a
40-
# service contract.
45+
# service contract. This nexus service has one operation.
4146
@service_handler
4247
class MyNexusServiceHandler:
43-
# The nexus service has one operation. When using the workflow_run_operation_handler
44-
# decorator, your start method must return a WorkflowHandle directly, using the
45-
# temporalio.nexus.handler.start_workflow helper. (Temporal server takes care of
46-
# delivering the workflow result to the caller, using the Nexus RPC callback mechanism).
47-
@workflow_run_operation_handler
48-
async def my_workflow_run_operation(
49-
self, ctx: StartOperationContext, name: str
50-
) -> WorkflowOperationToken[str]:
51-
tctx = TemporalNexusOperationContext.current()
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,
58-
)
48+
# Here we implement a Nexus operation backed by a Temporal workflow. The start
49+
# method must use TemporalOperationContext.start_workflow to start the workflow,
50+
# which returns a WorkflowOperationToken. (Temporal server will then take care of
51+
# delivering the workflow result to the caller, using the Nexus RPC callback
52+
# mechanism).
53+
@operation_handler
54+
def my_workflow_run_operation(
55+
self,
56+
) -> OperationHandler[str, str]:
57+
async def start(
58+
ctx: StartOperationContext, name: str
59+
) -> WorkflowOperationToken[str]:
60+
tctx = TemporalOperationContext.current()
61+
return await tctx.start_workflow(
62+
HandlerWorkflow.run,
63+
name,
64+
id=str(uuid.uuid4()),
65+
)
66+
67+
return WorkflowRunOperationHandler(start)
5968

6069

6170
#

0 commit comments

Comments
 (0)