Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Comprehensive Cloudflare platform skill covering Workers, D1, R2, KV, AI, Durable Objects, and security.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
references/workflows/configuration.md
1# Workflow Configuration23## wrangler.jsonc Setup45```jsonc6{7"name": "my-worker",8"main": "src/index.ts",9"compatibility_date": "2025-01-01", // Minimum 2024-10-22 required for Workflows bindings10"observability": {11"enabled": true // Enables Workflows dashboard + structured logs12},13"workflows": [14{15"name": "my-workflow", // Workflow name16"binding": "MY_WORKFLOW", // Env binding17"class_name": "MyWorkflow", // TS class name18// "script_name": "other-worker" // For cross-script calls19// "limits": { "steps": 25000 } // Optional: max steps per instance (check docs for default/max per plan)20}21],22"limits": {23"cpu_ms": 300000 // Check docs for default and max CPU time per plan24}25}26```2728## Step Configuration2930```typescript31// Basic step32const data = await step.do('step name', async () => ({ result: 'value' }));3334// With retry config35await step.do('api call', {36retries: {37limit: 10, // Accepts number or Infinity38delay: '10 seconds', // Accepts number (ms) or duration string39backoff: 'exponential' // constant | linear | exponential40},41timeout: '30 minutes' // Per-attempt timeout42}, async () => {43const res = await fetch('https://api.example.com/data');44if (!res.ok) throw new Error('Failed');45return res.json();46});47```4849### Parallel Steps50```typescript51const [user, settings] = await Promise.all([52step.do('fetch user', async () => this.env.KV.get(`user:${id}`)),53step.do('fetch settings', async () => this.env.KV.get(`settings:${id}`))54]);55```5657### Conditional Steps58```typescript59const config = await step.do('fetch config', async () =>60this.env.KV.get('flags', { type: 'json' })61);6263// ✅ Deterministic (based on step output)64if (config.enableEmail) {65await step.do('send email', async () => sendEmail());66}6768// ❌ Non-deterministic (Date.now outside step)69if (Date.now() > deadline) { /* BAD */ }70```7172### Dynamic Steps (Loops)73```typescript74const files = await step.do('list files', async () =>75this.env.BUCKET.list()76);7778for (const file of files.objects) {79await step.do(`process ${file.key}`, async () => {80const obj = await this.env.BUCKET.get(file.key);81return processData(await obj.arrayBuffer());82});83}84```8586## Multiple Workflows8788```jsonc89{90"workflows": [91{"name": "user-onboarding", "binding": "USER_ONBOARDING", "class_name": "UserOnboarding"},92{"name": "data-processing", "binding": "DATA_PROCESSING", "class_name": "DataProcessing"}93]94}95```9697Each class extends `WorkflowEntrypoint` with its own `Params` type.9899## Cross-Script Bindings100101Worker A defines workflow. Worker B calls it by adding `script_name`:102103```jsonc104// Worker B (caller)105{106"workflows": [{107"name": "billing-workflow",108"binding": "BILLING",109"script_name": "billing-worker" // Points to Worker A110}]111}112```113114## Bindings115116Workflows access Cloudflare bindings via `this.env`:117118```typescript119type Env = {120MY_WORKFLOW: Workflow;121KV: KVNamespace;122DB: D1Database;123BUCKET: R2Bucket;124AI: Ai;125VECTORIZE: VectorizeIndex;126};127128await step.do('use bindings', async () => {129const kv = await this.env.KV.get('key');130const db = await this.env.DB.prepare('SELECT * FROM users').first();131const file = await this.env.BUCKET.get('file.txt');132const ai = await this.env.AI.run('@cf/meta/llama-2-7b-chat-int8', { prompt: 'Hi' });133});134```135136## Pages Functions Binding137138Pages Functions can trigger Workflows via service bindings:139140```typescript141// functions/_middleware.ts142export const onRequest: PagesFunction<Env> = async ({ env, request }) => {143const instance = await env.MY_WORKFLOW.create({144params: { url: request.url }145});146return new Response(`Started ${instance.id}`);147};148```149150Configure in wrangler.jsonc under `service_bindings`.151152See: [api.md](./api.md), [patterns.md](./patterns.md)153