Skip to content

Commit a0ebade

Browse files
authored
Workflows: Added Workflow instances batch creation instructions (cloudflare#20775)
1 parent f5d2bb7 commit a0ebade

File tree

3 files changed

+54
-0
lines changed

3 files changed

+54
-0
lines changed

src/content/docs/workflows/build/rules-of-workflows.mdx

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,3 +449,32 @@ export class MyWorkflow extends WorkflowEntrypoint {
449449
}
450450
```
451451
</TypeScriptExample>
452+
453+
### Batch multiple Workflow invocations
454+
455+
When creating multiple Workflow instances, use the [`createBatch`](/workflows/build/workers-api/#createBatch) method to batch the invocations together. This allows you to create multiple Workflow instances in a single request, which will reduce the number of requests made to the Workflows API and increase the number of instances you can create per minute.
456+
457+
<TypeScriptExample filename="index.ts">
458+
459+
```ts
460+
export default {
461+
async fetch(req: Request, env: Env): Promise<Response> {
462+
let instances = [{"id": "user1", "params": {"name": "John"}}, {"id": "user2", "params": {"name": "Jane"}}, {"id": "user3", "params": {"name": "Alice"}}, {"id": "user4", "params": {"name": "Bob"}}];
463+
464+
// 🔴 Bad: Create them one by one, which is more likely to hit creation rate limits.
465+
for (let instance of instances) {
466+
await env.MY_WORKFLOW.create({
467+
id: instance.id,
468+
params: instance.params
469+
});
470+
}
471+
472+
// ✅ Good: Batch calls together
473+
// This improves throughput.
474+
let instances = await env.MY_WORKFLOW.createBatch(instances);
475+
return Response.json({ instances })
476+
},
477+
};
478+
```
479+
480+
</TypeScriptExample>

src/content/docs/workflows/build/workers-api.mdx

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,30 @@ export default {
266266
}
267267
```
268268

269+
### createBatch
270+
271+
Create (trigger) a batch of new instance of the given Workflow, up to 100 instances at a time.
272+
273+
This is useful when you are scheduling multiple instances at once. A call to `createBatch` is treated the same as a call to `create` (for a single instance) and allows you to work within the [instance creation limit](/workflows/reference/limits/).
274+
275+
* <code>createBatch(batch: WorkflowInstanceCreateOptions[]): Promise&lt;WorkflowInstance[]&gt;</code>
276+
277+
* `batch` - list of Options to pass when creating an instance, including a user-provided ID and payload parameters.
278+
279+
Each element of the `batch` list is expected to the
280+
281+
```ts
282+
// Create a new batch of 3 Workflow instances, each with its own ID and pass params to the Workflow instances
283+
const listOfInstances = [
284+
{ id: "id-abc123", params: { "hello": "world-0" } },
285+
{ id: "id-def456", params: { "hello": "world-1" } },
286+
{ id: "id-ghi789", params: { "hello": "world-2" } }
287+
];
288+
let instances = await env.MY_WORKFLOW.createBatch(listOfInstances);
289+
```
290+
291+
Returns an array of `WorkflowInstance`.
292+
269293
### get
270294

271295
Get a specific Workflow instance by ID.

src/content/docs/workflows/get-started/cli-quick-start.mdx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ class_name = "MyWorkflow"
188188
You can then invoke the methods on this binding directly from your Worker script's `env` parameter. The `Workflow` type has methods for:
189189

190190
* `create()` - creating (triggering) a new instance of the Workflow, returning the ID.
191+
* `createBatch()` - creating (triggering) a batch of new instances of the Workflow, returning the IDs.
191192
* `get()`- retrieve a Workflow instance by its ID.
192193
* `status()` - get the current status of a unique Workflow instance.
193194

0 commit comments

Comments
 (0)