-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathclient.mjs
More file actions
87 lines (69 loc) · 3.09 KB
/
client.mjs
File metadata and controls
87 lines (69 loc) · 3.09 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
import { OrchestrationStatus } from "@microsoft/durabletask-js";
import { createAzureManagedClient } from "@microsoft/durabletask-js-azuremanaged";
import { DefaultAzureCredential, ManagedIdentityCredential } from "@azure/identity";
const EMULATOR_ENDPOINT = "http://localhost:8080";
const endpoint = process.env.ENDPOINT ?? EMULATOR_ENDPOINT;
const taskHub = process.env.TASKHUB ?? "default";
const managedIdentityClientId = process.env.AZURE_MANAGED_IDENTITY_CLIENT_ID;
const TOTAL_ORCHESTRATIONS = Number(process.env.TOTAL_ORCHESTRATIONS ?? 20);
const INTERVAL_SECONDS = Number(process.env.ORCHESTRATION_INTERVAL ?? 5);
function createClient() {
if (endpoint === EMULATOR_ENDPOINT) {
const connectionString = `Endpoint=${endpoint};Authentication=None;TaskHub=${taskHub}`;
console.log("Using local emulator with no authentication");
return createAzureManagedClient(connectionString);
}
const credential = managedIdentityClientId
? new ManagedIdentityCredential({ clientId: managedIdentityClientId })
: new DefaultAzureCredential();
if (managedIdentityClientId) {
console.log(`Using managed identity with client ID: ${managedIdentityClientId}`);
} else {
console.log("Using DefaultAzureCredential authentication");
}
return createAzureManagedClient(endpoint, taskHub, credential);
}
function sleep(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
(async () => {
console.log("Starting Function Chaining client...");
console.log(`Endpoint: ${endpoint}`);
console.log(`Task hub: ${taskHub}`);
const baseName = process.argv[2] ?? "User";
const client = createClient();
const orchestrationIds = [];
let completed = 0;
let failed = 0;
try {
for (let index = 0; index < TOTAL_ORCHESTRATIONS; index += 1) {
const orchestrationInput = `${baseName}_${index + 1}`;
console.log(`Scheduling orchestration ${index + 1}/${TOTAL_ORCHESTRATIONS}: ${orchestrationInput}`);
const instanceId = await client.scheduleNewOrchestration("functionChainingOrchestrator", orchestrationInput);
orchestrationIds.push(instanceId);
console.log(`Scheduled orchestration ID: ${instanceId}`);
if (index < TOTAL_ORCHESTRATIONS - 1) {
await sleep(INTERVAL_SECONDS * 1000);
}
}
console.log(`All ${orchestrationIds.length} orchestrations scheduled. Waiting for completion...`);
for (const instanceId of orchestrationIds) {
const state = await client.waitForOrchestrationCompletion(instanceId, true, 120);
if (!state) {
console.log(`No orchestration state returned for ${instanceId}`);
failed += 1;
continue;
}
if (state.runtimeStatus === OrchestrationStatus.COMPLETED) {
completed += 1;
console.log(`Completed ${instanceId} -> ${state.serializedOutput}`);
} else {
failed += 1;
console.log(`Orchestration ${instanceId} finished with status: ${state.runtimeStatus}`);
}
}
console.log(`FINAL RESULTS: ${completed} completed, ${failed} failed, ${orchestrationIds.length} total orchestrations`);
} finally {
await client.stop();
}
})();