mirror of
https://github.com/vercel/workflow.git
synced 2026-09-14 19:59:43 +08:00
4ca9a3edbd
build durable, resilient, and observable workflows. Co-authored-by: Nathan Rajlich <n@n8.io> Co-authored-by: Pranay Prakash <pranay.gp@gmail.com> Co-authored-by: Adrian <me@adriandlam.com> Co-authored-by: JJ Kasper <jj@jjsweb.site> Co-authored-by: Vercel Release Bot <88769842+vercel-release-bot@users.noreply.github.com> Co-authored-by: Peter Wielander <mittgfu@gmail.com> Co-authored-by: Hayden Bleasel <hello@haydenbleasel.com> Co-authored-by: Gal Schlezinger <gal@spitfire.co.il> Co-authored-by: Manuel Muñoz Solera <mamuso@mamuso.net> Co-authored-by: Garrett <garrett.tolbert@vercel.com> Co-authored-by: Lars Grammel <lars.grammel@gmail.com> Co-authored-by: Pooya Parsa <pyapar@gmail.com> Co-authored-by: Tom Dale <tom@tomdale.net> Co-authored-by: Vishal Yathish <135551666+visyat@users.noreply.github.com> Co-authored-by: josh <144584931+dancer@users.noreply.github.com>
79 lines
2.2 KiB
TypeScript
79 lines
2.2 KiB
TypeScript
import chunk from 'lodash.chunk';
|
|
|
|
const ARRAY_LENGTH = 250;
|
|
const CHUNK_SIZE = 50;
|
|
|
|
/**
|
|
* Pattern 1: Each item in a batch gets processed in a step function
|
|
*
|
|
* If a step fails, doesn't fail the entire batch.
|
|
*/
|
|
export async function batchOverSteps() {
|
|
'use workflow';
|
|
|
|
console.log('Workflow started');
|
|
const arr = Array.from({ length: ARRAY_LENGTH }, (_, i) => i + 1);
|
|
const chunkSize = CHUNK_SIZE;
|
|
console.log(
|
|
`Chunking array with size: ${arr.length} and chunk size: ${chunkSize}`
|
|
);
|
|
const chunks = chunk(arr, chunkSize); // Create the batches
|
|
console.log(
|
|
`Created ${chunks.length} chunks (${chunks[0].length} items each)`
|
|
);
|
|
|
|
console.log('Starting batch processing');
|
|
for (const [index, batch] of chunks.entries()) {
|
|
console.log(`Batch ${index + 1}/${chunks.length}`);
|
|
await Promise.all(batch.map(logItem));
|
|
}
|
|
console.log('Batch processing completed');
|
|
console.log('Workflow completed');
|
|
}
|
|
|
|
async function logItem(item: number) {
|
|
'use step';
|
|
console.log(item, Date.now());
|
|
}
|
|
|
|
/**
|
|
* Pattern 2: Each batch gets processed in a step function
|
|
*
|
|
* NOTE: If a batch fails, the entire batch will be retried from the beginning.
|
|
*/
|
|
export async function batchInStep() {
|
|
'use workflow';
|
|
|
|
console.log('Workflow started');
|
|
const arr = Array.from({ length: ARRAY_LENGTH }, (_, i) => i + 1);
|
|
const chunkSize = CHUNK_SIZE;
|
|
console.log(
|
|
`Chunking array with size: ${arr.length} and chunk size: ${chunkSize}`
|
|
);
|
|
const chunks = chunk(arr, chunkSize); // Create the batches
|
|
console.log(
|
|
`Created ${chunks.length} chunks (${chunks[0].length} items each)`
|
|
);
|
|
|
|
console.log('Starting batch processing');
|
|
for (const [index, batch] of chunks.entries()) {
|
|
console.log(`Batch ${index + 1}/${chunks.length}`);
|
|
await processItems(batch);
|
|
}
|
|
console.log('Batch processing completed');
|
|
console.log('Workflow completed');
|
|
}
|
|
|
|
/**
|
|
* Step function that processes a batch of items with internal parallelism.
|
|
* Called once per batch, with all items processed in parallel inside the step.
|
|
*/
|
|
async function processItems(items: number[]) {
|
|
'use step';
|
|
await Promise.all(
|
|
items.map(async (item) => {
|
|
console.log(item, Date.now());
|
|
})
|
|
);
|
|
}
|