|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +/** |
| 5 | + * This example demonstrates Work Item Filters for Durable Task workers. |
| 6 | + * |
| 7 | + * Work Item Filters allow a worker to tell the sidecar which orchestrations, |
| 8 | + * activities, and entities it is configured to handle. The sidecar then only |
| 9 | + * dispatches matching work items to that worker, enabling efficient routing. |
| 10 | + * |
| 11 | + * Key concepts demonstrated: |
| 12 | + * - Auto-generated filters from the worker's registry (default behavior) |
| 13 | + * - Explicit filters via useWorkItemFilters() |
| 14 | + * |
| 15 | + * This example runs against: |
| 16 | + * DTS Emulator: |
| 17 | + * docker run --name dts-emulator -i -p 8080:8080 -d --rm mcr.microsoft.com/dts/dts-emulator:latest |
| 18 | + * Then: |
| 19 | + * npx ts-node --swc examples/work-item-filters/index.ts |
| 20 | + */ |
| 21 | + |
| 22 | +import { |
| 23 | + ActivityContext, |
| 24 | + OrchestrationContext, |
| 25 | + TOrchestrator, |
| 26 | + WorkItemFilters, |
| 27 | +} from "@microsoft/durabletask-js"; |
| 28 | +import { |
| 29 | + DurableTaskAzureManagedClientBuilder, |
| 30 | + DurableTaskAzureManagedWorkerBuilder, |
| 31 | +} from "@microsoft/durabletask-js-azuremanaged"; |
| 32 | + |
| 33 | +const endpoint = process.env.ENDPOINT || "localhost:8080"; |
| 34 | +const taskHub = process.env.TASKHUB || "default"; |
| 35 | + |
| 36 | +// ============================================================================ |
| 37 | +// Step 1: Define activities and orchestrators |
| 38 | +// ============================================================================ |
| 39 | + |
| 40 | +const greet = async (_: ActivityContext, name: string): Promise<string> => { |
| 41 | + return `Hello, ${name}!`; |
| 42 | +}; |
| 43 | + |
| 44 | +const add = async (_: ActivityContext, input: { a: number; b: number }): Promise<number> => { |
| 45 | + return input.a + input.b; |
| 46 | +}; |
| 47 | + |
| 48 | +const greetingOrchestrator: TOrchestrator = async function* ( |
| 49 | + ctx: OrchestrationContext, |
| 50 | + name: string, |
| 51 | +): Promise<any> { |
| 52 | + const result = yield ctx.callActivity(greet, name); |
| 53 | + return result; |
| 54 | +}; |
| 55 | + |
| 56 | +const mathOrchestrator: TOrchestrator = async function* ( |
| 57 | + ctx: OrchestrationContext, |
| 58 | + input: { a: number; b: number }, |
| 59 | +): Promise<any> { |
| 60 | + const result = yield ctx.callActivity(add, input); |
| 61 | + return result; |
| 62 | +}; |
| 63 | + |
| 64 | +// ============================================================================ |
| 65 | +// Step 2: Demonstrate different work item filter configurations |
| 66 | +// ============================================================================ |
| 67 | + |
| 68 | +async function runWithAutoGeneratedFilters() { |
| 69 | + console.log("\n=== Scenario 1: Auto-Generated Filters (Default) ==="); |
| 70 | + console.log("The worker auto-generates filters from its registered orchestrators and activities."); |
| 71 | + console.log("Only matching work items will be dispatched to this worker.\n"); |
| 72 | + |
| 73 | + const client = new DurableTaskAzureManagedClientBuilder() |
| 74 | + .endpoint(endpoint, taskHub, null) |
| 75 | + .build(); |
| 76 | + |
| 77 | + // No explicit filters — they are auto-generated from addOrchestrator/addActivity |
| 78 | + const worker = new DurableTaskAzureManagedWorkerBuilder() |
| 79 | + .endpoint(endpoint, taskHub, null) |
| 80 | + .addOrchestrator(greetingOrchestrator) |
| 81 | + .addActivity(greet) |
| 82 | + .build(); |
| 83 | + |
| 84 | + await worker.start(); |
| 85 | + console.log("Worker started with auto-generated filters for: greetingOrchestrator, greet"); |
| 86 | + |
| 87 | + const id = await client.scheduleNewOrchestration(greetingOrchestrator, "Auto-Filters"); |
| 88 | + console.log(`Scheduled orchestration: ${id}`); |
| 89 | + |
| 90 | + const state = await client.waitForOrchestrationCompletion(id, undefined, 30); |
| 91 | + console.log(`Result: ${state?.serializedOutput}`); |
| 92 | + |
| 93 | + await worker.stop(); |
| 94 | + await client.stop(); |
| 95 | +} |
| 96 | + |
| 97 | +async function runWithExplicitFilters() { |
| 98 | + console.log("\n=== Scenario 2: Explicit Filters ==="); |
| 99 | + console.log("The worker uses explicitly provided filters instead of auto-generating them."); |
| 100 | + console.log("This is useful when you want fine-grained control over which work items to accept.\n"); |
| 101 | + |
| 102 | + const client = new DurableTaskAzureManagedClientBuilder() |
| 103 | + .endpoint(endpoint, taskHub, null) |
| 104 | + .build(); |
| 105 | + |
| 106 | + // Provide explicit filters — these override auto-generation |
| 107 | + const filters: WorkItemFilters = { |
| 108 | + orchestrations: [{ name: "mathOrchestrator" }], |
| 109 | + activities: [{ name: "add" }], |
| 110 | + }; |
| 111 | + |
| 112 | + const worker = new DurableTaskAzureManagedWorkerBuilder() |
| 113 | + .endpoint(endpoint, taskHub, null) |
| 114 | + .addOrchestrator(mathOrchestrator) |
| 115 | + .addActivity(add) |
| 116 | + .useWorkItemFilters(filters) |
| 117 | + .build(); |
| 118 | + |
| 119 | + await worker.start(); |
| 120 | + console.log("Worker started with explicit filters for: mathOrchestrator, add"); |
| 121 | + |
| 122 | + const id = await client.scheduleNewOrchestration(mathOrchestrator, { a: 17, b: 25 }); |
| 123 | + console.log(`Scheduled orchestration: ${id}`); |
| 124 | + |
| 125 | + const state = await client.waitForOrchestrationCompletion(id, undefined, 30); |
| 126 | + console.log(`Result: ${state?.serializedOutput}`); |
| 127 | + |
| 128 | + await worker.stop(); |
| 129 | + await client.stop(); |
| 130 | +} |
| 131 | + |
| 132 | +// ============================================================================ |
| 133 | +// Step 3: Run all scenarios |
| 134 | +// ============================================================================ |
| 135 | + |
| 136 | +(async () => { |
| 137 | + console.log(`Connecting to DTS emulator at ${endpoint}, taskHub: ${taskHub}`); |
| 138 | + |
| 139 | + try { |
| 140 | + await runWithAutoGeneratedFilters(); |
| 141 | + await runWithExplicitFilters(); |
| 142 | + |
| 143 | + console.log("\n=== All scenarios completed successfully! ==="); |
| 144 | + } catch (error) { |
| 145 | + console.error("Error:", error); |
| 146 | + process.exit(1); |
| 147 | + } |
| 148 | + |
| 149 | + process.exit(0); |
| 150 | +})(); |
0 commit comments