11"""
2- This file demonstrates how to implement a Nexus service that is backed by a long-running
3- workflow and exposes operations that perform signals, updates, and queries against that
4- workflow.
2+ This file demonstrates how to implement a Nexus service that is backed by a long-running workflow
3+ and exposes operations that perform updates, and queries against that workflow.
54"""
65
76from __future__ import annotations
87
98import nexusrpc
9+ from temporalio import nexus
1010from temporalio .client import Client , WorkflowHandle
1111from temporalio .common import WorkflowIDConflictPolicy
1212
2121
2222@nexusrpc .handler .service_handler (service = GreetingService )
2323class GreetingServiceHandler :
24- def __init__ (
25- self ,
26- greeting_workflow_handle : WorkflowHandle [GreetingWorkflow , str ],
27- ):
28- self .greeting_workflow_handle = greeting_workflow_handle
24+ # This nexus service is backed by a long-running "entity" workflow. This means that the workflow
25+ # is always running in the background, allowing the service to be stateful and durable. The
26+ # service interacts with it via messages (updates and queries). All of this is implementation
27+ # detail private to the nexus handler: the nexus caller does not know how the operations are
28+ # implemented or what is providing the backing storage.
29+ LONG_RUNNING_WORKFLOW_ID = "nexus-sync-operations-greeting-workflow"
2930
3031 @classmethod
31- async def create (cls , client : Client , task_queue : str ) -> GreetingServiceHandler :
32- # Obtain a workflow handle to the long-running workflow that backs this service, starting
33- # the workflow if it is not already running.
34- return cls (await cls ._get_workflow_handle (client , task_queue ))
35-
36- @staticmethod
37- async def _get_workflow_handle (
38- client : Client , task_queue : str
39- ) -> WorkflowHandle [GreetingWorkflow , str ]:
40- return await client .start_workflow (
32+ async def start (cls , client : Client , task_queue : str ) -> None :
33+ # Start the long-running "entity" workflow, if it is not already running.
34+ await client .start_workflow (
4135 GreetingWorkflow .run ,
42- id = "nexus-sync-operations-greeting-workflow" ,
36+ id = cls . LONG_RUNNING_WORKFLOW_ID ,
4337 task_queue = task_queue ,
4438 id_conflict_policy = WorkflowIDConflictPolicy .USE_EXISTING ,
4539 )
4640
41+ @property
42+ def greeting_workflow_handle (self ) -> WorkflowHandle [GreetingWorkflow , str ]:
43+ # In nexus operation handler code, nexus.client() is always available, returning a client
44+ # connected to the handler namespace (it's the same client instance that your nexus worker
45+ # is using to poll the server for nexus tasks). This client can be used to interact with the
46+ # handler namespace, for example to send signals, queries, or updates. Remember however,
47+ # that a sync_operation handler must return quickly (no more than a few seconds). To do
48+ # long-running work in a nexus operation handler, use
49+ # temporalio.nexus.workflow_run_operation (see the hello_nexus sample).
50+ return nexus .client ().get_workflow_handle_for (
51+ GreetingWorkflow .run , self .LONG_RUNNING_WORKFLOW_ID
52+ )
53+
54+ # 👉 This is a handler for a nexus operation whose internal implementation involves executing a
55+ # query against a long-running workflow that is private to the nexus service.
4756 @nexusrpc .handler .sync_operation
4857 async def get_languages (
4958 self , ctx : nexusrpc .handler .StartOperationContext , input : GetLanguagesInput
@@ -52,12 +61,16 @@ async def get_languages(
5261 GreetingWorkflow .get_languages , input
5362 )
5463
64+ # 👉 This is a handler for a nexus operation whose internal implementation involves executing a
65+ # query against a long-running workflow that is private to the nexus service.
5566 @nexusrpc .handler .sync_operation
5667 async def get_language (
5768 self , ctx : nexusrpc .handler .StartOperationContext , input : None
5869 ) -> Language :
5970 return await self .greeting_workflow_handle .query (GreetingWorkflow .get_language )
6071
72+ # 👉 This is a handler for a nexus operation whose internal implementation involves executing an
73+ # update against a long-running workflow that is private to the nexus service.
6174 @nexusrpc .handler .sync_operation
6275 async def set_language (
6376 self ,
0 commit comments