const fanOutWorker = restate.service({
name: "worker",
handlers: {
run: async (ctx: Context, task: Task): Promise<Result> => {
// Split the task in subtasks
const subtasks: SubTask[] = await ctx.run("split task", () => split(task));
// Fan out the subtasks - run them in parallel
const resultPromises = [];
for (const subtask of subtasks) {
const subResultPromise = ctx.serviceClient(fanOutWorker).runSubtask(subtask);
resultPromises.push(subResultPromise);
}
// Fan in - Aggregate the results
const results = await RestatePromise.all(resultPromises);
return aggregate(ctx, results);
},
// Can also run on FaaS
runSubtask: async (ctx: Context, subtask: SubTask): Promise<SubTaskResult> => {
// Processing logic goes here ...
// Can be moved to a separate service to scale independently
return executeSubtask(ctx, subtask);
},
},
});
restate.serve({
services: [fanOutWorker],
port: 9080,
});