|
| 1 | +# Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +# Licensed under the MIT License. |
| 3 | +from .metadata import OrchestrationTrigger, ActivityTrigger, EntityTrigger,\ |
| 4 | + DurableClient |
| 5 | +from typing import Callable, Optional |
| 6 | +from azure.durable_functions.entity import Entity |
| 7 | +from azure.durable_functions.orchestrator import Orchestrator |
| 8 | +from azure.durable_functions import DurableOrchestrationClient |
| 9 | +from typing import Union |
| 10 | +from azure.functions import FunctionRegister, TriggerApi, BindingApi, AuthLevel |
| 11 | +from functools import wraps |
| 12 | + |
| 13 | + |
| 14 | +class DFApp(FunctionRegister, TriggerApi, BindingApi): |
| 15 | + """Durable Functions (DF) app. |
| 16 | +
|
| 17 | + Exports the decorators required to register DF Function-types. |
| 18 | + """ |
| 19 | + |
| 20 | + def __init__(self, |
| 21 | + http_auth_level: Union[AuthLevel, str] = AuthLevel.FUNCTION): |
| 22 | + """Instantiate a Durable Functions app with which to register Functions. |
| 23 | +
|
| 24 | + Parameters |
| 25 | + ---------- |
| 26 | + http_auth_level: Union[AuthLevel, str] |
| 27 | + Authorization level required for Function invocation. |
| 28 | + Defaults to AuthLevel.Function. |
| 29 | +
|
| 30 | + Returns |
| 31 | + ------- |
| 32 | + DFApp |
| 33 | + New instance of a Durable Functions app |
| 34 | + """ |
| 35 | + super().__init__(auth_level=http_auth_level) |
| 36 | + |
| 37 | + def _configure_entity_callable(self, wrap) -> Callable: |
| 38 | + """Obtain decorator to construct an Entity class from a user-defined Function. |
| 39 | +
|
| 40 | + In the old programming model, this decorator's logic was unavoidable boilerplate |
| 41 | + in user-code. Now, this is handled internally by the framework. |
| 42 | +
|
| 43 | + Parameters |
| 44 | + ---------- |
| 45 | + wrap: Callable |
| 46 | + The next decorator to be applied. |
| 47 | +
|
| 48 | + Returns |
| 49 | + ------- |
| 50 | + Callable |
| 51 | + The function to construct an Entity class from the user-defined Function, |
| 52 | + wrapped by the next decorator in the sequence. |
| 53 | + """ |
| 54 | + def decorator(entity_func): |
| 55 | + # Construct an entity based on the end-user code |
| 56 | + handle = Entity.create(entity_func) |
| 57 | + |
| 58 | + # invoke next decorator, with the Entity as input |
| 59 | + handle.__name__ = entity_func.__name__ |
| 60 | + return wrap(handle) |
| 61 | + |
| 62 | + return decorator |
| 63 | + |
| 64 | + def _configure_orchestrator_callable(self, wrap) -> Callable: |
| 65 | + """Obtain decorator to construct an Orchestrator class from a user-defined Function. |
| 66 | +
|
| 67 | + In the old programming model, this decorator's logic was unavoidable boilerplate |
| 68 | + in user-code. Now, this is handled internally by the framework. |
| 69 | +
|
| 70 | + Parameters |
| 71 | + ---------- |
| 72 | + wrap: Callable |
| 73 | + The next decorator to be applied. |
| 74 | +
|
| 75 | + Returns |
| 76 | + ------- |
| 77 | + Callable |
| 78 | + The function to construct an Orchestrator class from the user-defined Function, |
| 79 | + wrapped by the next decorator in the sequence. |
| 80 | + """ |
| 81 | + def decorator(orchestrator_func): |
| 82 | + # Construct an orchestrator based on the end-user code |
| 83 | + handle = Orchestrator.create(orchestrator_func) |
| 84 | + |
| 85 | + # invoke next decorator, with the Orchestrator as input |
| 86 | + handle.__name__ = orchestrator_func.__name__ |
| 87 | + return wrap(handle) |
| 88 | + |
| 89 | + return decorator |
| 90 | + |
| 91 | + def orchestration_trigger(self, context_name: str, |
| 92 | + orchestration: Optional[str] = None): |
| 93 | + """Register an Orchestrator Function. |
| 94 | +
|
| 95 | + Parameters |
| 96 | + ---------- |
| 97 | + context_name: str |
| 98 | + Parameter name of the DurableOrchestrationContext object. |
| 99 | + orchestration: Optional[str] |
| 100 | + Name of Orchestrator Function. |
| 101 | + The value is None by default, in which case the name of the method is used. |
| 102 | + """ |
| 103 | + @self._configure_orchestrator_callable |
| 104 | + @self._configure_function_builder |
| 105 | + def wrap(fb): |
| 106 | + |
| 107 | + def decorator(): |
| 108 | + fb.add_trigger( |
| 109 | + trigger=OrchestrationTrigger(name=context_name, |
| 110 | + orchestration=orchestration)) |
| 111 | + return fb |
| 112 | + |
| 113 | + return decorator() |
| 114 | + |
| 115 | + return wrap |
| 116 | + |
| 117 | + def activity_trigger(self, input_name: str, |
| 118 | + activity: Optional[str] = None): |
| 119 | + """Register an Activity Function. |
| 120 | +
|
| 121 | + Parameters |
| 122 | + ---------- |
| 123 | + input_name: str |
| 124 | + Parameter name of the Activity input. |
| 125 | + activity: Optional[str] |
| 126 | + Name of Activity Function. |
| 127 | + The value is None by default, in which case the name of the method is used. |
| 128 | + """ |
| 129 | + @self._configure_function_builder |
| 130 | + def wrap(fb): |
| 131 | + def decorator(): |
| 132 | + fb.add_trigger( |
| 133 | + trigger=ActivityTrigger(name=input_name, |
| 134 | + activity=activity)) |
| 135 | + return fb |
| 136 | + |
| 137 | + return decorator() |
| 138 | + |
| 139 | + return wrap |
| 140 | + |
| 141 | + def entity_trigger(self, context_name: str, |
| 142 | + entity_name: Optional[str] = None): |
| 143 | + """Register an Entity Function. |
| 144 | +
|
| 145 | + Parameters |
| 146 | + ---------- |
| 147 | + context_name: str |
| 148 | + Parameter name of the Entity input. |
| 149 | + entity_name: Optional[str] |
| 150 | + Name of Entity Function. |
| 151 | + The value is None by default, in which case the name of the method is used. |
| 152 | + """ |
| 153 | + @self._configure_entity_callable |
| 154 | + @self._configure_function_builder |
| 155 | + def wrap(fb): |
| 156 | + def decorator(): |
| 157 | + fb.add_trigger( |
| 158 | + trigger=EntityTrigger(name=context_name, |
| 159 | + entity_name=entity_name)) |
| 160 | + return fb |
| 161 | + |
| 162 | + return decorator() |
| 163 | + |
| 164 | + return wrap |
| 165 | + |
| 166 | + def _add_rich_client(self, fb, parameter_name, |
| 167 | + client_constructor): |
| 168 | + # Obtain user-code and force type annotation on the client-binding parameter to be `str`. |
| 169 | + # This ensures a passing type-check of that specific parameter, |
| 170 | + # circumventing a limitation of the worker in type-checking rich DF Client objects. |
| 171 | + # TODO: Once rich-binding type checking is possible, remove the annotation change. |
| 172 | + user_code = fb._function._func |
| 173 | + user_code.__annotations__[parameter_name] = str |
| 174 | + |
| 175 | + # `wraps` This ensures we re-export the same method-signature as the decorated method |
| 176 | + @wraps(user_code) |
| 177 | + async def df_client_middleware(*args, **kwargs): |
| 178 | + |
| 179 | + # Obtain JSON-string currently passed as DF Client, |
| 180 | + # construct rich object from it, |
| 181 | + # and assign parameter to that rich object |
| 182 | + starter = kwargs[parameter_name] |
| 183 | + client = client_constructor(starter) |
| 184 | + kwargs[parameter_name] = client |
| 185 | + |
| 186 | + # Invoke user code with rich DF Client binding |
| 187 | + return await user_code(*args, **kwargs) |
| 188 | + |
| 189 | + user_code_with_rich_client = df_client_middleware |
| 190 | + fb._function._func = user_code_with_rich_client |
| 191 | + |
| 192 | + def durable_client_input(self, |
| 193 | + client_name: str, |
| 194 | + task_hub: Optional[str] = None, |
| 195 | + connection_name: Optional[str] = None |
| 196 | + ): |
| 197 | + """Register a Durable-client Function. |
| 198 | +
|
| 199 | + Parameters |
| 200 | + ---------- |
| 201 | + client_name: str |
| 202 | + Parameter name of durable client. |
| 203 | + task_hub: Optional[str] |
| 204 | + Used in scenarios where multiple function apps share the same storage account |
| 205 | + but need to be isolated from each other. If not specified, the default value |
| 206 | + from host.json is used. |
| 207 | + This value must match the value used by the target orchestrator functions. |
| 208 | + connection_name: Optional[str] |
| 209 | + The name of an app setting that contains a storage account connection string. |
| 210 | + The storage account represented by this connection string must be the same one |
| 211 | + used by the target orchestrator functions. If not specified, the default storage |
| 212 | + account connection string for the function app is used. |
| 213 | + """ |
| 214 | + |
| 215 | + @self._configure_function_builder |
| 216 | + def wrap(fb): |
| 217 | + def decorator(): |
| 218 | + self._add_rich_client(fb, client_name, DurableOrchestrationClient) |
| 219 | + |
| 220 | + fb.add_binding( |
| 221 | + binding=DurableClient(name=client_name, |
| 222 | + task_hub=task_hub, |
| 223 | + connection_name=connection_name)) |
| 224 | + return fb |
| 225 | + |
| 226 | + return decorator() |
| 227 | + |
| 228 | + return wrap |
0 commit comments