@trigger.dev/sdk 4.5.0-rc.6 → 4.5.0
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/dist/commonjs/v3/ai-shared.js +1 -4
- package/dist/commonjs/v3/ai-shared.js.map +1 -1
- package/dist/commonjs/v3/ai.d.ts +178 -6
- package/dist/commonjs/v3/ai.js +417 -64
- package/dist/commonjs/v3/ai.js.map +1 -1
- package/dist/commonjs/v3/chat-client.js +18 -14
- package/dist/commonjs/v3/chat-client.js.map +1 -1
- package/dist/commonjs/v3/chat-react.js +1 -3
- package/dist/commonjs/v3/chat-react.js.map +1 -1
- package/dist/commonjs/v3/chat-server.d.ts +76 -0
- package/dist/commonjs/v3/chat-server.js +194 -24
- package/dist/commonjs/v3/chat-server.js.map +1 -1
- package/dist/commonjs/v3/chat-server.test.js +201 -6
- package/dist/commonjs/v3/chat-server.test.js.map +1 -1
- package/dist/commonjs/v3/chat.js +4 -3
- package/dist/commonjs/v3/chat.js.map +1 -1
- package/dist/commonjs/v3/chat.test.js +6 -18
- package/dist/commonjs/v3/chat.test.js.map +1 -1
- package/dist/commonjs/v3/createStartSessionAction.test.js +30 -0
- package/dist/commonjs/v3/createStartSessionAction.test.js.map +1 -1
- package/dist/commonjs/v3/idempotencyKeys.js.map +1 -1
- package/dist/commonjs/v3/prompt.js +2 -6
- package/dist/commonjs/v3/prompt.js.map +1 -1
- package/dist/commonjs/v3/promptManagement.js.map +1 -1
- package/dist/commonjs/v3/retry.js.map +1 -1
- package/dist/commonjs/v3/runs.d.ts +1 -1
- package/dist/commonjs/v3/sessions.d.ts +3 -2
- package/dist/commonjs/v3/sessions.js +4 -2
- package/dist/commonjs/v3/sessions.js.map +1 -1
- package/dist/commonjs/v3/shared.js.map +1 -1
- package/dist/commonjs/v3/skill.js.map +1 -1
- package/dist/commonjs/v3/streams.js +2 -3
- package/dist/commonjs/v3/streams.js.map +1 -1
- package/dist/commonjs/v3/streams.test.js.map +1 -1
- package/dist/commonjs/v3/test/mock-chat-agent.js +1 -4
- package/dist/commonjs/v3/test/mock-chat-agent.js.map +1 -1
- package/dist/commonjs/v3/test/test-session-handle.js.map +1 -1
- package/dist/commonjs/v3/triggerClient.test.js +1 -1
- package/dist/commonjs/v3/triggerClient.test.js.map +1 -1
- package/dist/commonjs/v3/triggerClient.types.test.js +11 -11
- package/dist/commonjs/v3/triggerClient.types.test.js.map +1 -1
- package/dist/commonjs/version.js +1 -1
- package/dist/esm/v3/ai-shared.js +1 -4
- package/dist/esm/v3/ai-shared.js.map +1 -1
- package/dist/esm/v3/ai.d.ts +178 -6
- package/dist/esm/v3/ai.js +417 -64
- package/dist/esm/v3/ai.js.map +1 -1
- package/dist/esm/v3/chat-client.js +18 -14
- package/dist/esm/v3/chat-client.js.map +1 -1
- package/dist/esm/v3/chat-react.js +1 -3
- package/dist/esm/v3/chat-react.js.map +1 -1
- package/dist/esm/v3/chat-server.d.ts +76 -0
- package/dist/esm/v3/chat-server.js +194 -24
- package/dist/esm/v3/chat-server.js.map +1 -1
- package/dist/esm/v3/chat-server.test.js +201 -6
- package/dist/esm/v3/chat-server.test.js.map +1 -1
- package/dist/esm/v3/chat.js +4 -3
- package/dist/esm/v3/chat.js.map +1 -1
- package/dist/esm/v3/chat.test.js +6 -18
- package/dist/esm/v3/chat.test.js.map +1 -1
- package/dist/esm/v3/createStartSessionAction.test.js +30 -0
- package/dist/esm/v3/createStartSessionAction.test.js.map +1 -1
- package/dist/esm/v3/idempotencyKeys.js +1 -1
- package/dist/esm/v3/idempotencyKeys.js.map +1 -1
- package/dist/esm/v3/prompt.js +2 -6
- package/dist/esm/v3/prompt.js.map +1 -1
- package/dist/esm/v3/promptManagement.js.map +1 -1
- package/dist/esm/v3/retry.js.map +1 -1
- package/dist/esm/v3/sessions.d.ts +3 -2
- package/dist/esm/v3/sessions.js +4 -2
- package/dist/esm/v3/sessions.js.map +1 -1
- package/dist/esm/v3/shared.js.map +1 -1
- package/dist/esm/v3/skill.js.map +1 -1
- package/dist/esm/v3/streams.js +2 -3
- package/dist/esm/v3/streams.js.map +1 -1
- package/dist/esm/v3/streams.test.js.map +1 -1
- package/dist/esm/v3/test/mock-chat-agent.js +4 -7
- package/dist/esm/v3/test/mock-chat-agent.js.map +1 -1
- package/dist/esm/v3/test/test-session-handle.js.map +1 -1
- package/dist/esm/v3/triggerClient.test.js +1 -1
- package/dist/esm/v3/triggerClient.test.js.map +1 -1
- package/dist/esm/v3/triggerClient.types.test.js +11 -11
- package/dist/esm/v3/triggerClient.types.test.js.map +1 -1
- package/dist/esm/version.js +1 -1
- package/docs/ai/prompts.mdx +426 -0
- package/docs/ai-chat/actions.mdx +111 -0
- package/docs/ai-chat/anatomy.mdx +67 -0
- package/docs/ai-chat/backend.mdx +813 -0
- package/docs/ai-chat/background-injection.mdx +217 -0
- package/docs/ai-chat/changelog.mdx +958 -0
- package/docs/ai-chat/chat-local.mdx +170 -0
- package/docs/ai-chat/client-protocol.mdx +1077 -0
- package/docs/ai-chat/compaction.mdx +407 -0
- package/docs/ai-chat/custom-agents.mdx +392 -0
- package/docs/ai-chat/error-handling.mdx +411 -0
- package/docs/ai-chat/fast-starts.mdx +754 -0
- package/docs/ai-chat/frontend.mdx +576 -0
- package/docs/ai-chat/how-it-works.mdx +226 -0
- package/docs/ai-chat/lifecycle-hooks.mdx +526 -0
- package/docs/ai-chat/mcp.mdx +97 -0
- package/docs/ai-chat/overview.mdx +86 -0
- package/docs/ai-chat/patterns/branching-conversations.mdx +280 -0
- package/docs/ai-chat/patterns/code-sandbox.mdx +122 -0
- package/docs/ai-chat/patterns/database-persistence.mdx +410 -0
- package/docs/ai-chat/patterns/human-in-the-loop.mdx +279 -0
- package/docs/ai-chat/patterns/large-payloads.mdx +165 -0
- package/docs/ai-chat/patterns/oom-resilience.mdx +116 -0
- package/docs/ai-chat/patterns/persistence-and-replay.mdx +207 -0
- package/docs/ai-chat/patterns/recovery-boot.mdx +226 -0
- package/docs/ai-chat/patterns/skills.mdx +217 -0
- package/docs/ai-chat/patterns/sub-agents.mdx +379 -0
- package/docs/ai-chat/patterns/tool-result-auditing.mdx +144 -0
- package/docs/ai-chat/patterns/trusted-edge-signals.mdx +333 -0
- package/docs/ai-chat/patterns/version-upgrades.mdx +168 -0
- package/docs/ai-chat/pending-messages.mdx +339 -0
- package/docs/ai-chat/prompt-caching.mdx +206 -0
- package/docs/ai-chat/quick-start.mdx +157 -0
- package/docs/ai-chat/reference.mdx +905 -0
- package/docs/ai-chat/server-chat.mdx +259 -0
- package/docs/ai-chat/sessions.mdx +329 -0
- package/docs/ai-chat/testing.mdx +678 -0
- package/docs/ai-chat/tools.mdx +187 -0
- package/docs/ai-chat/types.mdx +238 -0
- package/docs/ai-chat/upgrade-guide.mdx +511 -0
- package/docs/apikeys.mdx +54 -0
- package/docs/building-with-ai.mdx +261 -0
- package/docs/bulk-actions.mdx +49 -0
- package/docs/changelog.mdx +6 -0
- package/docs/cli-deploy-commands.mdx +9 -0
- package/docs/cli-dev-commands.mdx +9 -0
- package/docs/cli-init-commands.mdx +58 -0
- package/docs/cli-introduction.mdx +25 -0
- package/docs/cli-list-profiles-commands.mdx +42 -0
- package/docs/cli-login-commands.mdx +33 -0
- package/docs/cli-logout-commands.mdx +33 -0
- package/docs/cli-preview-archive.mdx +59 -0
- package/docs/cli-promote-commands.mdx +9 -0
- package/docs/cli-switch.mdx +43 -0
- package/docs/cli-update-commands.mdx +42 -0
- package/docs/cli-whoami-commands.mdx +33 -0
- package/docs/community.mdx +6 -0
- package/docs/config/config-file.mdx +602 -0
- package/docs/config/extensions/additionalFiles.mdx +38 -0
- package/docs/config/extensions/additionalPackages.mdx +40 -0
- package/docs/config/extensions/aptGet.mdx +34 -0
- package/docs/config/extensions/audioWaveform.mdx +20 -0
- package/docs/config/extensions/custom.mdx +380 -0
- package/docs/config/extensions/emitDecoratorMetadata.mdx +29 -0
- package/docs/config/extensions/esbuildPlugin.mdx +31 -0
- package/docs/config/extensions/ffmpeg.mdx +45 -0
- package/docs/config/extensions/lightpanda.mdx +56 -0
- package/docs/config/extensions/overview.mdx +67 -0
- package/docs/config/extensions/playwright.mdx +195 -0
- package/docs/config/extensions/prismaExtension.mdx +1014 -0
- package/docs/config/extensions/puppeteer.mdx +30 -0
- package/docs/config/extensions/pythonExtension.mdx +182 -0
- package/docs/config/extensions/syncEnvVars.mdx +291 -0
- package/docs/context.mdx +235 -0
- package/docs/database-connections.mdx +213 -0
- package/docs/deploy-environment-variables.mdx +435 -0
- package/docs/deployment/atomic-deployment.mdx +172 -0
- package/docs/deployment/dev-branches.mdx +92 -0
- package/docs/deployment/overview.mdx +257 -0
- package/docs/deployment/preview-branches.mdx +224 -0
- package/docs/errors-retrying.mdx +379 -0
- package/docs/github-actions.mdx +222 -0
- package/docs/github-integration.mdx +136 -0
- package/docs/github-repo.mdx +8 -0
- package/docs/help-email.mdx +6 -0
- package/docs/help-slack.mdx +12 -0
- package/docs/hidden-tasks.mdx +56 -0
- package/docs/how-it-works.mdx +454 -0
- package/docs/how-to-reduce-your-spend.mdx +224 -0
- package/docs/idempotency.mdx +504 -0
- package/docs/introduction.mdx +223 -0
- package/docs/limits.mdx +241 -0
- package/docs/logging.mdx +195 -0
- package/docs/machines.mdx +952 -0
- package/docs/manual-setup.mdx +632 -0
- package/docs/mcp-agent-rules.mdx +41 -0
- package/docs/mcp-introduction.mdx +385 -0
- package/docs/mcp-tools.mdx +273 -0
- package/docs/migrating-from-v3.mdx +334 -0
- package/docs/observability/dashboards.mdx +102 -0
- package/docs/observability/query.mdx +585 -0
- package/docs/open-source-contributing.mdx +16 -0
- package/docs/open-source-self-hosting.mdx +541 -0
- package/docs/private-networking/aws-console-setup.mdx +304 -0
- package/docs/private-networking/overview.mdx +144 -0
- package/docs/private-networking/troubleshooting.mdx +78 -0
- package/docs/queue-concurrency.mdx +354 -0
- package/docs/quick-start.mdx +97 -0
- package/docs/realtime/auth.mdx +208 -0
- package/docs/realtime/backend/overview.mdx +45 -0
- package/docs/realtime/backend/streams.mdx +418 -0
- package/docs/realtime/backend/subscribe.mdx +225 -0
- package/docs/realtime/how-it-works.mdx +94 -0
- package/docs/realtime/overview.mdx +63 -0
- package/docs/realtime/react-hooks/overview.mdx +73 -0
- package/docs/realtime/react-hooks/streams.mdx +449 -0
- package/docs/realtime/react-hooks/subscribe.mdx +674 -0
- package/docs/realtime/react-hooks/swr.mdx +87 -0
- package/docs/realtime/react-hooks/triggering.mdx +194 -0
- package/docs/realtime/react-hooks/use-wait-token.mdx +34 -0
- package/docs/realtime/run-object.mdx +174 -0
- package/docs/replaying.mdx +72 -0
- package/docs/request-feature.mdx +6 -0
- package/docs/roadmap.mdx +6 -0
- package/docs/run-tests.mdx +20 -0
- package/docs/run-usage.mdx +113 -0
- package/docs/runs/heartbeats.mdx +38 -0
- package/docs/runs/max-duration.mdx +139 -0
- package/docs/runs/metadata.mdx +734 -0
- package/docs/runs/priority.mdx +31 -0
- package/docs/runs.mdx +396 -0
- package/docs/self-hosting/docker.mdx +458 -0
- package/docs/self-hosting/env/supervisor.mdx +74 -0
- package/docs/self-hosting/env/webapp.mdx +276 -0
- package/docs/self-hosting/kubernetes.mdx +601 -0
- package/docs/self-hosting/overview.mdx +109 -0
- package/docs/skills.mdx +85 -0
- package/docs/tags.mdx +120 -0
- package/docs/tasks/overview.mdx +697 -0
- package/docs/tasks/scheduled.mdx +382 -0
- package/docs/tasks/schemaTask.mdx +413 -0
- package/docs/tasks/streams.mdx +884 -0
- package/docs/triggering.mdx +1320 -0
- package/docs/troubleshooting-alerts.mdx +385 -0
- package/docs/troubleshooting-debugging-in-vscode.mdx +9 -0
- package/docs/troubleshooting-github-issues.mdx +6 -0
- package/docs/troubleshooting-uptime-status.mdx +7 -0
- package/docs/troubleshooting.mdx +398 -0
- package/docs/upgrading-packages.mdx +80 -0
- package/docs/vercel-integration.mdx +207 -0
- package/docs/versioning.mdx +56 -0
- package/docs/video-walkthrough.mdx +23 -0
- package/docs/wait-for-token.mdx +540 -0
- package/docs/wait-for.mdx +42 -0
- package/docs/wait-until.mdx +53 -0
- package/docs/wait.mdx +18 -0
- package/docs/writing-tasks-introduction.mdx +33 -0
- package/package.json +8 -5
- package/skills/trigger-authoring-chat-agent/SKILL.md +296 -0
- package/skills/trigger-authoring-tasks/SKILL.md +254 -0
- package/skills/trigger-chat-agent-advanced/SKILL.md +368 -0
- package/skills/trigger-cost-savings/SKILL.md +116 -0
- package/skills/trigger-realtime-and-frontend/SKILL.md +276 -0
|
@@ -0,0 +1,224 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: "How to reduce your spend"
|
|
3
|
+
description: "Tips and best practices to reduce your costs on Trigger.dev"
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
## Check out your usage page regularly
|
|
7
|
+
|
|
8
|
+
Monitor your usage dashboard to understand your spending patterns. You can see:
|
|
9
|
+
|
|
10
|
+
- Your most expensive tasks
|
|
11
|
+
- Your total duration by task
|
|
12
|
+
- Number of runs by task
|
|
13
|
+
- Spikes in your daily usage
|
|
14
|
+
|
|
15
|
+

