|
1 | | -# Trigger.dev Advanced Tasks |
| 1 | +# Trigger.dev Advanced Tasks (v4) |
2 | 2 |
|
3 | 3 | **Advanced patterns and features for writing tasks** |
4 | 4 |
|
@@ -36,6 +36,145 @@ for await (const run of runs.subscribeToRunsWithTag("user_123")) { |
36 | 36 | - Max 10 tags per run, 1-64 characters each |
37 | 37 | - Tags don't propagate to child tasks automatically |
38 | 38 |
|
| 39 | +## Batch Triggering v2 |
| 40 | + |
| 41 | +Enhanced batch triggering with larger payloads and streaming ingestion. |
| 42 | + |
| 43 | +### Limits |
| 44 | + |
| 45 | +- **Maximum batch size**: 1,000 items (increased from 500) |
| 46 | +- **Payload per item**: 3MB each (increased from 1MB combined) |
| 47 | +- Payloads > 512KB automatically offload to object storage |
| 48 | + |
| 49 | +### Rate Limiting (per environment) |
| 50 | + |
| 51 | +| Tier | Bucket Size | Refill Rate | |
| 52 | +|------|-------------|-------------| |
| 53 | +| Free | 1,200 runs | 100 runs/10 sec | |
| 54 | +| Hobby | 5,000 runs | 500 runs/5 sec | |
| 55 | +| Pro | 5,000 runs | 500 runs/5 sec | |
| 56 | + |
| 57 | +### Concurrent Batch Processing |
| 58 | + |
| 59 | +| Tier | Concurrent Batches | |
| 60 | +|------|-------------------| |
| 61 | +| Free | 1 | |
| 62 | +| Hobby | 10 | |
| 63 | +| Pro | 10 | |
| 64 | + |
| 65 | +### Usage |
| 66 | + |
| 67 | +```ts |
| 68 | +import { myTask } from "./trigger/myTask"; |
| 69 | + |
| 70 | +// Basic batch trigger (up to 1,000 items) |
| 71 | +const runs = await myTask.batchTrigger([ |
| 72 | + { payload: { userId: "user-1" } }, |
| 73 | + { payload: { userId: "user-2" } }, |
| 74 | + { payload: { userId: "user-3" } }, |
| 75 | +]); |
| 76 | + |
| 77 | +// Batch trigger with wait |
| 78 | +const results = await myTask.batchTriggerAndWait([ |
| 79 | + { payload: { userId: "user-1" } }, |
| 80 | + { payload: { userId: "user-2" } }, |
| 81 | +]); |
| 82 | + |
| 83 | +for (const result of results) { |
| 84 | + if (result.ok) { |
| 85 | + console.log("Result:", result.output); |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +// With per-item options |
| 90 | +const batchHandle = await myTask.batchTrigger([ |
| 91 | + { |
| 92 | + payload: { userId: "123" }, |
| 93 | + options: { |
| 94 | + idempotencyKey: "user-123-batch", |
| 95 | + tags: ["priority"], |
| 96 | + }, |
| 97 | + }, |
| 98 | + { |
| 99 | + payload: { userId: "456" }, |
| 100 | + options: { |
| 101 | + idempotencyKey: "user-456-batch", |
| 102 | + }, |
| 103 | + }, |
| 104 | +]); |
| 105 | +``` |
| 106 | + |
| 107 | +## Debouncing |
| 108 | + |
| 109 | +Consolidate multiple triggers into a single execution by debouncing task runs with a unique key and delay window. |
| 110 | + |
| 111 | +### Use Cases |
| 112 | + |
| 113 | +- **User activity updates**: Batch rapid user actions into a single run |
| 114 | +- **Webhook deduplication**: Handle webhook bursts without redundant processing |
| 115 | +- **Search indexing**: Combine document updates instead of processing individually |
| 116 | +- **Notification batching**: Group notifications to prevent user spam |
| 117 | + |
| 118 | +### Basic Usage |
| 119 | + |
| 120 | +```ts |
| 121 | +await myTask.trigger( |
| 122 | + { userId: "123" }, |
| 123 | + { |
| 124 | + debounce: { |
| 125 | + key: "user-123-update", // Unique identifier for debounce group |
| 126 | + delay: "5s", // Wait duration ("5s", "1m", or milliseconds) |
| 127 | + }, |
| 128 | + } |
| 129 | +); |
| 130 | +``` |
| 131 | + |
| 132 | +### Execution Modes |
| 133 | + |
| 134 | +**Leading Mode** (default): Uses payload/options from the first trigger; subsequent triggers only reschedule execution time. |
| 135 | + |
| 136 | +```ts |
| 137 | +// First trigger sets the payload |
| 138 | +await myTask.trigger({ action: "first" }, { |
| 139 | + debounce: { key: "my-key", delay: "10s" } |
| 140 | +}); |
| 141 | + |
| 142 | +// Second trigger only reschedules - payload remains "first" |
| 143 | +await myTask.trigger({ action: "second" }, { |
| 144 | + debounce: { key: "my-key", delay: "10s" } |
| 145 | +}); |
| 146 | +// Task executes with { action: "first" } |
| 147 | +``` |
| 148 | + |
| 149 | +**Trailing Mode**: Uses payload/options from the most recent trigger. |
| 150 | + |
| 151 | +```ts |
| 152 | +await myTask.trigger( |
| 153 | + { data: "latest-value" }, |
| 154 | + { |
| 155 | + debounce: { |
| 156 | + key: "trailing-example", |
| 157 | + delay: "10s", |
| 158 | + mode: "trailing", |
| 159 | + }, |
| 160 | + } |
| 161 | +); |
| 162 | +``` |
| 163 | + |
| 164 | +In trailing mode, these options update with each trigger: |
| 165 | +- `payload` — task input data |
| 166 | +- `metadata` — run metadata |
| 167 | +- `tags` — run tags (replaces existing) |
| 168 | +- `maxAttempts` — retry attempts |
| 169 | +- `maxDuration` — maximum compute time |
| 170 | +- `machine` — machine preset |
| 171 | + |
| 172 | +### Important Notes |
| 173 | + |
| 174 | +- Idempotency keys take precedence over debounce settings |
| 175 | +- Compatible with `triggerAndWait()` — parent runs block correctly on debounced execution |
| 176 | +- Debounce key is scoped to the task |
| 177 | + |
39 | 178 | ## Concurrency & Queues |
40 | 179 |
|
41 | 180 | ```ts |
@@ -339,6 +478,8 @@ export const publicWorkflow = task({ |
339 | 478 | - **Metadata**: Track progress for long-running tasks |
340 | 479 | - **Machines**: Match machine size to computational requirements |
341 | 480 | - **Tags**: Use consistent naming patterns for filtering |
| 481 | +- **Debouncing**: Use for user activity, webhooks, and notification batching |
| 482 | +- **Batch triggering**: Use for bulk operations up to 1,000 items |
342 | 483 | - **Error Handling**: Distinguish between retryable and fatal errors |
343 | 484 |
|
344 | 485 | Design tasks to be stateless, idempotent, and resilient to failures. Use metadata for state tracking and queues for resource management. |
0 commit comments