Skip to content

Commit 1caec27

Browse files
authored
docs: Max duration (#1379)
* maxDuration docs * Update the init command to set the maxDuration and include a commented out maxDuration in the config file
1 parent e14c954 commit 1caec27

13 files changed

+248
-35
lines changed

docs/config/config-file.mdx

+15
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,21 @@ export default defineConfig({
152152

153153
The `logLevel` only determines which logs are sent to the Trigger.dev instance when using the `logger` API. All `console` based logs are always sent.
154154

155+
## Max duration
156+
157+
You can set the default `maxDuration` for all tasks in your project:
158+
159+
```ts trigger.config.ts
160+
import { defineConfig } from "@trigger.dev/sdk/v3";
161+
162+
export default defineConfig({
163+
//..other stuff
164+
maxDuration: 60, // 60 seconds
165+
});
166+
```
167+
168+
See our [maxDuration guide](/runs/max-duration) for more information.
169+
155170
## Build configuration
156171

157172
You can customize the build process using the `build` option:

docs/mint.json

+57-15
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
{
22
"$schema": "https://mintlify.com/schema.json",
33
"name": "Trigger.dev",
4-
"openapi": ["/openapi.yml", "/v3-openapi.yaml"],
4+
"openapi": [
5+
"/openapi.yml",
6+
"/v3-openapi.yaml"
7+
],
58
"api": {
69
"playground": {
710
"mode": "simple"
@@ -103,26 +106,41 @@
103106
"navigation": [
104107
{
105108
"group": "Getting Started",
106-
"pages": ["introduction", "quick-start", "how-it-works", "upgrading-beta", "limits"]
109+
"pages": [
110+
"introduction",
111+
"quick-start",
112+
"how-it-works",
113+
"upgrading-beta",
114+
"limits"
115+
]
107116
},
108117
{
109118
"group": "Fundamentals",
110119
"pages": [
111120
{
112121
"group": "Tasks",
113-
"pages": ["tasks/overview", "tasks/scheduled"]
122+
"pages": [
123+
"tasks/overview",
124+
"tasks/scheduled"
125+
]
114126
},
115127
"triggering",
116128
"apikeys",
117129
{
118130
"group": "Configuration",
119-
"pages": ["config/config-file", "config/extensions/overview"]
131+
"pages": [
132+
"config/config-file",
133+
"config/extensions/overview"
134+
]
120135
}
121136
]
122137
},
123138
{
124139
"group": "Development",
125-
"pages": ["cli-dev", "run-tests"]
140+
"pages": [
141+
"cli-dev",
142+
"run-tests"
143+
]
126144
},
127145
{
128146
"group": "Deployment",
@@ -132,7 +150,9 @@
132150
"github-actions",
133151
{
134152
"group": "Deployment integrations",
135-
"pages": ["vercel-integration"]
153+
"pages": [
154+
"vercel-integration"
155+
]
136156
}
137157
]
138158
},
@@ -144,16 +164,22 @@
144164
"errors-retrying",
145165
{
146166
"group": "Wait",
147-
"pages": ["wait", "wait-for", "wait-until", "wait-for-event", "wait-for-request"]
167+
"pages": [
168+
"wait",
169+
"wait-for",
170+
"wait-until",
171+
"wait-for-event",
172+
"wait-for-request"
173+
]
148174
},
149175
"queue-concurrency",
150176
"versioning",
151177
"machines",
152178
"idempotency",
153179
"replaying",
180+
"runs/max-duration",
154181
"tags",
155182
"runs/metadata",
156-
"notifications",
157183
"run-usage"
158184
]
159185
},
@@ -163,7 +189,10 @@
163189
"management/overview",
164190
{
165191
"group": "Tasks API",
166-
"pages": ["management/tasks/trigger", "management/tasks/batch-trigger"]
192+
"pages": [
193+
"management/tasks/trigger",
194+
"management/tasks/batch-trigger"
195+
]
167196
},
168197
{
169198
"group": "Runs API",
@@ -202,7 +231,9 @@
202231
},
203232
{
204233
"group": "Projects API",
205-
"pages": ["management/projects/runs"]
234+
"pages": [
235+
"management/projects/runs"
236+
]
206237
}
207238
]
208239
},
@@ -248,7 +279,11 @@
248279
},
249280
{
250281
"group": "Help",
251-
"pages": ["community", "help-slack", "help-email"]
282+
"pages": [
283+
"community",
284+
"help-slack",
285+
"help-email"
286+
]
252287
},
253288
{
254289
"group": "Frameworks",
@@ -272,7 +307,10 @@
272307
},
273308
{
274309
"group": "Guides",
275-
"pages": ["guides/frameworks/prisma", "guides/frameworks/sequin"]
310+
"pages": [
311+
"guides/frameworks/prisma",
312+
"guides/frameworks/sequin"
313+
]
276314
},
277315
{
278316
"group": "Example tasks",
@@ -295,16 +333,20 @@
295333
},
296334
{
297335
"group": "Dashboard",
298-
"pages": ["guides/dashboard/creating-a-project"]
336+
"pages": [
337+
"guides/dashboard/creating-a-project"
338+
]
299339
},
300340
{
301341
"group": "Migrations",
302-
"pages": ["guides/use-cases/upgrading-from-v2"]
342+
"pages": [
343+
"guides/use-cases/upgrading-from-v2"
344+
]
303345
}
304346
],
305347
"footerSocials": {
306348
"twitter": "https://twitter.com/triggerdotdev",
307349
"github": "https://github.com/triggerdotdev",
308350
"linkedin": "https://www.linkedin.com/company/triggerdotdev"
309351
}
310-
}
352+
}

