Skip to content

Commit ea5c2b0

Browse files
committed
Re-add Orchestrator object/model
1 parent 5df87b1 commit ea5c2b0

3 files changed

Lines changed: 73 additions & 5 deletions

File tree

azure-functions-durable/azure/durable_functions/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@
77

88
from .decorators.durable_app import Blueprint, DFApp
99
from .client import DurableFunctionsClient
10+
from .orchestrator import Orchestrator
1011

1112
# IMPORTANT: DO NOT REMOVE. `azure-functions` relies on the presence and value of this variable
1213
# for version detection
1314
version = "2.x"
1415

15-
__all__ = ["Blueprint", "DFApp", "DurableFunctionsClient", "version"]
16+
__all__ = ["Blueprint", "DFApp", "DurableFunctionsClient", "Orchestrator", "version"]

azure-functions-durable/azure/durable_functions/decorators/durable_app.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from .metadata import OrchestrationTrigger, ActivityTrigger, EntityTrigger, \
1313
DurableClient
1414
from ..worker import DurableFunctionsWorker
15+
from ..orchestrator import Orchestrator
1516

1617

1718
class Blueprint(TriggerApi, BindingApi):
@@ -58,10 +59,7 @@ def _configure_orchestrator_callable(self, wrap) -> Callable:
5859
def decorator(orchestrator_func: task.Orchestrator):
5960
# Construct an orchestrator based on the end-user code
6061

61-
def handle(context) -> str:
62-
return DurableFunctionsWorker()._execute_orchestrator(orchestrator_func, context)
63-
64-
handle.orchestrator_function = orchestrator_func # type: ignore
62+
handle = Orchestrator.create(orchestrator_func)
6563

6664
# invoke next decorator, with the Orchestrator as input
6765
handle.__name__ = orchestrator_func.__name__
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
"""Durable Orchestrator.
2+
3+
Responsible for orchestrating the execution of the user defined generator
4+
function.
5+
"""
6+
from typing import Callable, Any, Generator
7+
8+
import azure.functions as func
9+
10+
from durabletask.task import OrchestrationContext
11+
12+
from .worker import DurableFunctionsWorker
13+
14+
class Orchestrator:
15+
"""Durable Orchestration Class.
16+
17+
Responsible for orchestrating the execution of the user defined generator
18+
function.
19+
"""
20+
21+
def __init__(self,
22+
activity_func: Callable[[OrchestrationContext, Any], Generator[Any, Any, Any]]):
23+
"""Create a new orchestrator for the user defined generator.
24+
25+
Responsible for orchestrating the execution of the user defined
26+
generator function.
27+
:param activity_func: Generator function to orchestrate.
28+
"""
29+
self.fn: Callable[[OrchestrationContext, Any], Generator[Any, Any, Any]] = activity_func
30+
31+
def handle(self, context: OrchestrationContext) -> str:
32+
"""Handle the orchestration of the user defined generator function.
33+
34+
Parameters
35+
----------
36+
context : DurableOrchestrationContext
37+
The DF orchestration context
38+
39+
Returns
40+
-------
41+
str
42+
The JSON-formatted string representing the user's orchestration
43+
state after this invocation
44+
"""
45+
self.durable_context = context
46+
return DurableFunctionsWorker()._execute_orchestrator(self.fn, context)
47+
48+
@classmethod
49+
def create(cls, fn: Callable[[OrchestrationContext, Any], Generator[Any, Any, Any]]) \
50+
-> Callable[[Any], str]:
51+
"""Create an instance of the orchestration class.
52+
53+
Parameters
54+
----------
55+
fn: Callable[[DurableOrchestrationContext], Iterator[Any]]
56+
Generator function that needs orchestration
57+
58+
Returns
59+
-------
60+
Callable[[Any], str]
61+
Handle function of the newly created orchestration client
62+
"""
63+
64+
def handle(context) -> str:
65+
return Orchestrator(fn).handle(context)
66+
67+
handle.orchestrator_function = fn # type: ignore
68+
69+
return handle

0 commit comments

Comments
 (0)