workflow 5.0.0-beta.1 → 5.0.0-beta.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +4 -4
- package/dist/api-workflow.d.ts +1 -1
- package/dist/api-workflow.d.ts.map +1 -1
- package/dist/api-workflow.js +2 -2
- package/dist/api.d.ts +5 -1
- package/dist/api.d.ts.map +1 -1
- package/dist/api.js +14 -2
- package/dist/astro.js +1 -1
- package/dist/index.js +1 -1
- package/dist/internal/builtins.d.ts +17 -0
- package/dist/internal/builtins.d.ts.map +1 -1
- package/dist/internal/builtins.js +65 -1
- package/dist/internal/class-serialization.js +1 -1
- package/dist/internal/errors.js +1 -1
- package/dist/nest.js +1 -1
- package/dist/next.cjs +1 -1
- package/dist/nitro.js +1 -1
- package/dist/nuxt.js +1 -1
- package/dist/observability.d.ts +1 -1
- package/dist/observability.js +2 -2
- package/dist/runtime.d.ts +1 -1
- package/dist/runtime.d.ts.map +1 -1
- package/dist/runtime.js +2 -2
- package/dist/stdlib.js +1 -1
- package/dist/sveltekit.js +1 -1
- package/dist/typescript-plugin.cjs +1 -1
- package/dist/vite.js +1 -1
- package/dist/workflow.js +1 -1
- package/docs/ai/index.mdx +6 -5
- package/docs/ai/resumable-streams.mdx +1 -1
- package/docs/api-reference/vitest/index.mdx +28 -1
- package/docs/api-reference/workflow/create-webhook.mdx +37 -18
- package/docs/api-reference/workflow/experimental-set-attributes.mdx +63 -0
- package/docs/api-reference/workflow/get-workflow-metadata.mdx +34 -0
- package/docs/api-reference/workflow/index.mdx +3 -0
- package/docs/api-reference/workflow-ai/durable-agent.mdx +0 -4
- package/docs/api-reference/workflow-ai/index.mdx +0 -5
- package/docs/api-reference/workflow-ai/workflow-chat-transport.mdx +0 -4
- package/docs/api-reference/workflow-api/start.mdx +5 -4
- package/docs/api-reference/workflow-errors/workflow-run-failed-error.mdx +16 -6
- package/docs/api-reference/workflow-next/with-workflow.mdx +34 -2
- package/docs/changelog/attributes-mvp.mdx +368 -0
- package/docs/changelog/eager-processing.mdx +595 -0
- package/docs/changelog/index.mdx +2 -1
- package/docs/cookbook/advanced/child-workflows.mdx +313 -0
- package/docs/cookbook/advanced/meta.json +9 -0
- package/docs/cookbook/advanced/publishing-libraries.mdx +336 -0
- package/docs/cookbook/advanced/serializable-steps.mdx +147 -0
- package/docs/cookbook/advanced/upgrading-workflows.mdx +195 -0
- package/docs/cookbook/agent-patterns/agent-cancellation.mdx +156 -0
- package/docs/cookbook/agent-patterns/durable-agent.mdx +154 -0
- package/docs/cookbook/agent-patterns/human-in-the-loop.mdx +255 -0
- package/docs/cookbook/agent-patterns/meta.json +4 -0
- package/docs/cookbook/common-patterns/batching.mdx +105 -0
- package/docs/cookbook/common-patterns/idempotency.mdx +107 -0
- package/docs/cookbook/common-patterns/meta.json +15 -0
- package/docs/cookbook/common-patterns/rate-limiting.mdx +228 -0
- package/docs/cookbook/common-patterns/saga.mdx +247 -0
- package/docs/cookbook/common-patterns/scheduling.mdx +129 -0
- package/docs/cookbook/common-patterns/sequential-and-parallel.mdx +155 -0
- package/docs/cookbook/common-patterns/timeouts.mdx +99 -0
- package/docs/cookbook/common-patterns/webhooks.mdx +185 -0
- package/docs/cookbook/common-patterns/workflow-composition.mdx +111 -0
- package/docs/cookbook/index.mdx +38 -0
- package/docs/cookbook/integrations/ai-sdk.mdx +383 -0
- package/docs/cookbook/integrations/chat-sdk.mdx +307 -0
- package/docs/cookbook/integrations/meta.json +4 -0
- package/docs/cookbook/integrations/sandbox.mdx +520 -0
- package/docs/cookbook/meta.json +5 -0
- package/docs/deploying/building-a-world.mdx +1 -1
- package/docs/deploying/world/local-world.mdx +1 -1
- package/docs/deploying/world/postgres-world.mdx +6 -4
- package/docs/deploying/world/vercel-world.mdx +3 -1
- package/docs/errors/abort-signal-timeout-in-workflow.mdx +80 -0
- package/docs/errors/hook-conflict.mdx +56 -4
- package/docs/errors/index.mdx +3 -0
- package/docs/errors/runtime-decryption-failed.mdx +77 -0
- package/docs/errors/start-invalid-workflow-function.mdx +1 -1
- package/docs/foundations/cancellation.mdx +460 -0
- package/docs/foundations/errors-and-retries.mdx +7 -3
- package/docs/foundations/index.mdx +3 -3
- package/docs/foundations/meta.json +3 -2
- package/docs/foundations/serialization.mdx +78 -42
- package/docs/foundations/starting-workflows.mdx +6 -2
- package/docs/foundations/versioning.mdx +263 -0
- package/docs/getting-started/astro.mdx +6 -0
- package/docs/getting-started/index.mdx +13 -7
- package/docs/getting-started/meta.json +3 -1
- package/docs/getting-started/nestjs.mdx +8 -0
- package/docs/getting-started/next.mdx +5 -3
- package/docs/getting-started/nitro.mdx +22 -0
- package/docs/getting-started/python.mdx +165 -0
- package/docs/getting-started/sveltekit.mdx +6 -0
- package/docs/getting-started/tanstack-start.mdx +241 -0
- package/docs/how-it-works/cancellation.mdx +287 -0
- package/docs/how-it-works/code-transform.mdx +2 -2
- package/docs/how-it-works/event-sourcing.mdx +2 -2
- package/docs/how-it-works/meta.json +2 -1
- package/docs/internal/index.mdx +19 -0
- package/docs/internal/meta.json +5 -0
- package/docs/internal/serializable-abort-controller.mdx +148 -0
- package/docs/meta.json +1 -0
- package/docs/migration-guides/index.mdx +34 -0
- package/docs/migration-guides/meta.json +9 -0
- package/docs/migration-guides/migrating-from-aws-step-functions.mdx +358 -0
- package/docs/migration-guides/migrating-from-inngest.mdx +304 -0
- package/docs/migration-guides/migrating-from-temporal.mdx +313 -0
- package/docs/migration-guides/migrating-from-trigger-dev.mdx +328 -0
- package/docs/observability/attributes.mdx +66 -0
- package/docs/observability/index.mdx +9 -1
- package/docs/observability/meta.json +1 -1
- package/package.json +14 -13
- package/docs/foundations/common-patterns.mdx +0 -265
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Rate Limiting & Retries
|
|
3
|
+
description: Handle 429 responses and transient failures with RetryableError and exponential backoff.
|
|
4
|
+
type: guide
|
|
5
|
+
summary: When an external API returns 429, throw RetryableError with the Retry-After value so the workflow runtime automatically reschedules the step after the specified delay.
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
Use this pattern when calling external APIs that enforce rate limits. Instead of writing manual retry loops, throw `RetryableError` with a `retryAfter` value and let the workflow runtime handle rescheduling.
|
|
9
|
+
|
|
10
|
+
## When to use this
|
|
11
|
+
|
|
12
|
+
- Calling APIs that return 429 (Too Many Requests) with `Retry-After` headers
|
|
13
|
+
- Any step that hits transient failures and needs backoff
|
|
14
|
+
- Syncing data with third-party services (Stripe, CRMs, scrapers)
|
|
15
|
+
|
|
16
|
+
## Pattern: RetryableError with Retry-After
|
|
17
|
+
|
|
18
|
+
A step function calls an external API. On 429, it reads the `Retry-After` header and throws `RetryableError`. The runtime reschedules the step automatically.
|
|
19
|
+
|
|
20
|
+
```typescript
|
|
21
|
+
import { RetryableError } from "workflow";
|
|
22
|
+
|
|
23
|
+
declare function fetchFromCrm(contactId: string): Promise<unknown>; // @setup
|
|
24
|
+
declare function upsertToWarehouse(contactId: string, contact: unknown): Promise<void>; // @setup
|
|
25
|
+
|
|
26
|
+
export async function syncContact(contactId: string) {
|
|
27
|
+
"use workflow";
|
|
28
|
+
|
|
29
|
+
const contact = await fetchFromCrm(contactId);
|
|
30
|
+
await upsertToWarehouse(contactId, contact);
|
|
31
|
+
|
|
32
|
+
return { contactId, status: "synced" };
|
|
33
|
+
}
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
### Step function with rate limit handling
|
|
37
|
+
|
|
38
|
+
```typescript
|
|
39
|
+
import { RetryableError } from "workflow";
|
|
40
|
+
|
|
41
|
+
async function fetchFromCrm(contactId: string) {
|
|
42
|
+
"use step";
|
|
43
|
+
|
|
44
|
+
const res = await fetch(`https://crm.example.com/contacts/${contactId}`);
|
|
45
|
+
|
|
46
|
+
if (res.status === 429) { // [!code highlight]
|
|
47
|
+
const retryAfter = res.headers.get("Retry-After");
|
|
48
|
+
throw new RetryableError("Rate limited by CRM", { // [!code highlight]
|
|
49
|
+
retryAfter: retryAfter ? parseInt(retryAfter) * 1000 : "1m",
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
if (!res.ok) throw new Error(`CRM returned ${res.status}`);
|
|
54
|
+
return res.json();
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
async function upsertToWarehouse(contactId: string, contact: unknown) {
|
|
58
|
+
"use step";
|
|
59
|
+
await fetch(`https://warehouse.example.com/contacts/${contactId}`, {
|
|
60
|
+
method: "PUT",
|
|
61
|
+
body: JSON.stringify(contact),
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
## Pattern: Exponential backoff
|
|
67
|
+
|
|
68
|
+
Use `getStepMetadata()` to access the current attempt number and calculate increasing delays:
|
|
69
|
+
|
|
70
|
+
```typescript
|
|
71
|
+
import { RetryableError, getStepMetadata } from "workflow";
|
|
72
|
+
|
|
73
|
+
async function callFlakeyApi(endpoint: string) {
|
|
74
|
+
"use step";
|
|
75
|
+
|
|
76
|
+
const { attempt } = getStepMetadata(); // [!code highlight]
|
|
77
|
+
const res = await fetch(endpoint);
|
|
78
|
+
|
|
79
|
+
if (res.status === 429 || res.status >= 500) {
|
|
80
|
+
throw new RetryableError(`Request failed (${res.status})`, { // [!code highlight]
|
|
81
|
+
retryAfter: (attempt ** 2) * 1000, // 1s, 4s, 9s... // [!code highlight]
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
return res.json();
|
|
86
|
+
}
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
## Pattern: Circuit breaker with sleep
|
|
90
|
+
|
|
91
|
+
When a dependency is completely down, stop hitting it for a cooldown period using `sleep()`, then probe with a single test request:
|
|
92
|
+
|
|
93
|
+
```typescript
|
|
94
|
+
import { sleep } from "workflow";
|
|
95
|
+
|
|
96
|
+
export async function circuitBreaker(maxRequests: number = 10) {
|
|
97
|
+
"use workflow";
|
|
98
|
+
|
|
99
|
+
let state: "closed" | "open" | "half-open" = "closed";
|
|
100
|
+
let consecutiveFailures = 0;
|
|
101
|
+
const FAILURE_THRESHOLD = 3;
|
|
102
|
+
|
|
103
|
+
for (let i = 1; i <= maxRequests; i++) {
|
|
104
|
+
if (state === "open") {
|
|
105
|
+
await sleep("30s"); // Durable cooldown // [!code highlight]
|
|
106
|
+
state = "half-open";
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
const success = await callService(i);
|
|
110
|
+
|
|
111
|
+
if (success) {
|
|
112
|
+
consecutiveFailures = 0;
|
|
113
|
+
if (state === "half-open") state = "closed";
|
|
114
|
+
} else {
|
|
115
|
+
consecutiveFailures++;
|
|
116
|
+
if (consecutiveFailures >= FAILURE_THRESHOLD) {
|
|
117
|
+
state = "open";
|
|
118
|
+
consecutiveFailures = 0;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
return { status: state === "closed" ? "recovered" : "failed" };
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
async function callService(requestNum: number): Promise<boolean> {
|
|
127
|
+
"use step";
|
|
128
|
+
try {
|
|
129
|
+
const res = await fetch("https://payment-gateway.example.com/charge");
|
|
130
|
+
return res.ok;
|
|
131
|
+
} catch {
|
|
132
|
+
return false;
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
## Pattern: Custom max retries
|
|
138
|
+
|
|
139
|
+
Override the default retry count (3) for steps that need more or fewer attempts:
|
|
140
|
+
|
|
141
|
+
```typescript
|
|
142
|
+
async function fetchWithRetries(url: string) {
|
|
143
|
+
"use step";
|
|
144
|
+
const res = await fetch(url);
|
|
145
|
+
if (!res.ok) throw new Error(`Failed: ${res.status}`);
|
|
146
|
+
return res.json();
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
// Allow up to 10 retry attempts
|
|
150
|
+
fetchWithRetries.maxRetries = 10; // [!code highlight]
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
## Application-level retry
|
|
154
|
+
|
|
155
|
+
Sometimes you need retry logic at the workflow level -- wrapping a step call with your own backoff instead of relying on the framework's built-in `RetryableError`. This is useful when you want full control over retry conditions, delays, and error filtering.
|
|
156
|
+
|
|
157
|
+
```typescript
|
|
158
|
+
interface RetryOptions {
|
|
159
|
+
maxRetries?: number;
|
|
160
|
+
baseDelay?: number;
|
|
161
|
+
maxDelay?: number;
|
|
162
|
+
shouldRetry?: (error: Error, attempt: number) => boolean;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
async function withRetry<T>(
|
|
166
|
+
fn: () => Promise<T>,
|
|
167
|
+
options: RetryOptions = {},
|
|
168
|
+
): Promise<T> {
|
|
169
|
+
const { maxRetries = 3, baseDelay = 2000, maxDelay = 10000, shouldRetry } = options;
|
|
170
|
+
let lastError: Error | undefined;
|
|
171
|
+
|
|
172
|
+
for (let attempt = 0; attempt <= maxRetries; attempt++) {
|
|
173
|
+
try {
|
|
174
|
+
return await fn();
|
|
175
|
+
} catch (error) {
|
|
176
|
+
lastError = error instanceof Error ? error : new Error(String(error));
|
|
177
|
+
const isLastAttempt = attempt === maxRetries;
|
|
178
|
+
if (isLastAttempt || (shouldRetry && !shouldRetry(lastError, attempt + 1))) {
|
|
179
|
+
throw lastError;
|
|
180
|
+
}
|
|
181
|
+
// Exponential backoff with jitter
|
|
182
|
+
const delay = Math.min(baseDelay * 2 ** attempt * (0.5 + Math.random() * 0.5), maxDelay);
|
|
183
|
+
await new Promise(resolve => setTimeout(resolve, delay));
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
throw lastError;
|
|
188
|
+
}
|
|
189
|
+
```
|
|
190
|
+
|
|
191
|
+
Use it in a workflow to wrap step calls:
|
|
192
|
+
|
|
193
|
+
```typescript
|
|
194
|
+
declare function withRetry<T>(fn: () => Promise<T>, options?: { maxRetries?: number; shouldRetry?: (error: Error) => boolean }): Promise<T>; // @setup
|
|
195
|
+
declare function downloadFile(url: string): Promise<any>; // @setup
|
|
196
|
+
|
|
197
|
+
export async function downloadWithRetry(url: string) {
|
|
198
|
+
"use workflow";
|
|
199
|
+
|
|
200
|
+
const result = await withRetry(() => downloadFile(url), { // [!code highlight]
|
|
201
|
+
maxRetries: 5,
|
|
202
|
+
shouldRetry: (error) => error.message.includes("Timeout"),
|
|
203
|
+
});
|
|
204
|
+
|
|
205
|
+
return result;
|
|
206
|
+
}
|
|
207
|
+
```
|
|
208
|
+
|
|
209
|
+
**When to use this vs `RetryableError`/`FatalError`:**
|
|
210
|
+
- **`RetryableError`** runs inside a step -- the framework reschedules the step after the delay. Use it for transient HTTP errors (429, 503) where the runtime should handle backoff.
|
|
211
|
+
- **Application-level retry** wraps the step call from the workflow. Use it when you need custom retry conditions, want to retry across different steps, or when you're building a library and prefer not to depend on workflow-specific error classes.
|
|
212
|
+
|
|
213
|
+
## Tips
|
|
214
|
+
|
|
215
|
+
- **`RetryableError` is for transient failures.** Use it when the request might succeed on a later attempt (429, 503, network timeout).
|
|
216
|
+
- **`FatalError` is for permanent failures.** Use it when retrying won't help (404, 401, invalid input). This skips all remaining retries.
|
|
217
|
+
- **The `retryAfter` option accepts** a millisecond number, a duration string (`"1m"`, `"30s"`), or a `Date` object.
|
|
218
|
+
- **Steps retry up to 3 times by default.** Set `fn.maxRetries = N` to change this per step function.
|
|
219
|
+
- **Don't write manual sleep-retry loops.** The runtime handles scheduling natively with `RetryableError` -- it's more efficient and survives cold starts.
|
|
220
|
+
|
|
221
|
+
## Key APIs
|
|
222
|
+
|
|
223
|
+
- [`"use workflow"`](/docs/foundations/workflows-and-steps) -- marks the orchestrator function
|
|
224
|
+
- [`"use step"`](/docs/foundations/workflows-and-steps) -- marks functions that run with full Node.js access
|
|
225
|
+
- [`RetryableError`](/docs/api-reference/workflow/retryable-error) -- signals the runtime to retry after a delay
|
|
226
|
+
- [`FatalError`](/docs/api-reference/workflow/fatal-error) -- signals a permanent failure, skipping retries
|
|
227
|
+
- [`getStepMetadata()`](/docs/api-reference/step/get-step-metadata) -- provides the current attempt number and step ID
|
|
228
|
+
- [`sleep()`](/docs/api-reference/workflow/sleep) -- durable pause for circuit breaker cooldowns
|
|
@@ -0,0 +1,247 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Transactions & Rollbacks (Saga)
|
|
3
|
+
description: Coordinate multi-step transactions with automatic rollback when a step fails.
|
|
4
|
+
type: guide
|
|
5
|
+
summary: Run a sequence of steps where each registers a compensation. If any step throws a FatalError, compensations execute in reverse order to restore consistency.
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
Use the saga pattern when a business transaction spans multiple services and you need automatic rollback if any step fails. Each forward step registers a compensation, and on failure the workflow unwinds them in reverse order.
|
|
9
|
+
|
|
10
|
+
## When to use this
|
|
11
|
+
|
|
12
|
+
- Multi-service transactions (reserve inventory, charge payment, provision access)
|
|
13
|
+
- Any sequence where partial completion leaves the system in an inconsistent state
|
|
14
|
+
- Operations that need "all or nothing" semantics across external APIs
|
|
15
|
+
|
|
16
|
+
## How it works
|
|
17
|
+
|
|
18
|
+
1. Each forward step does work and registers a compensation function.
|
|
19
|
+
2. If any step throws `FatalError`, the catch block runs compensations in reverse (LIFO) order to restore consistency.
|
|
20
|
+
3. Regular errors are retried automatically (up to 3x by default). Use `FatalError` only for permanent failures where retrying won't help.
|
|
21
|
+
|
|
22
|
+
## Pattern
|
|
23
|
+
|
|
24
|
+
Each step returns a result and pushes a compensation handler onto a stack. If a later step throws a `FatalError`, the workflow catches it and executes compensations in LIFO order.
|
|
25
|
+
|
|
26
|
+
```typescript
|
|
27
|
+
import { FatalError } from "workflow";
|
|
28
|
+
|
|
29
|
+
declare function reserveSeats(accountId: string, seats: number): Promise<string>; // @setup
|
|
30
|
+
declare function releaseSeats(accountId: string, reservationId: string): Promise<void>; // @setup
|
|
31
|
+
declare function captureInvoice(accountId: string, seats: number): Promise<string>; // @setup
|
|
32
|
+
declare function refundInvoice(accountId: string, invoiceId: string): Promise<void>; // @setup
|
|
33
|
+
declare function provisionSeats(accountId: string, seats: number): Promise<string>; // @setup
|
|
34
|
+
declare function deprovisionSeats(accountId: string, entitlementId: string): Promise<void>; // @setup
|
|
35
|
+
declare function sendConfirmation(accountId: string, invoiceId: string, entitlementId: string): Promise<void>; // @setup
|
|
36
|
+
|
|
37
|
+
export async function subscriptionUpgradeSaga(accountId: string, seats: number) {
|
|
38
|
+
"use workflow";
|
|
39
|
+
|
|
40
|
+
const compensations: Array<() => Promise<void>> = [];
|
|
41
|
+
|
|
42
|
+
try {
|
|
43
|
+
const reservationId = await reserveSeats(accountId, seats);
|
|
44
|
+
compensations.push(() => releaseSeats(accountId, reservationId)); // [!code highlight]
|
|
45
|
+
|
|
46
|
+
const invoiceId = await captureInvoice(accountId, seats);
|
|
47
|
+
compensations.push(() => refundInvoice(accountId, invoiceId)); // [!code highlight]
|
|
48
|
+
|
|
49
|
+
const entitlementId = await provisionSeats(accountId, seats);
|
|
50
|
+
compensations.push(() => deprovisionSeats(accountId, entitlementId)); // [!code highlight]
|
|
51
|
+
|
|
52
|
+
// No compensation — notifications are fire-and-forget
|
|
53
|
+
await sendConfirmation(accountId, invoiceId, entitlementId);
|
|
54
|
+
|
|
55
|
+
return { status: "completed" };
|
|
56
|
+
} catch (error) {
|
|
57
|
+
// Unwind compensations in reverse (LIFO) order
|
|
58
|
+
for (const compensate of compensations.reverse()) { // [!code highlight]
|
|
59
|
+
await compensate(); // [!code highlight]
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
return { status: "rolled_back" };
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
### Step functions
|
|
68
|
+
|
|
69
|
+
Each step is a `"use step"` function with full Node.js access (fetch, fs, npm packages). Forward steps do the work and throw `FatalError` on permanent failure; compensation steps undo it and must be idempotent — safe to call multiple times if the workflow restarts mid-rollback.
|
|
70
|
+
|
|
71
|
+
```typescript
|
|
72
|
+
import { FatalError } from "workflow";
|
|
73
|
+
|
|
74
|
+
// Forward steps
|
|
75
|
+
|
|
76
|
+
async function reserveSeats(accountId: string, seats: number): Promise<string> {
|
|
77
|
+
"use step";
|
|
78
|
+
const res = await fetch(`https://api.example.com/seats/reserve`, {
|
|
79
|
+
method: "POST",
|
|
80
|
+
body: JSON.stringify({ accountId, seats }),
|
|
81
|
+
});
|
|
82
|
+
if (!res.ok) throw new FatalError("Seat reservation failed"); // [!code highlight]
|
|
83
|
+
const { reservationId } = await res.json();
|
|
84
|
+
return reservationId;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
async function captureInvoice(accountId: string, seats: number): Promise<string> {
|
|
88
|
+
"use step";
|
|
89
|
+
const res = await fetch(`https://api.example.com/invoices`, {
|
|
90
|
+
method: "POST",
|
|
91
|
+
body: JSON.stringify({ accountId, seats }),
|
|
92
|
+
});
|
|
93
|
+
if (!res.ok) throw new FatalError("Invoice capture failed"); // [!code highlight]
|
|
94
|
+
const { invoiceId } = await res.json();
|
|
95
|
+
return invoiceId;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
async function provisionSeats(accountId: string, seats: number): Promise<string> {
|
|
99
|
+
"use step";
|
|
100
|
+
const res = await fetch(`https://api.example.com/entitlements`, {
|
|
101
|
+
method: "POST",
|
|
102
|
+
body: JSON.stringify({ accountId, seats }),
|
|
103
|
+
});
|
|
104
|
+
if (!res.ok) throw new FatalError("Provisioning failed"); // [!code highlight]
|
|
105
|
+
const { entitlementId } = await res.json();
|
|
106
|
+
return entitlementId;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
async function sendConfirmation(
|
|
110
|
+
accountId: string,
|
|
111
|
+
invoiceId: string,
|
|
112
|
+
entitlementId: string
|
|
113
|
+
): Promise<void> {
|
|
114
|
+
"use step";
|
|
115
|
+
await fetch(`https://api.example.com/notifications`, {
|
|
116
|
+
method: "POST",
|
|
117
|
+
body: JSON.stringify({ accountId, invoiceId, entitlementId, template: "upgrade-complete" }),
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
// Compensation steps — must be idempotent
|
|
122
|
+
|
|
123
|
+
async function releaseSeats(accountId: string, reservationId: string): Promise<void> {
|
|
124
|
+
"use step";
|
|
125
|
+
await fetch(`https://api.example.com/seats/release`, {
|
|
126
|
+
method: "POST",
|
|
127
|
+
body: JSON.stringify({ accountId, reservationId }),
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
async function refundInvoice(accountId: string, invoiceId: string): Promise<void> {
|
|
132
|
+
"use step";
|
|
133
|
+
await fetch(`https://api.example.com/invoices/${invoiceId}/refund`, {
|
|
134
|
+
method: "POST",
|
|
135
|
+
body: JSON.stringify({ accountId }),
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
async function deprovisionSeats(accountId: string, entitlementId: string): Promise<void> {
|
|
140
|
+
"use step";
|
|
141
|
+
await fetch(`https://api.example.com/entitlements/${entitlementId}`, {
|
|
142
|
+
method: "DELETE",
|
|
143
|
+
body: JSON.stringify({ accountId }),
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
### Streaming step progress (optional)
|
|
149
|
+
|
|
150
|
+
Use `getWritable()` to stream progress events to a UI so users can see each step execute in real time.
|
|
151
|
+
|
|
152
|
+
```typescript
|
|
153
|
+
import { FatalError } from "workflow";
|
|
154
|
+
import { getWritable } from "workflow";
|
|
155
|
+
|
|
156
|
+
type SagaEvent =
|
|
157
|
+
| { type: "step_start"; step: string }
|
|
158
|
+
| { type: "step_done"; step: string; detail: string }
|
|
159
|
+
| { type: "step_failed"; step: string; error: string }
|
|
160
|
+
| { type: "compensating"; step: string }
|
|
161
|
+
| { type: "compensated"; step: string }
|
|
162
|
+
| { type: "result"; status: "completed" | "rolled_back" };
|
|
163
|
+
|
|
164
|
+
async function emit(event: SagaEvent) {
|
|
165
|
+
"use step";
|
|
166
|
+
const writer = getWritable<SagaEvent>().getWriter();
|
|
167
|
+
try {
|
|
168
|
+
await writer.write(event);
|
|
169
|
+
} finally {
|
|
170
|
+
writer.releaseLock();
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
declare function reserveSeats(accountId: string, seats: number): Promise<string>; // @setup
|
|
175
|
+
declare function releaseSeats(accountId: string, reservationId: string): Promise<void>; // @setup
|
|
176
|
+
declare function captureInvoice(accountId: string, seats: number): Promise<string>; // @setup
|
|
177
|
+
declare function refundInvoice(accountId: string, invoiceId: string): Promise<void>; // @setup
|
|
178
|
+
declare function provisionSeats(accountId: string, seats: number): Promise<string>; // @setup
|
|
179
|
+
declare function deprovisionSeats(accountId: string, entitlementId: string): Promise<void>; // @setup
|
|
180
|
+
declare function sendConfirmation(accountId: string, invoiceId: string, entitlementId: string): Promise<void>; // @setup
|
|
181
|
+
|
|
182
|
+
export async function subscriptionUpgradeSaga(accountId: string, seats: number) {
|
|
183
|
+
"use workflow";
|
|
184
|
+
|
|
185
|
+
const compensations: Array<{ name: string; execute: () => Promise<void> }> = [];
|
|
186
|
+
|
|
187
|
+
try {
|
|
188
|
+
await emit({ type: "step_start", step: "Reserve Seats" });
|
|
189
|
+
const reservationId = await reserveSeats(accountId, seats);
|
|
190
|
+
compensations.push({ name: "Release Seats", execute: () => releaseSeats(accountId, reservationId) });
|
|
191
|
+
await emit({ type: "step_done", step: "Reserve Seats", detail: reservationId });
|
|
192
|
+
|
|
193
|
+
await emit({ type: "step_start", step: "Capture Invoice" });
|
|
194
|
+
const invoiceId = await captureInvoice(accountId, seats);
|
|
195
|
+
compensations.push({ name: "Refund Invoice", execute: () => refundInvoice(accountId, invoiceId) });
|
|
196
|
+
await emit({ type: "step_done", step: "Capture Invoice", detail: invoiceId });
|
|
197
|
+
|
|
198
|
+
await emit({ type: "step_start", step: "Provision Seats" });
|
|
199
|
+
const entitlementId = await provisionSeats(accountId, seats);
|
|
200
|
+
compensations.push({ name: "Deprovision Seats", execute: () => deprovisionSeats(accountId, entitlementId) });
|
|
201
|
+
await emit({ type: "step_done", step: "Provision Seats", detail: entitlementId });
|
|
202
|
+
|
|
203
|
+
// No compensation — notifications are fire-and-forget
|
|
204
|
+
await emit({ type: "step_start", step: "Send Confirmation" });
|
|
205
|
+
await sendConfirmation(accountId, invoiceId, entitlementId);
|
|
206
|
+
await emit({ type: "step_done", step: "Send Confirmation", detail: "sent" });
|
|
207
|
+
|
|
208
|
+
await emit({ type: "result", status: "completed" });
|
|
209
|
+
return { status: "completed" };
|
|
210
|
+
} catch (error) {
|
|
211
|
+
const errorMessage = error instanceof Error ? error.message : "Unknown error";
|
|
212
|
+
await emit({ type: "step_failed", step: "failed", error: errorMessage });
|
|
213
|
+
|
|
214
|
+
// Unwind compensations in reverse (LIFO) order
|
|
215
|
+
for (const comp of compensations.reverse()) {
|
|
216
|
+
await emit({ type: "compensating", step: comp.name });
|
|
217
|
+
await comp.execute();
|
|
218
|
+
await emit({ type: "compensated", step: comp.name });
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
await emit({ type: "result", status: "rolled_back" });
|
|
222
|
+
return { status: "rolled_back" };
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
```
|
|
226
|
+
|
|
227
|
+
## Adapting to your use case
|
|
228
|
+
|
|
229
|
+
- Replace the step functions with real API calls. Each `"use step"` function has full Node.js access.
|
|
230
|
+
- Add or remove steps as needed — the pattern scales to any number of steps.
|
|
231
|
+
- Make compensations idempotent — they may be retried if the workflow restarts mid-rollback.
|
|
232
|
+
- The `emit()` calls and `SagaEvent` type are optional — remove them if you don't need real-time UI progress.
|
|
233
|
+
|
|
234
|
+
## Tips
|
|
235
|
+
|
|
236
|
+
- **Use `FatalError` for permanent failures.** Regular errors trigger automatic retries (up to 3 by default). Throw `FatalError` when retrying won't help (e.g., insufficient funds, invalid input).
|
|
237
|
+
- **Make compensations idempotent.** If a compensation step is retried, it should produce the same result. Check whether the resource was already released before releasing it again.
|
|
238
|
+
- **Compensation steps are also `"use step"` functions.** This makes them durable — if the workflow restarts mid-rollback, it resumes where it left off.
|
|
239
|
+
- **Capture values in closures carefully.** Use block-scoped variables or copy values before pushing compensations to avoid referencing stale state.
|
|
240
|
+
- **Notifications don't need compensations.** Fire-and-forget steps like sending emails or Slack messages typically don't register a compensation.
|
|
241
|
+
|
|
242
|
+
## Key APIs
|
|
243
|
+
|
|
244
|
+
- [`"use workflow"`](/docs/api-reference/workflow/use-workflow) -- declares the orchestrator function
|
|
245
|
+
- [`"use step"`](/docs/api-reference/workflow/use-step) -- declares step functions with full Node.js access
|
|
246
|
+
- [`FatalError`](/docs/api-reference/workflow/fatal-error) -- non-retryable error that triggers compensation
|
|
247
|
+
- [`getWritable()`](/docs/api-reference/workflow/get-writable) -- streams data from workflows for real-time UI updates
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Sleep, Scheduling & Timed Workflows
|
|
3
|
+
description: Use durable sleep to schedule actions minutes, hours, days, or weeks into the future.
|
|
4
|
+
type: guide
|
|
5
|
+
summary: Schedule future actions with durable sleep that survives cold starts, and race sleeps against hooks to let external events cancel the workflow early.
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
Workflow's `sleep()` is durable — it survives cold starts, restarts, and deployments. Combined with `defineHook()` and `Promise.race()`, it becomes the foundation for interruptible scheduled workflows like drip campaigns, reminders, and timed sequences.
|
|
9
|
+
|
|
10
|
+
<Callout type="info">
|
|
11
|
+
Scheduled workflows are still pinned to the deployment that started them. If you are building recurring or indefinitely running schedules that should adopt newer code over time, see [Versioning](/docs/foundations/versioning) for the explicit `deploymentId: "latest"` continuation pattern.
|
|
12
|
+
</Callout>
|
|
13
|
+
|
|
14
|
+
## When to use this
|
|
15
|
+
|
|
16
|
+
- Sending emails on a schedule (drip campaigns, onboarding sequences, reminders)
|
|
17
|
+
- Waiting for a deadline but allowing early cancellation
|
|
18
|
+
- Any pattern where "do X, wait N hours, then do Y" needs to be both reliable and interruptible
|
|
19
|
+
|
|
20
|
+
## Drip campaign with cancellation
|
|
21
|
+
|
|
22
|
+
A drip campaign sends emails at intervals, sleeping between each. Each sleep races against a cancellation hook — if an external event fires the hook (e.g. user converts, unsubscribes), the campaign stops immediately.
|
|
23
|
+
|
|
24
|
+
```typescript
|
|
25
|
+
import { defineHook, sleep } from "workflow";
|
|
26
|
+
|
|
27
|
+
// Hook that any API route can fire to cancel the drip
|
|
28
|
+
export const cancelDrip = defineHook<{ reason?: string }>(); // [!code highlight]
|
|
29
|
+
|
|
30
|
+
async function sendEmail(email: string, template: string): Promise<void> {
|
|
31
|
+
"use step";
|
|
32
|
+
await fetch("https://api.sendgrid.com/v3/mail/send", {
|
|
33
|
+
method: "POST",
|
|
34
|
+
headers: { Authorization: `Bearer ${process.env.SENDGRID_KEY}` },
|
|
35
|
+
body: JSON.stringify({ to: [{ email }], template_id: template }),
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export async function emailSequence(email: string) {
|
|
40
|
+
"use workflow";
|
|
41
|
+
|
|
42
|
+
await sendEmail(email, "welcome");
|
|
43
|
+
|
|
44
|
+
// Race durable sleep against the cancellation hook
|
|
45
|
+
const hook = cancelDrip.create({ token: `cancel-drip:${email}` }); // [!code highlight]
|
|
46
|
+
const cancelled = await Promise.race([ // [!code highlight]
|
|
47
|
+
sleep("2d").then(() => false), // [!code highlight]
|
|
48
|
+
hook.then(() => true), // [!code highlight]
|
|
49
|
+
]); // [!code highlight]
|
|
50
|
+
if (cancelled) return { status: "cancelled", email };
|
|
51
|
+
|
|
52
|
+
await sendEmail(email, "getting-started-tips");
|
|
53
|
+
|
|
54
|
+
// Create a fresh hook for the next sleep window
|
|
55
|
+
const hook2 = cancelDrip.create({ token: `cancel-drip:${email}` }); // [!code highlight]
|
|
56
|
+
const cancelled2 = await Promise.race([ // [!code highlight]
|
|
57
|
+
sleep("2d").then(() => false), // [!code highlight]
|
|
58
|
+
hook2.then(() => true), // [!code highlight]
|
|
59
|
+
]); // [!code highlight]
|
|
60
|
+
if (cancelled2) return { status: "cancelled", email };
|
|
61
|
+
|
|
62
|
+
await sendEmail(email, "feature-highlights");
|
|
63
|
+
|
|
64
|
+
return { status: "drip-complete", email };
|
|
65
|
+
}
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
### Cancelling from an API route
|
|
69
|
+
|
|
70
|
+
Any server-side code can fire the hook by calling `.resume()` with the same token:
|
|
71
|
+
|
|
72
|
+
```typescript
|
|
73
|
+
import { cancelDrip } from "@/workflows/email-sequence";
|
|
74
|
+
|
|
75
|
+
export async function POST(req: Request) {
|
|
76
|
+
const { email, reason } = await req.json();
|
|
77
|
+
|
|
78
|
+
if (!email) {
|
|
79
|
+
return Response.json({ error: "email is required" }, { status: 400 });
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
try {
|
|
83
|
+
await cancelDrip.resume(`cancel-drip:${email}`, { // [!code highlight]
|
|
84
|
+
reason: reason ?? "User completed action", // [!code highlight]
|
|
85
|
+
}); // [!code highlight]
|
|
86
|
+
} catch (error) {
|
|
87
|
+
const msg = error instanceof Error ? error.message.toLowerCase() : "";
|
|
88
|
+
if (msg.includes("not found") || msg.includes("expired")) {
|
|
89
|
+
return Response.json({
|
|
90
|
+
success: true,
|
|
91
|
+
email,
|
|
92
|
+
note: "No active drip found (already completed or cancelled)",
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
throw error;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
return Response.json({ success: true, email });
|
|
99
|
+
}
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
## How it works
|
|
103
|
+
|
|
104
|
+
1. **Durable sleep** — `sleep("2d")` persists through restarts at zero compute cost. The workflow resumes precisely when the timer fires.
|
|
105
|
+
2. **Hook creation** — `cancelDrip.create({ token })` registers a hook that resolves when any external system calls `.resume()` with the same token.
|
|
106
|
+
3. **Race** — `Promise.race([sleep(...), hook])` blocks until either the timer fires or the hook is resumed, whichever comes first.
|
|
107
|
+
4. **Fresh hooks per window** — after a sleep completes normally, the previous hook instance is consumed. A new `.create()` call registers a fresh hook for the next sleep window, reusing the same token.
|
|
108
|
+
|
|
109
|
+
## Adapting to your use case
|
|
110
|
+
|
|
111
|
+
- **Change durations** — replace `"2d"` with any duration string (`"1h"`, `"7d"`, `"30m"`) or a `Date` object for absolute times.
|
|
112
|
+
- **Add more steps** — the pattern scales to any number of email-then-sleep pairs.
|
|
113
|
+
- **Snooze instead of cancel** — resolve the hook with a `snooze` payload and sleep again: `sleep(new Date(Date.now() + payload.snoozeMs))`.
|
|
114
|
+
- **Timeout any operation** — the same `Promise.race(sleep, work)` pattern works for adding deadlines to slow steps.
|
|
115
|
+
- **Real providers** — swap the `sendEmail` step body for Resend, Postmark, or any HTTP API. The `"use step"` function has full Node.js access.
|
|
116
|
+
|
|
117
|
+
## Tips
|
|
118
|
+
|
|
119
|
+
- **`sleep()` accepts** duration strings (`"1d"`, `"2h"`, `"30s"`), milliseconds, or `Date` objects for sleeping until a specific time.
|
|
120
|
+
- **Durable means durable.** A `sleep("7d")` workflow costs nothing while sleeping — no compute, no memory.
|
|
121
|
+
- **Use `sleep()` in workflow context only.** Step functions cannot call `sleep()` directly. If a step needs a delay, use `setTimeout` inside the step.
|
|
122
|
+
|
|
123
|
+
## Key APIs
|
|
124
|
+
|
|
125
|
+
- [`"use workflow"`](/docs/foundations/workflows-and-steps) — marks the orchestrator function
|
|
126
|
+
- [`"use step"`](/docs/foundations/workflows-and-steps) — marks functions that run with full Node.js access
|
|
127
|
+
- [`sleep()`](/docs/api-reference/workflow/sleep) — durable wait (survives restarts, zero compute cost)
|
|
128
|
+
- [`defineHook()`](/docs/api-reference/workflow/define-hook) — creates a typed hook that external systems can fire
|
|
129
|
+
- [`Promise.race()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/race) — races sleep against hooks for interruptible waits
|