-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathdurable_app.py
More file actions
266 lines (215 loc) · 10.1 KB
/
Copy pathdurable_app.py
File metadata and controls
266 lines (215 loc) · 10.1 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
import base64
from functools import wraps
from durabletask.internal.orchestrator_service_pb2 import OrchestratorRequest, OrchestratorResponse
from .metadata import OrchestrationTrigger, ActivityTrigger, EntityTrigger, \
DurableClient
from typing import Callable, Optional
from typing import Union
from azure.functions import FunctionRegister, TriggerApi, BindingApi, AuthLevel
# TODO: Use __init__.py to optimize imports
from durabletask.azurefunctions.client import DurableFunctionsClient
from durabletask.azurefunctions.worker import DurableFunctionsWorker
from durabletask.azurefunctions.internal.azurefunctions_null_stub import AzureFunctionsNullStub
class Blueprint(TriggerApi, BindingApi):
"""Durable Functions (DF) Blueprint container.
It allows functions to be declared via trigger and binding decorators,
but does not automatically index/register these functions.
To register these functions, utilize the `register_functions` method from any
:class:`FunctionRegister` subclass, such as `DFApp`.
"""
def __init__(self,
http_auth_level: Union[AuthLevel, str] = AuthLevel.FUNCTION):
"""Instantiate a Durable Functions app with which to register Functions.
Parameters
----------
http_auth_level: Union[AuthLevel, str]
Authorization level required for Function invocation.
Defaults to AuthLevel.Function.
Returns
-------
DFApp
New instance of a Durable Functions app
"""
super().__init__(auth_level=http_auth_level)
def _configure_orchestrator_callable(self, wrap) -> Callable:
"""Obtain decorator to construct an Orchestrator class from a user-defined Function.
Parameters
----------
wrap: Callable
The next decorator to be applied.
Returns
-------
Callable
The function to construct an Orchestrator class from the user-defined Function,
wrapped by the next decorator in the sequence.
"""
def decorator(orchestrator_func):
# Construct an orchestrator based on the end-user code
# TODO: Move this logic somewhere better
def handle(context) -> str:
context_body = getattr(context, "body", None)
if context_body is None:
context_body = context
orchestration_context = context_body
request = OrchestratorRequest()
request.ParseFromString(base64.b64decode(orchestration_context))
stub = AzureFunctionsNullStub()
worker = DurableFunctionsWorker()
response: Optional[OrchestratorResponse] = None
def stub_complete(stub_response):
nonlocal response
response = stub_response
stub.CompleteOrchestratorTask = stub_complete
execution_started_events = [e for e in [e1 for e1 in request.newEvents] + [e2 for e2 in request.pastEvents] if e.HasField("executionStarted")]
function_name = execution_started_events[-1].executionStarted.name
worker.add_named_orchestrator(function_name, orchestrator_func)
worker._execute_orchestrator(request, stub, None)
if response is None:
raise Exception("Orchestrator execution did not produce a response.")
# The Python worker returns the input as type "json", so double-encoding is necessary
return '"' + base64.b64encode(response.SerializeToString()).decode('utf-8') + '"'
handle.orchestrator_function = orchestrator_func
# invoke next decorator, with the Orchestrator as input
handle.__name__ = orchestrator_func.__name__
return wrap(handle)
return decorator
def _configure_entity_callable(self, wrap) -> Callable:
"""Obtain decorator to construct an Entity class from a user-defined Function.
Parameters
----------
wrap: Callable
The next decorator to be applied.
Returns
-------
Callable
The function to construct an Entity class from the user-defined Function,
wrapped by the next decorator in the sequence.
"""
def decorator(entity_func):
# TODO: Implement entity support - similar to orchestrators (?)
raise NotImplementedError()
return decorator
def _add_rich_client(self, fb, parameter_name,
client_constructor):
# Obtain user-code and force type annotation on the client-binding parameter to be `str`.
# This ensures a passing type-check of that specific parameter,
# circumventing a limitation of the worker in type-checking rich DF Client objects.
# TODO: Once rich-binding type checking is possible, remove the annotation change.
user_code = fb._function._func
user_code.__annotations__[parameter_name] = str
# `wraps` This ensures we re-export the same method-signature as the decorated method
@wraps(user_code)
async def df_client_middleware(*args, **kwargs):
# Obtain JSON-string currently passed as DF Client,
# construct rich object from it,
# and assign parameter to that rich object
starter = kwargs[parameter_name]
client = client_constructor(starter)
kwargs[parameter_name] = client
# Invoke user code with rich DF Client binding
return await user_code(*args, **kwargs)
# TODO: Is there a better way to support retrieving the unwrapped user code?
df_client_middleware.client_function = fb._function._func # type: ignore
user_code_with_rich_client = df_client_middleware
fb._function._func = user_code_with_rich_client
def orchestration_trigger(self, context_name: str,
orchestration: Optional[str] = None):
"""Register an Orchestrator Function.
Parameters
----------
context_name: str
Parameter name of the DurableOrchestrationContext object.
orchestration: Optional[str]
Name of Orchestrator Function.
The value is None by default, in which case the name of the method is used.
"""
@self._configure_orchestrator_callable
@self._configure_function_builder
def wrap(fb):
def decorator():
fb.add_trigger(
trigger=OrchestrationTrigger(name=context_name,
orchestration=orchestration))
return fb
return decorator()
return wrap
def activity_trigger(self, input_name: str,
activity: Optional[str] = None):
"""Register an Activity Function.
Parameters
----------
input_name: str
Parameter name of the Activity input.
activity: Optional[str]
Name of Activity Function.
The value is None by default, in which case the name of the method is used.
"""
@self._configure_function_builder
def wrap(fb):
def decorator():
fb.add_trigger(
trigger=ActivityTrigger(name=input_name,
activity=activity))
return fb
return decorator()
return wrap
def entity_trigger(self, context_name: str,
entity_name: Optional[str] = None):
"""Register an Entity Function.
Parameters
----------
context_name: str
Parameter name of the Entity input.
entity_name: Optional[str]
Name of Entity Function.
The value is None by default, in which case the name of the method is used.
"""
@self._configure_entity_callable
@self._configure_function_builder
def wrap(fb):
def decorator():
fb.add_trigger(
trigger=EntityTrigger(name=context_name,
entity_name=entity_name))
return fb
return decorator()
return wrap
def durable_client_input(self,
client_name: str,
task_hub: Optional[str] = None,
connection_name: Optional[str] = None
):
"""Register a Durable-client Function.
Parameters
----------
client_name: str
Parameter name of durable client.
task_hub: Optional[str]
Used in scenarios where multiple function apps share the same storage account
but need to be isolated from each other. If not specified, the default value
from host.json is used.
This value must match the value used by the target orchestrator functions.
connection_name: Optional[str]
The name of an app setting that contains a storage account connection string.
The storage account represented by this connection string must be the same one
used by the target orchestrator functions. If not specified, the default storage
account connection string for the function app is used.
"""
@self._configure_function_builder
def wrap(fb):
def decorator():
self._add_rich_client(fb, client_name, DurableFunctionsClient)
fb.add_binding(
binding=DurableClient(name=client_name,
task_hub=task_hub,
connection_name=connection_name))
return fb
return decorator()
return wrap
class DFApp(Blueprint, FunctionRegister):
"""Durable Functions (DF) app.
Exports the decorators required to declare and index DF Function-types.
"""
pass