-
Notifications
You must be signed in to change notification settings - Fork 64
Expand file tree
/
Copy pathorchestrator.py
More file actions
99 lines (78 loc) · 3.33 KB
/
Copy pathorchestrator.py
File metadata and controls
99 lines (78 loc) · 3.33 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
"""Durable Orchestrator.
Responsible for orchestrating the execution of the user defined generator
function.
"""
from azure.durable_functions.models.TaskOrchestrationExecutor import TaskOrchestrationExecutor
from typing import Callable, Any, Generator
from .models import DurableOrchestrationContext
import azure.functions as func
class OrchestrationHandler(Callable):
"""Durable Orchestration Handler.
A callable class that wraps the user defined generator function for execution
by the Python worker and also allows access to the original method for unit testing
"""
def __init__(self, func: Callable[[DurableOrchestrationContext], Generator[Any, Any, Any]]):
"""
Create a new orchestrator handler for the user defined orchestrator function.
Parameters
----------
func: Callable[[DurableOrchestrationContext], Generator[Any, Any, Any]]
The user defined orchestrator function.
"""
self.orchestrator_function = func
def __call__(self, context: func.OrchestrationContext) -> str:
"""
Handle the execution of the user defined orchestrator function.
Parameters
----------
context : func.OrchestrationContext
The DF orchestration context
"""
context_body = getattr(context, "body", None)
if context_body is None:
context_body = context
return Orchestrator(self.orchestrator_function).handle(
DurableOrchestrationContext.from_json(context_body)
)
class Orchestrator:
"""Durable Orchestration Class.
Responsible for orchestrating the execution of the user defined generator
function.
"""
def __init__(self,
activity_func: Callable[[DurableOrchestrationContext], Generator[Any, Any, Any]]):
"""Create a new orchestrator for the user defined generator.
Responsible for orchestrating the execution of the user defined
generator function.
:param activity_func: Generator function to orchestrate.
"""
self.fn: Callable[[DurableOrchestrationContext], Generator[Any, Any, Any]] = activity_func
self.task_orchestration_executor = TaskOrchestrationExecutor()
def handle(self, context: DurableOrchestrationContext) -> str:
"""Handle the orchestration of the user defined generator function.
Parameters
----------
context : DurableOrchestrationContext
The DF orchestration context
Returns
-------
str
The JSON-formatted string representing the user's orchestration
state after this invocation
"""
self.durable_context = context
return self.task_orchestration_executor.execute(context, context.histories, self.fn)
@classmethod
def create(cls, fn: Callable[[DurableOrchestrationContext], Generator[Any, Any, Any]]) \
-> Callable[[Any], str]:
"""Create an instance of the orchestration class.
Parameters
----------
fn: Callable[[DurableOrchestrationContext], Iterator[Any]]
Generator function that needs orchestration
Returns
-------
OrchestrationHandler
Orchestration handler callable class for the newly created orchestration client
"""
return OrchestrationHandler(fn)