11"""
22Nexus operation handler for the on-demand pattern. Each operation receives the target
3- userId in its input, and run_from_remote starts a brand-new GreetingWorkflow.
3+ user_id in its input, and run_from_remote starts a brand-new GreetingWorkflow. Operations
4+ use Temporal operation handlers so the SDK can manage their lifecycle and link the caller's
5+ Nexus operation to the target Workflow.
46"""
57
68from __future__ import annotations
79
810import nexusrpc
911from temporalio import nexus
10- from temporalio .client import WorkflowHandle
12+ from temporalio .client import Client , WorkflowHandle
1113
1214from nexus_messaging .ondemandpattern .handler .workflows import GreetingWorkflow
1315from nexus_messaging .ondemandpattern .service import (
@@ -31,9 +33,9 @@ def _get_workflow_id(self, user_id: str) -> str:
3133 return WORKFLOW_ID_PREFIX + user_id
3234
3335 def _get_workflow_handle (
34- self , user_id : str
36+ self , client : Client , user_id : str
3537 ) -> WorkflowHandle [GreetingWorkflow , str ]:
36- return nexus . client () .get_workflow_handle_for (
38+ return client .get_workflow_handle_for (
3739 GreetingWorkflow .run , self ._get_workflow_id (user_id )
3840 )
3941
@@ -48,37 +50,55 @@ async def run_from_remote(
4850 id = self ._get_workflow_id (input .user_id ),
4951 )
5052
51- @nexusrpc . handler . sync_operation
53+ @nexus . temporal_operation
5254 async def get_languages (
53- self , ctx : nexusrpc .handler .StartOperationContext , input : GetLanguagesInput
54- ) -> GetLanguagesOutput :
55- return await self ._get_workflow_handle (input .user_id ).query (
55+ self ,
56+ _ctx : nexus .TemporalStartOperationContext ,
57+ client : nexus .TemporalNexusClient ,
58+ input : GetLanguagesInput ,
59+ ) -> nexus .TemporalOperationResult [GetLanguagesOutput ]:
60+ result = await self ._get_workflow_handle (client .client , input .user_id ).query (
5661 GreetingWorkflow .get_languages , input
5762 )
63+ return nexus .TemporalOperationResult .sync (result )
5864
59- @nexusrpc . handler . sync_operation
65+ @nexus . temporal_operation
6066 async def get_language (
61- self , ctx : nexusrpc .handler .StartOperationContext , input : GetLanguageInput
62- ) -> Language :
63- return await self ._get_workflow_handle (input .user_id ).query (
67+ self ,
68+ _ctx : nexus .TemporalStartOperationContext ,
69+ client : nexus .TemporalNexusClient ,
70+ input : GetLanguageInput ,
71+ ) -> nexus .TemporalOperationResult [Language ]:
72+ result = await self ._get_workflow_handle (client .client , input .user_id ).query (
6473 GreetingWorkflow .get_language ,
6574 )
75+ return nexus .TemporalOperationResult .sync (result )
6676
6777 # Routes to set_language_using_activity so that new languages not already in the
6878 # greetings map can be fetched via an activity.
69- @nexusrpc . handler . sync_operation
79+ @nexus . temporal_operation
7080 async def set_language (
71- self , ctx : nexusrpc .handler .StartOperationContext , input : SetLanguageInput
72- ) -> Language :
73- return await self ._get_workflow_handle (input .user_id ).execute_update (
74- GreetingWorkflow .set_language_using_activity , input
81+ self ,
82+ _ctx : nexus .TemporalStartOperationContext ,
83+ client : nexus .TemporalNexusClient ,
84+ input : SetLanguageInput ,
85+ ) -> nexus .TemporalOperationResult [Language ]:
86+ result = await self ._get_workflow_handle (
87+ client .client , input .user_id
88+ ).execute_update (
89+ GreetingWorkflow .set_language_using_activity ,
90+ input ,
7591 )
92+ return nexus .TemporalOperationResult .sync (result )
7693
77- @nexusrpc . handler . sync_operation
94+ @nexus . temporal_operation
7895 async def approve (
79- self , ctx : nexusrpc .handler .StartOperationContext , input : ApproveInput
80- ) -> ApproveOutput :
81- await self ._get_workflow_handle (input .user_id ).signal (
96+ self ,
97+ _ctx : nexus .TemporalStartOperationContext ,
98+ client : nexus .TemporalNexusClient ,
99+ input : ApproveInput ,
100+ ) -> nexus .TemporalOperationResult [ApproveOutput ]:
101+ await self ._get_workflow_handle (client .client , input .user_id ).signal (
82102 GreetingWorkflow .approve , input
83103 )
84- return ApproveOutput ()
104+ return nexus . TemporalOperationResult . sync ( ApproveOutput () )
0 commit comments