-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathclient.mjs
More file actions
88 lines (66 loc) · 2.8 KB
/
client.mjs
File metadata and controls
88 lines (66 loc) · 2.8 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
88
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;
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 getWorkItemCount() {
const input = process.argv[2];
if (!input) {
return 10;
}
const count = Number(input);
if (!Number.isInteger(count) || count <= 0) {
throw new Error("The optional work-item count argument must be a positive integer.");
}
return count;
}
(async () => {
console.log("Starting Fan-out/Fan-in client...");
console.log(`Endpoint: ${endpoint}`);
console.log(`Task hub: ${taskHub}`);
const count = getWorkItemCount();
const workItems = Array.from({ length: count }, (_unused, index) => index + 1);
const client = createClient();
try {
console.log(`Scheduling orchestration with ${count} work items...`);
const instanceId = await client.scheduleNewOrchestration("fanOutFanInOrchestrator", workItems);
console.log(`Started orchestration with ID: ${instanceId}`);
console.log("Waiting for orchestration to complete...");
const state = await client.waitForOrchestrationCompletion(instanceId, true, 120);
if (!state) {
console.log("No orchestration state was returned.");
process.exitCode = 1;
return;
}
if (state.runtimeStatus === OrchestrationStatus.COMPLETED) {
console.log(`Orchestration completed with status: ${state.runtimeStatus}`);
console.log(`Result: ${state.serializedOutput}`);
return;
}
console.log(`Orchestration finished with status: ${state.runtimeStatus}`);
if (state.runtimeStatus === OrchestrationStatus.FAILED && state.failureDetails) {
console.log(`Failure: ${state.failureDetails.errorType} - ${state.failureDetails.message}`);
}
process.exitCode = 1;
} finally {
await client.stop();
}
})();