|
|
16
|
+
|
|
17
|
+
You can view your usage page by clicking the "Organization" menu in the top left of the dashboard and then clicking "Usage".
|
|
18
|
+
|
|
19
|
+
## Set billing limits and alerts
|
|
20
|
+
|
|
21
|
+
Configure billing limits and alerts in your dashboard to protect against unexpected usage and get notified when you approach spending thresholds. This helps you:
|
|
22
|
+
|
|
23
|
+
- Set a monthly compute spend limit for your organization
|
|
24
|
+
- Catch unexpected cost increases early
|
|
25
|
+
- Identify runaway tasks before they become expensive
|
|
26
|
+
|
|
27
|
+
The **Billing limits** settings page has two sections:
|
|
28
|
+
|
|
29
|
+
- **Billing limit**: Choose your plan limit, a custom amount, or no limit. When a limit is reached, billable environments (`production`, `staging`, and `preview`) enter a **grace period** — queues pause and new runs queue without starting. After grace expires, new triggers are rejected until you increase or remove the limit.
|
|
30
|
+
- **Billing alerts**: Add email alerts at specific spend thresholds (% of your limit when a limit is set, or dollar amounts when no limit is configured). Alerts notify you only; they do **not** pause environments or reject triggers.
|
|
31
|
+
|
|
32
|
+
**Limits vs alerts:** A billing limit enforces spend (grace → reject). Billing alerts are optional notifications at thresholds you choose.
|
|
33
|
+
|
|
34
|
+
**Soft limits:** Billing limits are not instantaneous hard caps. Usage is evaluated on a short delay, so spend can briefly exceed your limit before enforcement applies. Queued runs during grace incur no compute cost until they start. See our [terms](https://trigger.dev/terms) for refund policy details.
|
|
35
|
+
|
|
36
|
+
On the **Usage** page, when you have a custom billing limit (or a plan limit that differs from included usage), a **Billing limit** marker appears on the usage bar alongside your current spend and plan included usage.
|
|
37
|
+
|
|
38
|
+

|
|
39
|
+
|
|
40
|
+
You can open the page from the **Organization** menu in the top left of the dashboard, then **Settings** → **Billing limits**.
|
|
41
|
+
|
|
42
|
+
## Reduce your machine sizes
|
|
43
|
+
|
|
44
|
+
The larger the machine, the more it costs per second. [View the machine pricing](https://trigger.dev/pricing#computePricing).
|
|
45
|
+
|
|
46
|
+
Start with the smallest machine that works, then scale up only if needed:
|
|
47
|
+
|
|
48
|
+
```ts
|
|
49
|
+
// Default: small-1x (0.5 vCPU, 0.5 GB RAM)
|
|
50
|
+
export const lightTask = task({
|
|
51
|
+
id: "light-task",
|
|
52
|
+
// No machine config needed - uses small-1x by default
|
|
53
|
+
run: async (payload) => {
|
|
54
|
+
// Simple operations
|
|
55
|
+
},
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
// Only use larger machines when necessary
|
|
59
|
+
export const heavyTask = task({
|
|
60
|
+
id: "heavy-task",
|
|
61
|
+
machine: "medium-1x", // 1 vCPU, 2 GB RAM
|
|
62
|
+
run: async (payload) => {
|
|
63
|
+
// CPU/memory intensive operations
|
|
64
|
+
},
|
|
65
|
+
});
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
You can also override machine size when triggering if you know certain payloads need more resources. [Read more about machine sizes](/machines).
|
|
69
|
+
|
|
70
|
+
## Avoid duplicate work using idempotencyKey
|
|
71
|
+
|
|
72
|
+
Idempotency keys prevent expensive duplicate work by ensuring the same operation isn't performed multiple times. This is especially valuable during task retries or when the same trigger might fire multiple times.
|
|
73
|
+
|
|
74
|
+
When you use an idempotency key, Trigger.dev remembers the result and skips re-execution, saving you compute costs:
|
|
75
|
+
|
|
76
|
+
```ts
|
|
77
|
+
export const expensiveApiCall = task({
|
|
78
|
+
id: "expensive-api-call",
|
|
79
|
+
run: async (payload: { userId: string }) => {
|
|
80
|
+
// This expensive operation will only run once per user
|
|
81
|
+
await wait.for(
|
|
82
|
+
{ seconds: 30 },
|
|
83
|
+
{
|
|
84
|
+
idempotencyKey: `user-processing-${payload.userId}`,
|
|
85
|
+
idempotencyKeyTTL: "1h",
|
|
86
|
+
}
|
|
87
|
+
);
|
|
88
|
+
|
|
89
|
+
const result = await processUserData(payload.userId);
|
|
90
|
+
return result;
|
|
91
|
+
},
|
|
92
|
+
});
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
You can use idempotency keys with various wait functions:
|
|
96
|
+
|
|
97
|
+
```ts
|
|
98
|
+
// Skip waits during retries
|
|
99
|
+
const token = await wait.createToken({
|
|
100
|
+
idempotencyKey: `daily-report-${new Date().toDateString()}`,
|
|
101
|
+
idempotencyKeyTTL: "24h",
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
// Prevent duplicate child task execution
|
|
105
|
+
await childTask.triggerAndWait(
|
|
106
|
+
{ data: payload },
|
|
107
|
+
{
|
|
108
|
+
idempotencyKey: `process-${payload.id}`,
|
|
109
|
+
idempotencyKeyTTL: "1h",
|
|
110
|
+
}
|
|
111
|
+
);
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
The `idempotencyKeyTTL` controls how long the result is cached. Use shorter TTLs (like "1h") for time-sensitive operations, or longer ones (up to 30 days default) for expensive operations that rarely need re-execution. This prevents both unnecessary duplicate work and stale data issues.
|
|
115
|
+
|
|
116
|
+
## Do more work in parallel in a single task
|
|
117
|
+
|
|
118
|
+
Sometimes it's more efficient to do more work in a single task than split across many. This is particularly true when you're doing lots of async work such as API calls – most of the time is spent waiting, so it's an ideal candidate for doing calls in parallel inside the same task.
|
|
119
|
+
|
|
120
|
+
```ts
|
|
121
|
+
export const processItems = task({
|
|
122
|
+
id: "process-items",
|
|
123
|
+
run: async (payload: { items: string[] }) => {
|
|
124
|
+
// Process all items in parallel
|
|
125
|
+
const promises = payload.items.map((item) => processItem(item));
|
|
126
|
+
// This works very well for API calls
|
|
127
|
+
await Promise.all(promises);
|
|
128
|
+
},
|
|
129
|
+
});
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
## Don't needlessly retry
|
|
133
|
+
|
|
134
|
+
When an error is thrown in a task, your run will be automatically reattempted based on your [retry settings](/tasks/overview#retry-options).
|
|
135
|
+
|
|
136
|
+
Try setting lower `maxAttempts` for less critical tasks:
|
|
137
|
+
|
|
138
|
+
```ts
|
|
139
|
+
export const apiTask = task({
|
|
140
|
+
id: "api-task",
|
|
141
|
+
retry: {
|
|
142
|
+
maxAttempts: 2, // Don't retry forever
|
|
143
|
+
},
|
|
144
|
+
run: async (payload) => {
|
|
145
|
+
// API calls that might fail
|
|
146
|
+
},
|
|
147
|
+
});
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
This is very useful for intermittent errors, but if there's a permanent error you don't want to retry because you will just keep failing and waste compute. Use [AbortTaskRunError](/errors-retrying#using-aborttaskrunerror) to prevent a retry:
|
|
151
|
+
|
|
152
|
+
```ts
|
|
153
|
+
import { task, AbortTaskRunError } from "@trigger.dev/sdk";
|
|
154
|
+
|
|
155
|
+
export const someTask = task({
|
|
156
|
+
id: "some-task",
|
|
157
|
+
run: async (payload) => {
|
|
158
|
+
const result = await doSomething(payload);
|
|
159
|
+
|
|
160
|
+
if (!result.success) {
|
|
161
|
+
// This is a known permanent error, so don't retry
|
|
162
|
+
throw new AbortTaskRunError(result.error);
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
return result;
|
|
166
|
+
},
|
|
167
|
+
});
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
## Use appropriate maxDuration settings
|
|
171
|
+
|
|
172
|
+
Set realistic maxDurations to prevent runs from executing for too long:
|
|
173
|
+
|
|
174
|
+
```ts
|
|
175
|
+
export const boundedTask = task({
|
|
176
|
+
id: "bounded-task",
|
|
177
|
+
maxDuration: 300, // 5 minutes max
|
|
178
|
+
run: async (payload) => {
|
|
179
|
+
// Task will be terminated after 5 minutes
|
|
180
|
+
},
|
|
181
|
+
});
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
## Use waitpoints instead of polling
|
|
185
|
+
|
|
186
|
+
Waits longer than 5 seconds automatically checkpoint your task, meaning you don't pay for compute while waiting. Use `wait.for()`, `wait.until()`, or `triggerAndWait()` instead of polling loops.
|
|
187
|
+
|
|
188
|
+
```ts
|
|
189
|
+
import { task, wait } from "@trigger.dev/sdk";
|
|
190
|
+
|
|
191
|
+
export const waitpointTask = task({
|
|
192
|
+
id: "waitpoint-task",
|
|
193
|
+
run: async (payload) => {
|
|
194
|
+
// This wait is free - your task is checkpointed
|
|
195
|
+
await wait.for({ minutes: 5 });
|
|
196
|
+
|
|
197
|
+
// Parent is also checkpointed while waiting for child tasks
|
|
198
|
+
const result = await childTask.triggerAndWait({ data: payload });
|
|
199
|
+
return result;
|
|
200
|
+
},
|
|
201
|
+
});
|
|
202
|
+
```
|
|
203
|
+
|
|
204
|
+
[Read more about waitpoints](/wait-for).
|
|
205
|
+
|
|
206
|
+
## Use debounce to consolidate multiple triggers
|
|
207
|
+
|
|
208
|
+
When a task might be triggered multiple times in quick succession, use debounce to consolidate them into a single run. This is useful for document indexing, webhook aggregation, cache invalidation, and real-time sync scenarios.
|
|
209
|
+
|
|
210
|
+
```ts
|
|
211
|
+
// Multiple rapid triggers consolidate into 1 run
|
|
212
|
+
await updateIndex.trigger(
|
|
213
|
+
{ docId: "doc-123" },
|
|
214
|
+
{ debounce: { key: "doc-123", delay: "5s" } }
|
|
215
|
+
);
|
|
216
|
+
|
|
217
|
+
// Use trailing mode to process the most recent payload
|
|
218
|
+
await processUpdate.trigger(
|
|
219
|
+
{ version: 2 },
|
|
220
|
+
{ debounce: { key: "update-123", delay: "10s", mode: "trailing" } }
|
|
221
|
+
);
|
|
222
|
+
```
|
|
223
|
+
|
|
224
|
+
[Read more about debounce](/triggering#debounce).
|