-
Notifications
You must be signed in to change notification settings - Fork 20
Add Work Item Filters for Functions sample (DTS backend, .NET and Python) #277
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
bachuv
wants to merge
4
commits into
Azure-Samples:main
Choose a base branch
from
bachuv:vabachu/work-item-filters-functions
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
samples/durable-functions/dotnet/WorkItemFiltering.AppB/AppB.csproj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
| <PropertyGroup> | ||
| <TargetFramework>net8.0</TargetFramework> | ||
| <AzureFunctionsVersion>v4</AzureFunctionsVersion> | ||
| <OutputType>Exe</OutputType> | ||
| <ImplicitUsings>enable</ImplicitUsings> | ||
| <Nullable>enable</Nullable> | ||
| <RootNamespace>WorkItemFiltering.AppB</RootNamespace> | ||
| <AssemblyName>WorkItemFiltering.AppB</AssemblyName> | ||
| </PropertyGroup> | ||
| <ItemGroup> | ||
| <PackageReference Include="Microsoft.Azure.Functions.Worker" Version="2.51.0" /> | ||
| <PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.DurableTask" Version="1.16.5" /> | ||
| <PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.DurableTask.AzureManaged" Version="1.8.1" /> | ||
| <PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http" Version="3.3.0" /> | ||
| <PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="2.0.7" OutputItemType="Analyzer" /> | ||
| </ItemGroup> | ||
| </Project> |
72 changes: 72 additions & 0 deletions
72
samples/durable-functions/dotnet/WorkItemFiltering.AppB/Functions.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using System.Net; | ||
| using Microsoft.Azure.Functions.Worker; | ||
| using Microsoft.Azure.Functions.Worker.Http; | ||
| using Microsoft.DurableTask; | ||
| using Microsoft.DurableTask.Client; | ||
| using Microsoft.Extensions.Logging; | ||
|
|
||
| namespace WorkItemFiltering.AppB; | ||
|
|
||
| // ============================================================================= | ||
| // App B — registers an entirely DIFFERENT set of functions from App A. | ||
| // Both apps share the same DTS task hub ("default"). Work item filtering ensures | ||
| // each app only receives work items for the functions it has registered. | ||
| // | ||
| // App A owns: GreetingOrchestration, FanOutOrchestration, ParentOrchestration, | ||
| // CounterOrchestration, SayHello activity, CounterEntity | ||
| // App B owns: OrdersOrchestration, ShipOrder activity | ||
| // | ||
| // Either app's client endpoint can SCHEDULE any orchestration name. The | ||
| // scheduler routes the work item to the app whose filter matches. | ||
| // ============================================================================= | ||
|
|
||
| public static class OrdersOrchestration | ||
| { | ||
| [Function(nameof(OrdersOrchestration))] | ||
| public static async Task<string> Run([OrchestrationTrigger] TaskOrchestrationContext ctx) | ||
| { | ||
| var logger = ctx.CreateReplaySafeLogger(nameof(OrdersOrchestration)); | ||
| logger.LogInformation("OrdersOrchestration started on App B"); | ||
|
|
||
| string orderId = ctx.GetInput<string>() ?? $"order-{ctx.NewGuid():N}"; | ||
| string shipResult = await ctx.CallActivityAsync<string>(nameof(ShipOrder), orderId); | ||
| return shipResult; | ||
| } | ||
|
|
||
| [Function(nameof(OrdersOrchestration) + "_Start")] | ||
| public static async Task<HttpResponseData> Start( | ||
| [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "orchestrators/orders")] HttpRequestData req, | ||
| [DurableClient] DurableTaskClient client) | ||
| { | ||
| string instanceId = await client.ScheduleNewOrchestrationInstanceAsync( | ||
| nameof(OrdersOrchestration), input: "order-42"); | ||
| return client.CreateCheckStatusResponse(req, instanceId); | ||
| } | ||
| } | ||
|
|
||
| public static class ShipOrder | ||
| { | ||
| [Function(nameof(ShipOrder))] | ||
| public static string Run([ActivityTrigger] string orderId, FunctionContext ctx) | ||
| { | ||
| ctx.GetLogger(nameof(ShipOrder)).LogInformation("App B shipping {OrderId}", orderId); | ||
| return $"Shipped {orderId} from App B"; | ||
| } | ||
| } | ||
|
|
||
| // Generic starter so you can schedule ANY orchestration name from App B's port too. | ||
| public static class GenericStarter | ||
| { | ||
| [Function("AppB_StartOrchestration")] | ||
| public static async Task<HttpResponseData> Start( | ||
| [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "start/{name}")] HttpRequestData req, | ||
| [DurableClient] DurableTaskClient client, | ||
| string name) | ||
| { | ||
| string instanceId = await client.ScheduleNewOrchestrationInstanceAsync(name); | ||
| return client.CreateCheckStatusResponse(req, instanceId); | ||
| } | ||
| } |
5 changes: 5 additions & 0 deletions
5
samples/durable-functions/dotnet/WorkItemFiltering.AppB/Program.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| using Microsoft.Azure.Functions.Worker.Builder; | ||
| using Microsoft.Extensions.Hosting; | ||
|
|
||
| FunctionsApplicationBuilder builder = FunctionsApplication.CreateBuilder(args); | ||
| builder.Build().Run(); |
22 changes: 22 additions & 0 deletions
22
samples/durable-functions/dotnet/WorkItemFiltering.AppB/host.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| { | ||
| "version": "2.0", | ||
| "logging": { | ||
| "logLevel": { | ||
| "default": "Information", | ||
| "DurableTask.AzureStorage": "Warning", | ||
| "DurableTask.Core": "Warning", | ||
| "Microsoft.DurableTask": "Information", | ||
| "Host.Triggers.DurableTask": "Information" | ||
| } | ||
| }, | ||
| "extensions": { | ||
| "durableTask": { | ||
| "hubName": "default", | ||
| "storageProvider": { | ||
| "type": "azureManaged", | ||
| "connectionStringName": "DURABLE_TASK_SCHEDULER_CONNECTION_STRING", | ||
| "workItemFilteringEnabled": true | ||
| } | ||
| } | ||
| } | ||
| } |
161 changes: 161 additions & 0 deletions
161
samples/durable-functions/dotnet/WorkItemFiltering/Functions.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,161 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using Microsoft.Azure.Functions.Worker; | ||
| using Microsoft.Azure.Functions.Worker.Http; | ||
| using Microsoft.DurableTask; | ||
| using Microsoft.DurableTask.Client; | ||
| using Microsoft.DurableTask.Entities; | ||
| using Microsoft.Extensions.Logging; | ||
|
|
||
| namespace WorkItemFiltering; | ||
|
|
||
| // ============================================================================= | ||
| // Orchestrations | ||
| // ============================================================================= | ||
|
|
||
| /// <summary> | ||
| /// A simple orchestration that calls an activity and returns the result. | ||
| /// With work item filtering enabled, DTS will only dispatch this orchestration | ||
| /// to workers that have it registered. | ||
| /// </summary> | ||
| public static class GreetingOrchestration | ||
| { | ||
| [Function(nameof(GreetingOrchestration))] | ||
| public static async Task<string> Run([OrchestrationTrigger] TaskOrchestrationContext ctx) | ||
| { | ||
| ctx.CreateReplaySafeLogger(nameof(GreetingOrchestration)).LogInformation("GreetingOrchestration started"); | ||
| return await ctx.CallActivityAsync<string>(nameof(SayHello), "World"); | ||
| } | ||
|
|
||
| [Function(nameof(GreetingOrchestration) + "_Start")] | ||
| public static async Task<HttpResponseData> Start( | ||
| [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "orchestrators/greeting")] HttpRequestData req, | ||
| [DurableClient] DurableTaskClient client) | ||
| { | ||
| string instanceId = await client.ScheduleNewOrchestrationInstanceAsync(nameof(GreetingOrchestration)); | ||
| return client.CreateCheckStatusResponse(req, instanceId); | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// A fan-out/fan-in orchestration that calls the same activity in parallel. | ||
| /// Demonstrates that activity work items are also filtered. | ||
| /// </summary> | ||
| public static class FanOutOrchestration | ||
| { | ||
| [Function(nameof(FanOutOrchestration))] | ||
| public static async Task<string[]> Run([OrchestrationTrigger] TaskOrchestrationContext ctx) | ||
| { | ||
| ctx.CreateReplaySafeLogger(nameof(FanOutOrchestration)).LogInformation("FanOutOrchestration: fanning out to 3 activities"); | ||
| return await Task.WhenAll( | ||
| ctx.CallActivityAsync<string>(nameof(SayHello), "Tokyo"), | ||
| ctx.CallActivityAsync<string>(nameof(SayHello), "London"), | ||
| ctx.CallActivityAsync<string>(nameof(SayHello), "Seattle")); | ||
| } | ||
|
|
||
| [Function(nameof(FanOutOrchestration) + "_Start")] | ||
| public static async Task<HttpResponseData> Start( | ||
| [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "orchestrators/fanout")] HttpRequestData req, | ||
| [DurableClient] DurableTaskClient client) | ||
| { | ||
| string instanceId = await client.ScheduleNewOrchestrationInstanceAsync(nameof(FanOutOrchestration)); | ||
| return client.CreateCheckStatusResponse(req, instanceId); | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// A parent orchestration that calls a child orchestration. | ||
| /// Sub-orchestration dispatch is also governed by work item filters. | ||
| /// </summary> | ||
| public static class ParentOrchestration | ||
| { | ||
| [Function(nameof(ParentOrchestration))] | ||
| public static async Task<string> Run([OrchestrationTrigger] TaskOrchestrationContext ctx) | ||
| { | ||
| ctx.CreateReplaySafeLogger(nameof(ParentOrchestration)).LogInformation("Calling sub-orchestration"); | ||
| string result = await ctx.CallSubOrchestratorAsync<string>(nameof(GreetingOrchestration)); | ||
| return $"Parent received: {result}"; | ||
| } | ||
|
|
||
| [Function(nameof(ParentOrchestration) + "_Start")] | ||
| public static async Task<HttpResponseData> Start( | ||
| [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "orchestrators/parent")] HttpRequestData req, | ||
| [DurableClient] DurableTaskClient client) | ||
| { | ||
| string instanceId = await client.ScheduleNewOrchestrationInstanceAsync(nameof(ParentOrchestration)); | ||
| return client.CreateCheckStatusResponse(req, instanceId); | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// An orchestration that interacts with a durable entity. | ||
| /// Entity work items are also filtered. | ||
| /// </summary> | ||
| public static class CounterOrchestration | ||
| { | ||
| [Function(nameof(CounterOrchestration))] | ||
| public static async Task<int> Run([OrchestrationTrigger] TaskOrchestrationContext ctx) | ||
| { | ||
| var logger = ctx.CreateReplaySafeLogger(nameof(CounterOrchestration)); | ||
| var entityId = new EntityInstanceId(nameof(CounterEntity), "sample-counter"); | ||
|
|
||
| await ctx.Entities.CallEntityAsync(entityId, "Add", 10); | ||
| await ctx.Entities.CallEntityAsync(entityId, "Add", 20); | ||
| int value = await ctx.Entities.CallEntityAsync<int>(entityId, "Get"); | ||
|
|
||
| logger.LogInformation("Counter value = {Value}", value); | ||
| return value; | ||
| } | ||
|
|
||
| [Function(nameof(CounterOrchestration) + "_Start")] | ||
| public static async Task<HttpResponseData> Start( | ||
| [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "orchestrators/counter")] HttpRequestData req, | ||
| [DurableClient] DurableTaskClient client) | ||
| { | ||
| string instanceId = await client.ScheduleNewOrchestrationInstanceAsync(nameof(CounterOrchestration)); | ||
| return client.CreateCheckStatusResponse(req, instanceId); | ||
| } | ||
| } | ||
|
|
||
| // ============================================================================= | ||
| // Activities | ||
| // ============================================================================= | ||
|
|
||
| public static class SayHello | ||
| { | ||
| [Function(nameof(SayHello))] | ||
| public static string Run([ActivityTrigger] string name) => $"Hello, {name}!"; | ||
| } | ||
|
|
||
| // ============================================================================= | ||
| // Entities | ||
| // ============================================================================= | ||
|
|
||
| public class CounterEntity : TaskEntity<int> | ||
| { | ||
| public void Add(int amount) => this.State += amount; | ||
| public void Reset() => this.State = 0; | ||
| public int Get() => this.State; | ||
|
|
||
| [Function(nameof(CounterEntity))] | ||
| public static Task Dispatch([EntityTrigger] TaskEntityDispatcher dispatcher) | ||
| => dispatcher.DispatchAsync<CounterEntity>(); | ||
| } | ||
|
|
||
| // ============================================================================= | ||
| // Generic starter (for cross-app filter isolation tests) | ||
| // ============================================================================= | ||
|
|
||
| public static class GenericStarter | ||
| { | ||
| [Function("StartOrchestration")] | ||
| public static async Task<HttpResponseData> Start( | ||
| [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "start/{name}")] HttpRequestData req, | ||
| [DurableClient] DurableTaskClient client, | ||
| string name) | ||
| { | ||
| string instanceId = await client.ScheduleNewOrchestrationInstanceAsync(name); | ||
| return client.CreateCheckStatusResponse(req, instanceId); | ||
| } | ||
| } |
5 changes: 5 additions & 0 deletions
5
samples/durable-functions/dotnet/WorkItemFiltering/Program.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| using Microsoft.Azure.Functions.Worker.Builder; | ||
| using Microsoft.Extensions.Hosting; | ||
|
|
||
| FunctionsApplicationBuilder builder = FunctionsApplication.CreateBuilder(args); | ||
| builder.Build().Run(); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.