-
Notifications
You must be signed in to change notification settings - Fork 116
Expand file tree
/
Copy pathworkflows.py
More file actions
73 lines (61 loc) · 2.64 KB
/
Copy pathworkflows.py
File metadata and controls
73 lines (61 loc) · 2.64 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
"""
A caller workflow that executes Nexus operations. The caller does not have information
about how these operations are implemented by the Nexus service.
"""
from temporalio import workflow
from temporalio.exceptions import ApplicationError
from nexus_messaging.callerpattern.service import (
ApproveInput,
GetLanguageInput,
GetLanguagesInput,
Language,
NexusGreetingService,
SetLanguageInput,
)
NEXUS_ENDPOINT = "nexus-messaging-nexus-endpoint"
@workflow.defn
class CallerWorkflow:
@workflow.run
async def run(self, user_id: str) -> list[str]:
log: list[str] = []
nexus_client = workflow.create_nexus_client(
service=NexusGreetingService,
endpoint=NEXUS_ENDPOINT,
)
# Call a Nexus operation backed by a query against the entity workflow.
# The workflow must already be running on the handler, otherwise you will
# get an error saying the workflow has already terminated.
languages_output = await nexus_client.execute_operation(
NexusGreetingService.get_languages,
GetLanguagesInput(include_unsupported=False, user_id=user_id),
)
log.append(f"Supported languages: {languages_output.languages}")
workflow.logger.info("Supported languages: %s", languages_output.languages)
# Following are examples for each of the three messaging types -
# update, query, then signal.
# Call a Nexus operation backed by an update against the entity workflow.
previous_language = await nexus_client.execute_operation(
NexusGreetingService.set_language,
SetLanguageInput(language=Language.ARABIC, user_id=user_id),
)
# Call a Nexus operation backed by a query to confirm the language change.
current_language = await nexus_client.execute_operation(
NexusGreetingService.get_language,
GetLanguageInput(user_id=user_id),
)
if current_language != Language.ARABIC:
raise ApplicationError(f"Expected language ARABIC, got {current_language}")
log.append(
f"Language changed: {previous_language.name} -> {Language.ARABIC.name}"
)
workflow.logger.info(
"Language changed from %s to %s", previous_language, Language.ARABIC
)
# Call a Nexus operation backed by a signal against the entity workflow.
await nexus_client.execute_operation(
NexusGreetingService.approve,
ApproveInput(name="caller", user_id=user_id),
)
log.append("Workflow approved")
workflow.logger.info("Workflow approved")
return log