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
98See hello_nexus/basic/handler/service_handler_with_operation_handler_classes.py for the
109alternative "fully manual" style where you implement an OperationHandler class directly.
1413
1514import uuid
1615
17- import nexusrpc .handler
18- import temporalio .common
19- import temporalio .nexus
20- import temporalio .nexus .handler
2116from nexusrpc .handler import (
17+ OperationHandler ,
2218 StartOperationContext ,
19+ SyncOperationHandler ,
20+ operation_handler ,
2321 service_handler ,
24- sync_operation_handler ,
2522)
2623from temporalio .nexus .handler import (
27- TemporalNexusOperationContext ,
24+ TemporalOperationContext ,
2825 WorkflowOperationToken ,
26+ WorkflowRunOperationHandler ,
2927)
3028
3129from 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 )
0 commit comments