2
2
from dataclasses import dataclass
3
3
from datetime import timedelta
4
4
from enum import IntEnum
5
- from typing import Optional
5
+ from typing import List , Optional
6
6
7
7
from temporalio import activity , workflow
8
8
from temporalio .exceptions import ApplicationError
@@ -51,14 +51,21 @@ def __init__(self) -> None:
51
51
Language .ENGLISH : "Hello, world" ,
52
52
}
53
53
self .language = Language .ENGLISH
54
+ self .lock = asyncio .Lock () # used by the async handler below
54
55
55
56
@workflow .run
56
57
async def run (self ) -> str :
57
- await workflow .wait_condition (lambda : self .approved_for_release )
58
+ # 👉 In addition to waiting for the `approve` Signal, we also wait for
59
+ # all handlers to finish. Otherwise, the Workflow might return its
60
+ # result while an async set_language_using_activity Update is in
61
+ # progress.
62
+ await workflow .wait_condition (
63
+ lambda : self .approved_for_release and workflow .all_handlers_finished ()
64
+ )
58
65
return self .greetings [self .language ]
59
66
60
67
@workflow .query
61
- def get_languages (self , input : GetLanguagesInput ) -> list [Language ]:
68
+ def get_languages (self , input : GetLanguagesInput ) -> List [Language ]:
62
69
# 👉 A Query handler returns a value: it can inspect but must not mutate the Workflow state.
63
70
if input .include_unsupported :
64
71
return sorted (Language )
@@ -83,42 +90,9 @@ def validate_language(self, language: Language) -> None:
83
90
# 👉 In an Update validator you raise any exception to reject the Update.
84
91
raise ValueError (f"{ language .name } is not supported" )
85
92
86
- @workflow .query
87
- def get_language (self ) -> Language :
88
- return self .language
89
-
90
-
91
- @workflow .defn
92
- class GreetingWorkflowWithAsyncHandler (GreetingWorkflow ):
93
- """
94
- A workflow that that returns a greeting in one of many available languages.
95
-
96
- It supports a Query to obtain the current language, an Update to change the
97
- current language and receive the previous language in response, and a Signal
98
- to approve the Workflow so that it is allowed to return its result.
99
- """
100
-
101
- # 👉 This workflow supports the full range of languages, because the update
102
- # handler is async and uses an activity to call a remote service.
103
-
104
- def __init__ (self ) -> None :
105
- super ().__init__ ()
106
- self .lock = asyncio .Lock ()
107
-
108
- @workflow .run
109
- async def run (self ) -> str :
110
- # 👉 In addition to waiting for the `approve` Signal, we also wait for
111
- # all handlers to finish. Otherwise, the Workflow might return its
112
- # result while a set_language Update is in progress.
113
- await workflow .wait_condition (
114
- lambda : self .approved_for_release and workflow .all_handlers_finished ()
115
- )
116
- return self .greetings [self .language ]
117
-
118
93
@workflow .update
119
- async def set_language (self , language : Language ) -> Language :
120
- # 👉 An Update handler can mutate the Workflow state and return a value.
121
- # 👉 Since this update handler is async, it can execute an activity.
94
+ async def set_language_using_activity (self , language : Language ) -> Language :
95
+ # 👉 This update handler is async, so it can execute an activity.
122
96
if language not in self .greetings :
123
97
# 👉 We use a lock so that, if this handler is executed multiple
124
98
# times, each execution can schedule the activity only when the
@@ -143,9 +117,17 @@ async def set_language(self, language: Language) -> Language:
143
117
previous_language , self .language = self .language , language
144
118
return previous_language
145
119
120
+ @workflow .query
121
+ def get_language (self ) -> Language :
122
+ return self .language
123
+
146
124
147
125
@activity .defn
148
126
async def call_greeting_service (to_language : Language ) -> Optional [str ]:
127
+ """
128
+ An Activity that simulates a call to a remote greeting service.
129
+ The remote greeting service supports the full range of languages.
130
+ """
149
131
greetings = {
150
132
Language .ARABIC : "مرحبا بالعالم" ,
151
133
Language .CHINESE : "你好,世界" ,
0 commit comments