docs/runs/max-duration-error.png

29 KB
Loading

docs/runs/max-duration.mdx

+138
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
---
2+
title: "Max duration"
3+
sidebarTitle: "Max duration"
4+
description: "Set a maximum duration for a task to run."
5+
---
6+
7+
By default tasks can execute indefinitely, which can be great! But you also might want to set a `maxDuration` to prevent a task from running too long. You can set the `maxDuration` for a run in the following ways:
8+
9+
- Across all your tasks in the [config](/config/config-file#max-duration)
10+
- On a specific task
11+
- On a specific run when you [trigger a task](/triggering#maxduration)
12+
13+
## How it works
14+
15+
The `maxDuration` is set in seconds, and is compared to the CPU time elapsed since the start of a single execution (which we call attempts) of the task. The CPU time is the time that the task has been actively running on the CPU, and does not include time spent waiting during the following:
16+
17+
- `wait.for` calls
18+
- `triggerAndWait` calls
19+
- `batchTriggerAndWait` calls
20+
21+
You can inspect the CPU time of a task inside the run function with our `usage` utility:
22+
23+
```ts /trigger/max-duration.ts
24+
import { task, usage } from "@trigger.dev/sdk/v3";
25+
26+
export const maxDurationTask = task({
27+
id: "max-duration-task",
28+
maxDuration: 300, // 300 seconds or 5 minutes
29+
run: async (payload: any, { ctx }) => {
30+
let currentUsage = usage.getCurrent();
31+
32+
currentUsage.attempt.durationMs; // The CPU time in milliseconds since the start of the run
33+
},
34+
});
35+
```
36+
37+
The above value will be compared to the `maxDuration` you set. If the task exceeds the `maxDuration`, it will be stopped with the following error:
38+
39+
![Max duration error](/runs/max-duration-error.png)
40+
41+
<Note>The minimum maxDuration is 5 seconds. The maximum is ~68 years.</Note>
42+
43+
## Configuring a default max duration
44+
45+
You can set a default `maxDuration` for all tasks in your [config file](/config/config-file#default-machine). This will apply to all tasks unless you override it on a specific task or run.
46+
47+
```ts /config/default-max-duration.ts
48+
import { defineConfig } from "@trigger.dev/sdk/v3";
49+
50+
export default defineConfig({
51+
//Your project ref (you can see it on the Project settings page in the dashboard)
52+
project: "proj_gtcwttqhhtlasxgfuhxs",
53+
maxDuration: 60, // 60 seconds or 1 minute
54+
});
55+
```
56+
57+
## Configuring for a task
58+
59+
You can set a `maxDuration` on a specific task:
60+
61+
```ts /trigger/max-duration-task.ts
62+
import { task } from "@trigger.dev/sdk/v3";
63+
64+
export const maxDurationTask = task({
65+
id: "max-duration-task",
66+
maxDuration: 300, // 300 seconds or 5 minutes
67+
run: async (payload: any, { ctx }) => {
68+
//...
69+
},
70+
});
71+
```
72+
73+
This will override the default `maxDuration` set in the config file. If you have a config file with a default `maxDuration` of 60 seconds, and you set a `maxDuration` of 300 seconds on a task, the task will run for 300 seconds.
74+
75+
You can "turn off" the Max duration set in your config file for a specific task like so:
76+
77+
```ts /trigger/max-duration-task.ts
78+
import { task, timeout } from "@trigger.dev/sdk/v3";
79+
80+
export const maxDurationTask = task({
81+
id: "max-duration-task",
82+
maxDuration: timeout.None, // No max duration
83+
run: async (payload: any, { ctx }) => {
84+
//...
85+
},
86+
});
87+
```
88+
89+
## Configuring for a run
90+
91+
You can set a `maxDuration` on a specific run when you trigger a task:
92+
93+
```ts /trigger/max-duration.ts
94+
import { maxDurationTask } from "./trigger/max-duration-task";
95+
96+
// Trigger the task with a maxDuration of 300 seconds
97+
const run = await maxDurationTask.trigger(
98+
{ foo: "bar" },
99+
{
100+
maxDuration: 300, // 300 seconds or 5 minutes
101+
}
102+
);
103+
```
104+
105+
You can also set the `maxDuration` to `timeout.None` to turn off the max duration for a specific run:
106+
107+
```ts /trigger/max-duration.ts
108+
import { maxDurationTask } from "./trigger/max-duration-task";
109+
import { timeout } from "@trigger.dev/sdk/v3";
110+
111+
// Trigger the task with no maxDuration
112+
const run = await maxDurationTask.trigger(
113+
{ foo: "bar" },
114+
{
115+
maxDuration: timeout.None, // No max duration
116+
}
117+
);
118+
```
119+
120+
## maxDuration in run context
121+
122+
You can access the `maxDuration` set for a run in the run context:
123+
124+
```ts /trigger/max-duration-task.ts
125+
import { task } from "@trigger.dev/sdk/v3";
126+
127+
export const maxDurationTask = task({
128+
id: "max-duration-task",
129+
maxDuration: 300, // 300 seconds or 5 minutes
130+
run: async (payload: any, { ctx }) => {
131+
console.log(ctx.run.maxDuration); // 300
132+
},
133+
});
134+
```
135+
136+
## maxDuration and lifecycle functions
137+
138+
When a task run exceeds the `maxDuration`, the lifecycle functions `cleanup`, `onSuccess`, and `onFailure` will not be called.

0 commit comments

Comments
 (0)