|
| 1 | +""" |
| 2 | +Nexus operation handler implementation for the entity pattern. Each operation receives a |
| 3 | +user_id, which is mapped to a workflow ID. The operations are synchronous because queries |
| 4 | +and updates against a running workflow complete quickly. |
| 5 | +""" |
| 6 | + |
| 7 | +from __future__ import annotations |
| 8 | + |
| 9 | +import nexusrpc |
| 10 | +from temporalio import nexus |
| 11 | +from temporalio.client import WorkflowHandle |
| 12 | + |
| 13 | +from nexus_messaging.callerpattern.handler.workflows import GreetingWorkflow |
| 14 | +from nexus_messaging.callerpattern.service import ( |
| 15 | + ApproveInput, |
| 16 | + ApproveOutput, |
| 17 | + GetLanguageInput, |
| 18 | + GetLanguagesInput, |
| 19 | + GetLanguagesOutput, |
| 20 | + Language, |
| 21 | + NexusGreetingService, |
| 22 | + SetLanguageInput, |
| 23 | +) |
| 24 | + |
| 25 | +WORKFLOW_ID_PREFIX = "GreetingWorkflow_for_" |
| 26 | + |
| 27 | + |
| 28 | +def get_workflow_id(user_id: str) -> str: |
| 29 | + """Map a user ID to a workflow ID. |
| 30 | +
|
| 31 | + This example assumes you might have multiple workflows, one for each user. |
| 32 | + If you had a single workflow for all users, you could remove this function, |
| 33 | + remove the user_id from each input, and just use a single workflow ID. |
| 34 | + """ |
| 35 | + return f"{WORKFLOW_ID_PREFIX}{user_id}" |
| 36 | + |
| 37 | + |
| 38 | +@nexusrpc.handler.service_handler(service=NexusGreetingService) |
| 39 | +class NexusGreetingServiceHandler: |
| 40 | + def _get_workflow_handle( |
| 41 | + self, user_id: str |
| 42 | + ) -> WorkflowHandle[GreetingWorkflow, str]: |
| 43 | + return nexus.client().get_workflow_handle_for( |
| 44 | + GreetingWorkflow.run, get_workflow_id(user_id) |
| 45 | + ) |
| 46 | + |
| 47 | + @nexusrpc.handler.sync_operation |
| 48 | + async def get_languages( |
| 49 | + self, ctx: nexusrpc.handler.StartOperationContext, input: GetLanguagesInput |
| 50 | + ) -> GetLanguagesOutput: |
| 51 | + return await self._get_workflow_handle(input.user_id).query( |
| 52 | + GreetingWorkflow.get_languages, input |
| 53 | + ) |
| 54 | + |
| 55 | + @nexusrpc.handler.sync_operation |
| 56 | + async def get_language( |
| 57 | + self, ctx: nexusrpc.handler.StartOperationContext, input: GetLanguageInput |
| 58 | + ) -> Language: |
| 59 | + return await self._get_workflow_handle(input.user_id).query( |
| 60 | + GreetingWorkflow.get_language |
| 61 | + ) |
| 62 | + |
| 63 | + # Routes to set_language_using_activity (not set_language) so that new languages not |
| 64 | + # already in the greetings map can be fetched via an activity. |
| 65 | + @nexusrpc.handler.sync_operation |
| 66 | + async def set_language( |
| 67 | + self, ctx: nexusrpc.handler.StartOperationContext, input: SetLanguageInput |
| 68 | + ) -> Language: |
| 69 | + return await self._get_workflow_handle(input.user_id).execute_update( |
| 70 | + GreetingWorkflow.set_language_using_activity, input |
| 71 | + ) |
| 72 | + |
| 73 | + @nexusrpc.handler.sync_operation |
| 74 | + async def approve( |
| 75 | + self, ctx: nexusrpc.handler.StartOperationContext, input: ApproveInput |
| 76 | + ) -> ApproveOutput: |
| 77 | + await self._get_workflow_handle(input.user_id).signal( |
| 78 | + GreetingWorkflow.approve, input |
| 79 | + ) |
| 80 | + return ApproveOutput() |
0 commit comments