-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathfork-join.ts
More file actions
165 lines (149 loc) · 4.53 KB
/
fork-join.ts
File metadata and controls
165 lines (149 loc) · 4.53 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/**
* Fork/Join — Parallel task execution with join synchronization
*
* Demonstrates:
* - ConductorWorkflow.fork() for parallel branches with auto-join
* - Multiple independent tasks running concurrently
* - Aggregating results after join
*
* Run:
* CONDUCTOR_SERVER_URL=http://localhost:8080 npx ts-node examples/advanced/fork-join.ts
*/
import {
OrkesClients,
ConductorWorkflow,
TaskHandler,
worker,
simpleTask,
inlineTask,
} from "../../src/sdk";
import type { Task } from "../../src/open-api";
// ── Workers for parallel branches ───────────────────────────────────
const _fetchUser = worker({ taskDefName: "fj_fetch_user", registerTaskDef: true })(
async (task: Task) => {
const userId = task.inputData?.userId as string;
return {
status: "COMPLETED",
outputData: { userId, name: "Jane Doe", email: "jane@example.com" },
};
}
);
const _fetchOrders = worker({ taskDefName: "fj_fetch_orders", registerTaskDef: true })(
async (task: Task) => {
const userId = task.inputData?.userId as string;
// Simulate fetching orders
await new Promise((resolve) => setTimeout(resolve, 50));
return {
status: "COMPLETED",
outputData: {
userId,
orders: [
{ id: "ORD-1", total: 99.99 },
{ id: "ORD-2", total: 149.50 },
],
},
};
}
);
const _fetchPreferences = worker({ taskDefName: "fj_fetch_preferences", registerTaskDef: true })(
async (task: Task) => {
const userId = task.inputData?.userId as string;
return {
status: "COMPLETED",
outputData: {
userId,
preferences: { theme: "dark", notifications: true, language: "en" },
},
};
}
);
async function main() {
const clients = await OrkesClients.from();
const workflowClient = clients.getWorkflowClient();
const client = clients.getClient();
// ── Approach 1: Using ConductorWorkflow.fork() ─────────────────────
const wf1 = new ConductorWorkflow(
workflowClient,
"fork_join_helper_example"
).description("Parallel fetch using fork with 3 branches");
wf1.fork([
[
simpleTask("user_ref", "fj_fetch_user", {
userId: "${workflow.input.userId}",
}),
],
[
simpleTask("orders_ref", "fj_fetch_orders", {
userId: "${workflow.input.userId}",
}),
],
[
simpleTask("prefs_ref", "fj_fetch_preferences", {
userId: "${workflow.input.userId}",
}),
],
]);
// Aggregate results
wf1.add(
inlineTask(
"aggregate_ref",
`(function() {
return {
user: $.user_ref.output,
orderCount: $.orders_ref.output.orders.length,
theme: $.prefs_ref.output.preferences.theme
};
})()`,
"javascript"
)
);
wf1.outputParameters({
user: "${user_ref.output}",
orders: "${orders_ref.output.orders}",
preferences: "${prefs_ref.output.preferences}",
summary: "${aggregate_ref.output.result}",
});
await wf1.register(true);
console.log("Registered workflow 1:", wf1.getName());
// ── Approach 2: Using ConductorWorkflow.fork() ────────────────────
const wf2 = new ConductorWorkflow(
workflowClient,
"fork_join_fluent_example"
)
.description("Parallel fetch using fluent fork API")
.fork([
[
simpleTask("user_ref2", "fj_fetch_user", {
userId: "${workflow.input.userId}",
}),
],
[
simpleTask("orders_ref2", "fj_fetch_orders", {
userId: "${workflow.input.userId}",
}),
],
])
.outputParameters({
user: "${user_ref2.output}",
orders: "${orders_ref2.output.orders}",
});
await wf2.register(true);
console.log("Registered workflow 2:", wf2.getName());
// Execute
const handler = new TaskHandler({ client, scanForDecorated: true });
await handler.startWorkers();
console.log("\n--- Executing fork/join with helper ---");
const run1 = await wf1.execute({ userId: "user-123" });
console.log("Status:", run1.status);
console.log("Output:", JSON.stringify(run1.output, null, 2));
console.log("\n--- Executing fork/join with fluent API ---");
const run2 = await wf2.execute({ userId: "user-456" });
console.log("Status:", run2.status);
console.log("Output:", JSON.stringify(run2.output, null, 2));
await handler.stopWorkers();
process.exit(0);
}
main().catch((err) => {
console.error(err);
process.exit(1);
});