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