Skip to content

Commit 6467b77

Browse files
committed
Respond to upstream: from_callable, inherit from abstract base class
1 parent 096947b commit 6467b77

3 files changed

Lines changed: 8 additions & 61 deletions

File tree

hello_nexus/basic/handler/service_handler.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ async def start(
6464
id=str(uuid.uuid4()),
6565
)
6666

67-
return WorkflowRunOperationHandler(start)
67+
return WorkflowRunOperationHandler.from_callable(start)
6868

6969
# This is a sync operation. That means that unlike the workflow run operation above,
7070
# in this case the `start` method returns the final operation result. Sync operations
@@ -78,4 +78,4 @@ async def start(ctx: StartOperationContext, input: MyInput) -> MyOutput:
7878
# You could use self.connected_db_client here.
7979
return MyOutput(message=f"Hello {input.name} from sync operation!")
8080

81-
return SyncOperationHandler(start)
81+
return SyncOperationHandler.from_callable(start)

hello_nexus/basic/handler/service_handler_with_operation_handler_classes.py

Lines changed: 5 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -21,21 +21,15 @@ class directly.
2121
import uuid
2222

2323
from nexusrpc.handler import (
24-
CancelOperationContext,
25-
FetchOperationInfoContext,
26-
FetchOperationResultContext,
2724
OperationHandler,
28-
OperationInfo,
2925
StartOperationContext,
3026
StartOperationResultAsync,
3127
StartOperationResultSync,
28+
SyncOperationHandler,
3229
operation_handler,
3330
service_handler,
3431
)
35-
from temporalio.nexus.handler import (
36-
cancel_operation,
37-
start_workflow,
38-
)
32+
from temporalio.nexus.handler import WorkflowRunOperationHandler, start_workflow
3933

4034
from hello_nexus.basic.handler.db_client import MyDBClient
4135
from hello_nexus.basic.handler.service_handler import MyInput, MyNexusService, MyOutput
@@ -67,7 +61,7 @@ def my_workflow_run_operation(
6761

6862

6963
# This is a Nexus operation that responds synchronously to all requests.
70-
class MySyncOperation(OperationHandler[MyInput, MyOutput]):
64+
class MySyncOperation(SyncOperationHandler[MyInput, MyOutput]):
7165
# You can add an __init__ method taking any required arguments, since you are in
7266
# control of instantiating the OperationHandler inside the operation handler method
7367
# above decorated with @operation_handler.
@@ -82,45 +76,16 @@ async def start(
8276
output = MyOutput(message=f"Hello {input.name} from sync operation!")
8377
return StartOperationResultSync(output)
8478

85-
async def fetch_info(
86-
self,
87-
ctx: FetchOperationInfoContext,
88-
token: str,
89-
) -> OperationInfo:
90-
raise NotImplementedError(
91-
"fetch_info is not supported when a Nexus operation is called by a Temporal workflow"
92-
)
93-
94-
async def fetch_result(
95-
self,
96-
ctx: FetchOperationResultContext,
97-
token: str,
98-
) -> MyOutput:
99-
raise NotImplementedError(
100-
"fetch_result is not supported when a Nexus operation is called by a Temporal workflow, "
101-
"but this sample does not demonstrate result fetching"
102-
)
103-
104-
async def cancel(
105-
self,
106-
ctx: CancelOperationContext,
107-
token: str,
108-
) -> None:
109-
raise NotImplementedError(
110-
"cancel is supported when a Nexus operation is called by a Temporal workflow, "
111-
"but this sample does not demonstrate cancellation"
112-
)
113-
11479

11580
# This is a Nexus operation that is backed by a Temporal workflow. That means that it
11681
# responds asynchronously to all requests: it starts a workflow and responds with a token
11782
# that the handler can associate with the worklow is started.
118-
class MyWorkflowRunOperation(OperationHandler[MyInput, MyOutput]):
83+
class MyWorkflowRunOperation(WorkflowRunOperationHandler[MyInput, MyOutput]):
11984
# You can add an __init__ method taking any required arguments, since you are in
12085
# control of instantiating the OperationHandler inside the operation handler method
12186
# above decorated with @operation_handler.
12287

123-
# The start method starts a workflow, and returns a WorkflowRunOperationResult that it
88+
# The start method starts a workflow, and returns a StartOperationResultAsync that it
12489
# creates from the workflow handle. This return value contains the Nexus operation
12590
# token that the handler can use to obtain a handle and interact with the workflow on
12691
# future requests (for example if a cancel request is subsequently sent by the
@@ -136,21 +101,3 @@ async def start(
136101
id=str(uuid.uuid4()),
137102
)
138103
return StartOperationResultAsync(token.encode())
139-
140-
async def cancel(self, ctx: CancelOperationContext, token: str) -> None:
141-
return await cancel_operation(token)
142-
143-
async def fetch_info(
144-
self, ctx: FetchOperationInfoContext, token: str
145-
) -> OperationInfo:
146-
raise NotImplementedError(
147-
"fetch_info is not supported when a Nexus operation is called by a Temporal workflow"
148-
)
149-
150-
async def fetch_result(
151-
self, ctx: FetchOperationResultContext, token: str
152-
) -> MyOutput:
153-
raise NotImplementedError(
154-
"fetch_result is not supported when a Nexus operation is called by a Temporal workflow, "
155-
"but this sample does not demonstrate result fetching"
156-
)

hello_nexus/without_service_definition/app.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ async def start(
6363
id=str(uuid.uuid4()),
6464
)
6565

66-
return WorkflowRunOperationHandler(start)
66+
return WorkflowRunOperationHandler.from_callable(start)
6767

6868

6969
#

0 commit comments

Comments
 (0)