-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathkitchensink.ts
More file actions
201 lines (177 loc) · 5.24 KB
/
kitchensink.ts
File metadata and controls
201 lines (177 loc) · 5.24 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/**
* Kitchen Sink — Every major task type in a single workflow
*
* Demonstrates: simpleTask, httpTask, inlineTask, jsonJqTask, switchTask,
* forkJoinTask, waitTaskDuration, setVariableTask, terminateTask, subWorkflowTask,
* doWhileTask, and eventTask.
*
* Run:
* CONDUCTOR_SERVER_URL=http://localhost:8080 npx ts-node examples/kitchensink.ts
*/
import {
OrkesClients,
ConductorWorkflow,
TaskHandler,
worker,
simpleTask,
httpTask,
inlineTask,
jsonJqTask,
switchTask,
waitTaskDuration,
setVariableTask,
subWorkflowTask,
doWhileTask,
} from "../src/sdk";
import type { Task } from "../src/open-api";
// ── Workers ─────────────────────────────────────────────────────────
const _simpleWorker = worker({ taskDefName: "ks_simple_worker", registerTaskDef: true })(
async (task: Task) => {
return {
status: "COMPLETED",
outputData: { processed: true, input: task.inputData },
};
}
);
const _branchA = worker({ taskDefName: "ks_branch_a", registerTaskDef: true })(
async (_task: Task) => {
return {
status: "COMPLETED",
outputData: { branch: "A", done: true },
};
}
);
const _branchB = worker({ taskDefName: "ks_branch_b", registerTaskDef: true })(
async (_task: Task) => {
return {
status: "COMPLETED",
outputData: { branch: "B", done: true },
};
}
);
const _loopTask = worker({ taskDefName: "ks_loop_task", registerTaskDef: true })(
async (task: Task) => {
const iteration = (task.inputData?.iteration as number) ?? 0;
return {
status: "COMPLETED",
outputData: { iteration, processed: true },
};
}
);
// ── Workflow ─────────────────────────────────────────────────────────
async function main() {
const clients = await OrkesClients.from();
const workflowClient = clients.getWorkflowClient();
const client = clients.getClient();
// ── Sub-workflow (registered separately) ──
const subWf = new ConductorWorkflow(workflowClient, "ks_sub_workflow")
.add(
simpleTask("sub_simple_ref", "ks_simple_worker", {
message: "${workflow.input.subMessage}",
})
)
.outputParameters({ subResult: "${sub_simple_ref.output.processed}" });
await subWf.register(true);
// ── Main kitchen-sink workflow ──
const wf = new ConductorWorkflow(workflowClient, "kitchen_sink_workflow")
.description("Demonstrates every major task type");
// 1. Simple task
wf.add(
simpleTask("simple_ref", "ks_simple_worker", {
key: "${workflow.input.inputValue}",
})
);
// 2. HTTP task
wf.add(
httpTask("http_ref", {
uri: "https://jsonplaceholder.typicode.com/posts/1",
method: "GET",
})
);
// 3. Inline (JavaScript) task
wf.add(
inlineTask(
"inline_ref",
`(function() {
return { doubled: $.value * 2 };
})()`,
"javascript"
)
);
// 4. JSON JQ Transform task
wf.add(
jsonJqTask("jq_ref", ".http_ref.output.body | { title: .title, id: .id }")
);
// 5. Set variable task
wf.add(
setVariableTask("set_var_ref", {
counter: 0,
status: "in_progress",
})
);
// 6. Switch task (decision)
wf.add(
switchTask(
"switch_ref",
"${workflow.input.route}",
{
fast: [
simpleTask("fast_path_ref", "ks_simple_worker", { path: "fast" }),
],
slow: [
simpleTask("slow_path_ref", "ks_simple_worker", { path: "slow" }),
],
},
[simpleTask("default_path_ref", "ks_simple_worker", { path: "default" })]
)
);
// 7. Fork/Join — parallel branches (using fluent API)
wf.fork([
[simpleTask("branch_a_ref", "ks_branch_a", {})],
[simpleTask("branch_b_ref", "ks_branch_b", {})],
]);
// 8. Do-While loop
wf.add(
doWhileTask(
"loop_ref",
'if ($.loop_task_ref["iteration"] >= 3) { false; } else { true; }',
[
simpleTask("loop_task_ref", "ks_loop_task", {
iteration: "${loop_ref.output.iteration}",
}),
]
)
);
// 9. Wait task
wf.add(waitTaskDuration("wait_ref", "1s"));
// 10. Sub-workflow task
wf.add(subWorkflowTask("sub_wf_ref", "ks_sub_workflow", 1));
// Output
wf.outputParameters({
simpleResult: "${simple_ref.output.processed}",
httpTitle: "${http_ref.output.body.title}",
inlineResult: "${inline_ref.output.result}",
jqResult: "${jq_ref.output.result}",
branchA: "${branch_a_ref.output.branch}",
branchB: "${branch_b_ref.output.branch}",
subResult: "${sub_wf_ref.output.subResult}",
});
await wf.register(true);
console.log("Registered kitchen-sink workflow:", wf.getName());
// Execute
const handler = new TaskHandler({ client, scanForDecorated: true });
await handler.startWorkers();
const run = await wf.execute({
inputValue: 42,
route: "fast",
subMessage: "hello from sub",
});
console.log("Status:", run.status);
console.log("Output:", JSON.stringify(run.output, null, 2));
await handler.stopWorkers();
process.exit(0);
}
main().catch((err) => {
console.error(err);
process.exit(1);
});