Orchestratorfunktionen können andere Orchestratorfunktionen als untergeordnete Orchestrierungen aufrufen. Eine untergeordnete Orchestrierung wird als untergeordnetes Element des aufrufenden (übergeordneten) Orchestrators ausgeführt und verhält sich wie eine Aktivität aus der Perspektive des Aufrufers: Sie kann einen Wert zurückgeben, Ausnahmen auslösen, die vom übergeordneten Element abgefangen werden, und automatische Wiederholungsversuche unterstützen.
Wann Unter-Orchestrierungen verwendet werden sollten
Verwenden Sie Sub-Orchestrierungen, wenn Sie Folgendes benötigen:
-
Erstellen sie wiederverwendbare Workflowbausteine: Extrahieren Sie einen mehrstufigen Workflow in einen eigenen Orchestrator, damit mehrere übergeordnete Orchestrierungen sie aufrufen können.
-
Paralleles Auffächern von Orchestrierungen: Planen Sie zahlreiche Instanzen desselben Orchestrators gleichzeitig, und warten Sie, bis alle Instanzen abgeschlossen sind.
-
Organisieren komplexer Workflows: Unterteilen Sie eine große Orchestrierung in benannte, testbare Stücke anstelle einer einzelnen langen Funktion.
Hinweis
Sub-Orchestrierungen müssen in derselben App wie die übergeordnete Orchestrierung definiert werden. Verwenden Sie zum Aufrufen von Orchestrierungen in einer anderen App stattdessen das HTTP 202-Pollingmuster. Weitere Informationen finden Sie unter HTTP-Features.
In diesem Artikel:
Definieren einer untergeordneten Orchestrierung
Im folgenden Beispiel wird ein IoT-Szenario veranschaulicht, in dem mehrere Geräte eingerichtet werden müssen. Die Funktion stellt den Setupworkflow dar, der für jedes Gerät ausgeführt wird:
Isoliertes Arbeitsmodell
public static async Task DeviceProvisioningOrchestration(
[OrchestrationTrigger] TaskOrchestrationContext context, string deviceId)
{
// Step 1: Create an installation package in blob storage and return a SAS URL.
Uri sasUrl = await context.CallActivityAsync<Uri>("CreateInstallationPackage", deviceId);
// Step 2: Notify the device that the installation package is ready.
await context.CallActivityAsync("SendPackageUrlToDevice", (deviceId, sasUrl));
// Step 3: Wait for the device to acknowledge that it has downloaded the new package.
await context.WaitForExternalEvent<bool>("DownloadCompletedAck");
// Step 4: ...
}
In-Prozess-Modell
public static async Task DeviceProvisioningOrchestration(
[OrchestrationTrigger] IDurableOrchestrationContext context)
{
string deviceId = context.GetInput<string>();
// Step 1: Create an installation package in blob storage and return a SAS URL.
Uri sasUrl = await context.CallActivityAsync<Uri>("CreateInstallationPackage", deviceId);
// Step 2: Notify the device that the installation package is ready.
await context.CallActivityAsync("SendPackageUrlToDevice", Tuple.Create(deviceId, sasUrl));
// Step 3: Wait for the device to acknowledge that it has downloaded the new package.
await context.WaitForExternalEvent<bool>("DownloadCompletedAck");
// Step 4: ...
}
const df = require("durable-functions");
df.app.orchestration("deviceProvisioningOrchestration", function* (context) {
const deviceId = context.df.getInput();
// Step 1: Create an installation package in blob storage and return a SAS URL.
const sasUrl = yield context.df.callActivity("createInstallationPackage", deviceId);
// Step 2: Notify the device that the installation package is ready.
yield context.df.callActivity("sendPackageUrlToDevice", { id: deviceId, url: sasUrl });
// Step 3: Wait for the device to acknowledge that it has downloaded the new package.
yield context.df.waitForExternalEvent("downloadCompletedAck");
// Step 4: ...
});
import azure.functions as func
import azure.durable_functions as df
def orchestrator_function(context: df.DurableOrchestrationContext):
device_id = context.get_input()
# Step 1: Create an installation package in blob storage and return a SAS URL.
sas_url = yield context.call_activity("CreateInstallationPackage", device_id)
# Step 2: Notify the device that the installation package is ready.
yield context.call_activity("SendPackageUrlToDevice", { "id": device_id, "url": sas_url })
# Step 3: Wait for the device to acknowledge that it has downloaded the new package.
yield context.call_activity("DownloadCompletedAck")
# Step 4: ...
param($Context)
$deviceId = $Context.Input
# Step 1: Create an installation package in blob storage and return a SAS URL.
$sasUrl = Invoke-DurableActivity -FunctionName "CreateInstallationPackage" -Input $deviceId
# Step 2: Notify the device that the installation package is ready.
$deviceInfo = @{
id = $deviceId
url = $sasUrl
}
Invoke-DurableActivity -FunctionName "SendPackageUrlToDevice" -Input $deviceInfo
# Step 3: Wait for the device to acknowledge that it has downloaded the new package.
Start-DurableExternalEventListener -EventName "DownloadCompletedAck"
# Step 4: ...
@FunctionName("DeviceProvisioningOrchestration")
public void deviceProvisioningOrchestration(
@DurableOrchestrationTrigger(name = "ctx") TaskOrchestrationContext ctx) {
// Step 1: Create an installation package in blob storage and return a SAS URL.
String deviceId = ctx.getInput(String.class);
String blobUri = ctx.callActivity("CreateInstallPackage", deviceId, String.class).await();
// Step 2: Notify the device that the installation package is ready.
String[] args = { deviceId, blobUri };
ctx.callActivity("SendPackageUrlToDevice", args).await();
// Step 3: Wait for the device to acknowledge that it has downloaded the new package.
ctx.waitForExternalEvent("DownloadCompletedAck").await();
// Step 4: ...
}
Durable Functions unterstützt Go nicht. Um Go-Beispiele zu sehen, wählen Sie Durable Task SDKs oben in diesem Artikel aus.
using Microsoft.DurableTask;
[DurableTask]
public class DeviceProvisioningOrchestration : TaskOrchestrator<string, object?>
{
public override async Task<object?> RunAsync(TaskOrchestrationContext context, string deviceId)
{
// Step 1: Create an installation package in blob storage and return a SAS URL.
Uri sasUrl = await context.CallActivityAsync<Uri>("CreateInstallationPackage", deviceId);
// Step 2: Notify the device that the installation package is ready.
await context.CallActivityAsync("SendPackageUrlToDevice", (deviceId, sasUrl.ToString()));
// Step 3: Wait for the device to acknowledge that it has downloaded the new package.
await context.WaitForExternalEvent<bool>("DownloadCompletedAck");
// Step 4: ...
return null;
}
}
import { OrchestrationContext, TOrchestrator } from "@microsoft/durabletask-js";
const deviceProvisioningOrchestrator: TOrchestrator = async function* (
ctx: OrchestrationContext,
deviceId: string
): any {
// Step 1: Create an installation package in blob storage and return a SAS URL.
const sasUrl: string = yield ctx.callActivity(createInstallationPackage, deviceId);
// Step 2: Notify the device that the installation package is ready.
yield ctx.callActivity(sendPackageUrlToDevice, { id: deviceId, url: sasUrl });
// Step 3: Wait for the device to acknowledge that it has downloaded the new package.
yield ctx.waitForExternalEvent("DownloadCompletedAck");
// Step 4: ...
};
from durabletask import task
def device_provisioning_orchestrator(ctx: task.OrchestrationContext, device_id: str):
# Step 1: Create an installation package in blob storage and return a SAS URL.
sas_url = yield ctx.call_activity("create_installation_package", input=device_id)
# Step 2: Notify the device that the installation package is ready.
yield ctx.call_activity("send_package_url_to_device", input={"id": device_id, "url": sas_url})
# Step 3: Wait for the device to acknowledge that it has downloaded the new package.
yield ctx.wait_for_external_event("DownloadCompletedAck")
# Step 4: ...
Dieses Beispiel wird für .NET, JavaScript, Java und Python gezeigt.
import com.microsoft.durabletask.TaskOrchestration;
import com.microsoft.durabletask.TaskOrchestrationContext;
public class DeviceProvisioningOrchestration implements TaskOrchestration {
@Override
public void run(TaskOrchestrationContext ctx) {
String deviceId = ctx.getInput(String.class);
// Step 1: Create an installation package in blob storage and return a SAS URL.
String blobUri = ctx.callActivity("CreateInstallPackage", deviceId, String.class).await();
// Step 2: Notify the device that the installation package is ready.
String[] args = { deviceId, blobUri };
ctx.callActivity("SendPackageUrlToDevice", args).await();
// Step 3: Wait for the device to acknowledge that it has downloaded the new package.
ctx.waitForExternalEvent("DownloadCompletedAck").await();
// Step 4: ...
}
}
Diese Orchestratorfunktion kann eigenständig für die einmalige Einrichtung eines Geräts ausgeführt werden, oder ein übergeordneter Orchestrator kann sie mithilfe der call-sub-orchestrator-API als untergeordnete Orchestrierung einplanen.
Paralleles Ausführen untergeordneter Orchestrierungen
Das folgende Beispiel zeigt einen übergeordneten Orchestrator, der mehrere untergeordnete Orchestrierungen parallel auffächert. In einigen Sprachen wird eine deterministische untergeordnete Instanz-ID verwendet (abgeleitet von der Instanz-ID des übergeordneten Elements plus Index), um doppelte untergeordnete Orchestrierungen während der Wiedergabe zu verhindern.
Isoliertes Arbeitsmodell
[Function("ProvisionNewDevices")]
public static async Task ProvisionNewDevices(
[OrchestrationTrigger] TaskOrchestrationContext context)
{
string[] deviceIds = await context.CallActivityAsync<string[]>("GetNewDeviceIds");
// Run multiple device provisioning flows in parallel
var provisioningTasks = new List<Task>();
foreach (string deviceId in deviceIds)
{
Task provisionTask = context.CallSubOrchestratorAsync("DeviceProvisioningOrchestration", deviceId);
provisioningTasks.Add(provisionTask);
}
await Task.WhenAll(provisioningTasks);
// ...
}
In-Prozess-Modell
[FunctionName("ProvisionNewDevices")]
public static async Task ProvisionNewDevices(
[OrchestrationTrigger] IDurableOrchestrationContext context)
{
string[] deviceIds = await context.CallActivityAsync<string[]>("GetNewDeviceIds");
// Run multiple device provisioning flows in parallel
var provisioningTasks = new List<Task>();
foreach (string deviceId in deviceIds)
{
Task provisionTask = context.CallSubOrchestratorAsync("DeviceProvisioningOrchestration", deviceId);
provisioningTasks.Add(provisionTask);
}
await Task.WhenAll(provisioningTasks);
// ...
}
const df = require("durable-functions");
df.app.orchestration("provisionNewDevices", function* (context) {
const deviceIds = yield context.df.callActivity("getNewDeviceIds");
// Run multiple device provisioning flows in parallel
const provisioningTasks = [];
var id = 0;
for (const deviceId of deviceIds) {
const child_id = context.df.instanceId + `:${id}`;
const provisionTask = context.df.callSubOrchestrator(
"deviceProvisioningOrchestration",
deviceId,
child_id
);
provisioningTasks.push(provisionTask);
id++;
}
yield context.df.Task.all(provisioningTasks);
// ...
});
import azure.functions as func
import azure.durable_functions as df
def orchestrator_function(context: df.DurableOrchestrationContext):
device_IDs = yield context.call_activity("GetNewDeviceIds")
# Run multiple device provisioning flows in parallel
provisioning_tasks = []
id_ = 0
for device_id in device_IDs:
child_id = f"{context.instance_id}:{id_}"
provision_task = context.call_sub_orchestrator("DeviceProvisioningOrchestration", device_id, child_id)
provisioning_tasks.append(provision_task)
id_ += 1
yield context.task_all(provisioning_tasks)
# ...
param($Context)
$deviceIds = Invoke-DurableActivity -FunctionName "GetNewDeviceIds"
# Run multiple device setting up flows in parallel
$provisioningTasks = @()
for ($i = 0; $i -lt $deviceIds.Count; $i++) {
$deviceId = $deviceIds[$i]
$childId = "$($Context.InstanceId):$i"
$provisionTask = Invoke-DurableSubOrchestrator `
-FunctionName "DeviceProvisioningOrchestration" `
-Input $deviceId `
-InstanceId $childId `
-NoWait
$provisioningTasks += $provisionTask
}
Wait-DurableTask -Task $provisioningTasks
# ...
@FunctionName("ProvisionNewDevices")
public void provisionNewDevices(
@DurableOrchestrationTrigger(name = "ctx") TaskOrchestrationContext ctx) {
List<?> deviceIDs = ctx.getInput(List.class);
// Schedule each device provisioning sub-orchestration to run in parallel
List<Task<Void>> parallelTasks = deviceIDs.stream()
.map(device -> ctx.callSubOrchestrator("DeviceProvisioningOrchestration", device))
.collect(Collectors.toList());
// ...
}
Durable Functions unterstützt Go nicht. Um Go-Beispiele zu sehen, wählen Sie Durable Task SDKs oben in diesem Artikel aus.
Nächste Schritte
using Microsoft.DurableTask;
[DurableTask]
public class ProvisionNewDevices : TaskOrchestrator<object?, object?>
{
public override async Task<object?> RunAsync(TaskOrchestrationContext context, object? input)
{
string[] deviceIds = await context.CallActivityAsync<string[]>("GetNewDeviceIds");
// Run multiple device provisioning flows in parallel
var provisioningTasks = new List<Task>();
foreach (string deviceId in deviceIds)
{
Task provisionTask = context.CallSubOrchestratorAsync("DeviceProvisioningOrchestration", deviceId);
provisioningTasks.Add(provisionTask);
}
await Task.WhenAll(provisioningTasks);
return null;
}
}
import { OrchestrationContext, TOrchestrator, whenAll } from "@microsoft/durabletask-js";
const provisionNewDevices: TOrchestrator = async function* (ctx: OrchestrationContext): any {
const deviceIds: string[] = yield ctx.callActivity(getNewDeviceIds);
// Run multiple device provisioning flows in parallel
const provisioningTasks = deviceIds.map((deviceId: string) =>
ctx.callSubOrchestrator(deviceProvisioningOrchestrator, deviceId)
);
yield whenAll(provisioningTasks);
};
from durabletask import task
def provision_new_devices(ctx: task.OrchestrationContext, _):
device_ids = yield ctx.call_activity("get_new_device_ids")
# Run multiple device provisioning flows in parallel
provisioning_tasks = []
for device_id in device_ids:
provision_task = ctx.call_sub_orchestrator(device_provisioning_orchestrator, input=device_id)
provisioning_tasks.append(provision_task)
yield task.when_all(provisioning_tasks)
Dieses Beispiel wird für .NET, JavaScript, Java, Python und Go gezeigt.
import com.microsoft.durabletask.TaskOrchestration;
import com.microsoft.durabletask.TaskOrchestrationContext;
public class ProvisionNewDevices implements TaskOrchestration {
@Override
public void run(TaskOrchestrationContext ctx) {
List<?> deviceIDs = ctx.getInput(List.class);
// Schedule each device provisioning sub-orchestration to run in parallel
List<Task<Void>> parallelTasks = deviceIDs.stream()
.map(device -> ctx.callSubOrchestrator("DeviceProvisioningOrchestration", device))
.collect(Collectors.toList());
ctx.allOf(parallelTasks).await();
}
}
Die folgenden eigenständigen Workflow-Funktionen demonstrieren die Parent-/Child-Komposition ohne externe Geräte-Services. Registrieren Sie sie als ParentGreeting und ChildGreeting.
package main
import (
"fmt"
"github.com/microsoft/durabletask-go/task"
)
func childGreeting(ctx *task.OrchestrationContext) (any, error) {
var name string
if err := ctx.GetInput(&name); err != nil {
return nil, err
}
return fmt.Sprintf("Hello %s!", name), nil
}
func parentGreeting(ctx *task.OrchestrationContext) (any, error) {
var names []string
if err := ctx.GetInput(&names); err != nil {
return nil, err
}
pending := make([]task.Task, len(names))
for i, name := range names {
pending[i] = ctx.CallSubOrchestrator(
"ChildGreeting", task.WithSubOrchestratorInput(name))
}
if err := ctx.WhenAll(pending...); err != nil {
return nil, err
}
results := make([]string, len(pending))
for i, child := range pending {
if err := child.Await(&results[i]); err != nil {
return nil, err
}
}
return results, nil
}
Planen Sie alle untergeordneten Prozesse ein, bevor Sie auf ihre parallele Ausführung warten. Die Ergebnisse bleiben in der Eingabereihenfolge. Für Kind-Ergebnisse, Instanz-IDs, Metadaten und Fehler siehe das Go SDK Sub-Orchestrations-Beispiel.
Nächste Schritte