diff --git a/docs/config/config-file.mdx b/docs/config/config-file.mdx
index b8d7c4ff0b..93fe8359a6 100644
--- a/docs/config/config-file.mdx
+++ b/docs/config/config-file.mdx
@@ -152,6 +152,21 @@ export default defineConfig({
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.
+## Max duration
+
+You can set the default `maxDuration` for all tasks in your project:
+
+```ts trigger.config.ts
+import { defineConfig } from "@trigger.dev/sdk/v3";
+
+export default defineConfig({
+ //..other stuff
+ maxDuration: 60, // 60 seconds
+});
+```
+
+See our [maxDuration guide](/runs/max-duration) for more information.
+
## Build configuration
You can customize the build process using the `build` option:
diff --git a/docs/mint.json b/docs/mint.json
index b6312b9a32..6e02c96d68 100644
--- a/docs/mint.json
+++ b/docs/mint.json
@@ -1,7 +1,10 @@
{
"$schema": "https://mintlify.com/schema.json",
"name": "Trigger.dev",
- "openapi": ["/openapi.yml", "/v3-openapi.yaml"],
+ "openapi": [
+ "/openapi.yml",
+ "/v3-openapi.yaml"
+ ],
"api": {
"playground": {
"mode": "simple"
@@ -103,26 +106,41 @@
"navigation": [
{
"group": "Getting Started",
- "pages": ["introduction", "quick-start", "how-it-works", "upgrading-beta", "limits"]
+ "pages": [
+ "introduction",
+ "quick-start",
+ "how-it-works",
+ "upgrading-beta",
+ "limits"
+ ]
},
{
"group": "Fundamentals",
"pages": [
{
"group": "Tasks",
- "pages": ["tasks/overview", "tasks/scheduled"]
+ "pages": [
+ "tasks/overview",
+ "tasks/scheduled"
+ ]
},
"triggering",
"apikeys",
{
"group": "Configuration",
- "pages": ["config/config-file", "config/extensions/overview"]
+ "pages": [
+ "config/config-file",
+ "config/extensions/overview"
+ ]
}
]
},
{
"group": "Development",
- "pages": ["cli-dev", "run-tests"]
+ "pages": [
+ "cli-dev",
+ "run-tests"
+ ]
},
{
"group": "Deployment",
@@ -132,7 +150,9 @@
"github-actions",
{
"group": "Deployment integrations",
- "pages": ["vercel-integration"]
+ "pages": [
+ "vercel-integration"
+ ]
}
]
},
@@ -144,16 +164,22 @@
"errors-retrying",
{
"group": "Wait",
- "pages": ["wait", "wait-for", "wait-until", "wait-for-event", "wait-for-request"]
+ "pages": [
+ "wait",
+ "wait-for",
+ "wait-until",
+ "wait-for-event",
+ "wait-for-request"
+ ]
},
"queue-concurrency",
"versioning",
"machines",
"idempotency",
"replaying",
+ "runs/max-duration",
"tags",
"runs/metadata",
- "notifications",
"run-usage"
]
},
@@ -163,7 +189,10 @@
"management/overview",
{
"group": "Tasks API",
- "pages": ["management/tasks/trigger", "management/tasks/batch-trigger"]
+ "pages": [
+ "management/tasks/trigger",
+ "management/tasks/batch-trigger"
+ ]
},
{
"group": "Runs API",
@@ -202,7 +231,9 @@
},
{
"group": "Projects API",
- "pages": ["management/projects/runs"]
+ "pages": [
+ "management/projects/runs"
+ ]
}
]
},
@@ -248,7 +279,11 @@
},
{
"group": "Help",
- "pages": ["community", "help-slack", "help-email"]
+ "pages": [
+ "community",
+ "help-slack",
+ "help-email"
+ ]
},
{
"group": "Frameworks",
@@ -272,7 +307,10 @@
},
{
"group": "Guides",
- "pages": ["guides/frameworks/prisma", "guides/frameworks/sequin"]
+ "pages": [
+ "guides/frameworks/prisma",
+ "guides/frameworks/sequin"
+ ]
},
{
"group": "Example tasks",
@@ -295,11 +333,15 @@
},
{
"group": "Dashboard",
- "pages": ["guides/dashboard/creating-a-project"]
+ "pages": [
+ "guides/dashboard/creating-a-project"
+ ]
},
{
"group": "Migrations",
- "pages": ["guides/use-cases/upgrading-from-v2"]
+ "pages": [
+ "guides/use-cases/upgrading-from-v2"
+ ]
}
],
"footerSocials": {
@@ -307,4 +349,4 @@
"github": "https://github.com/triggerdotdev",
"linkedin": "https://www.linkedin.com/company/triggerdotdev"
}
-}
+}
\ No newline at end of file
diff --git a/docs/runs/max-duration-error.png b/docs/runs/max-duration-error.png
new file mode 100644
index 0000000000..fffda40295
Binary files /dev/null and b/docs/runs/max-duration-error.png differ
diff --git a/docs/runs/max-duration.mdx b/docs/runs/max-duration.mdx
new file mode 100644
index 0000000000..88529df85d
--- /dev/null
+++ b/docs/runs/max-duration.mdx
@@ -0,0 +1,138 @@
+---
+title: "Max duration"
+sidebarTitle: "Max duration"
+description: "Set a maximum duration for a task to run."
+---
+
+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:
+
+- Across all your tasks in the [config](/config/config-file#max-duration)
+- On a specific task
+- On a specific run when you [trigger a task](/triggering#maxduration)
+
+## How it works
+
+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:
+
+- `wait.for` calls
+- `triggerAndWait` calls
+- `batchTriggerAndWait` calls
+
+You can inspect the CPU time of a task inside the run function with our `usage` utility:
+
+```ts /trigger/max-duration.ts
+import { task, usage } from "@trigger.dev/sdk/v3";
+
+export const maxDurationTask = task({
+ id: "max-duration-task",
+ maxDuration: 300, // 300 seconds or 5 minutes
+ run: async (payload: any, { ctx }) => {
+ let currentUsage = usage.getCurrent();
+
+ currentUsage.attempt.durationMs; // The CPU time in milliseconds since the start of the run
+ },
+});
+```
+
+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:
+
+
+
+The minimum maxDuration is 5 seconds. The maximum is ~68 years.
+
+## Configuring a default max duration
+
+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.
+
+```ts /config/default-max-duration.ts
+import { defineConfig } from "@trigger.dev/sdk/v3";
+
+export default defineConfig({
+ //Your project ref (you can see it on the Project settings page in the dashboard)
+ project: "proj_gtcwttqhhtlasxgfuhxs",
+ maxDuration: 60, // 60 seconds or 1 minute
+});
+```
+
+## Configuring for a task
+
+You can set a `maxDuration` on a specific task:
+
+```ts /trigger/max-duration-task.ts
+import { task } from "@trigger.dev/sdk/v3";
+
+export const maxDurationTask = task({
+ id: "max-duration-task",
+ maxDuration: 300, // 300 seconds or 5 minutes
+ run: async (payload: any, { ctx }) => {
+ //...
+ },
+});
+```
+
+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.
+
+You can "turn off" the Max duration set in your config file for a specific task like so:
+
+```ts /trigger/max-duration-task.ts
+import { task, timeout } from "@trigger.dev/sdk/v3";
+
+export const maxDurationTask = task({
+ id: "max-duration-task",
+ maxDuration: timeout.None, // No max duration
+ run: async (payload: any, { ctx }) => {
+ //...
+ },
+});
+```
+
+## Configuring for a run
+
+You can set a `maxDuration` on a specific run when you trigger a task:
+
+```ts /trigger/max-duration.ts
+import { maxDurationTask } from "./trigger/max-duration-task";
+
+// Trigger the task with a maxDuration of 300 seconds
+const run = await maxDurationTask.trigger(
+ { foo: "bar" },
+ {
+ maxDuration: 300, // 300 seconds or 5 minutes
+ }
+);
+```
+
+You can also set the `maxDuration` to `timeout.None` to turn off the max duration for a specific run:
+
+```ts /trigger/max-duration.ts
+import { maxDurationTask } from "./trigger/max-duration-task";
+import { timeout } from "@trigger.dev/sdk/v3";
+
+// Trigger the task with no maxDuration
+const run = await maxDurationTask.trigger(
+ { foo: "bar" },
+ {
+ maxDuration: timeout.None, // No max duration
+ }
+);
+```
+
+## maxDuration in run context
+
+You can access the `maxDuration` set for a run in the run context:
+
+```ts /trigger/max-duration-task.ts
+import { task } from "@trigger.dev/sdk/v3";
+
+export const maxDurationTask = task({
+ id: "max-duration-task",
+ maxDuration: 300, // 300 seconds or 5 minutes
+ run: async (payload: any, { ctx }) => {
+ console.log(ctx.run.maxDuration); // 300
+ },
+});
+```
+
+## maxDuration and lifecycle functions
+
+When a task run exceeds the `maxDuration`, the lifecycle functions `cleanup`, `onSuccess`, and `onFailure` will not be called.
diff --git a/docs/tasks/overview.mdx b/docs/tasks/overview.mdx
index 521bc6ff41..cb99c900f1 100644
--- a/docs/tasks/overview.mdx
+++ b/docs/tasks/overview.mdx
@@ -121,6 +121,22 @@ export const heavyTask = task({
});
```
+### `maxDuration` option
+
+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` on a task, and all runs of that task will be stopped if they exceed the duration.
+
+```ts /trigger/long-task.ts
+export const longTask = task({
+ id: "long-task",
+ maxDuration: 300, // 300 seconds or 5 minutes
+ run: async (payload: any, { ctx }) => {
+ //...
+ },
+});
+```
+
+See our [maxDuration guide](/runs/max-duration) for more information.
+
## Lifecycle functions

@@ -155,9 +171,7 @@ export const taskWithInitReturn = task({
});
```
-
-Errors thrown in the `init` function are ignored.
-
+Errors thrown in the `init` function are ignored.
### `cleanup` function
@@ -175,16 +189,15 @@ export const taskWithCleanup = task({
});
```
-
-Errors thrown in the `cleanup` function will fail the attempt.
-
+Errors thrown in the `cleanup` function will fail the attempt.
### `middleware` function
This function is called before the `run` function, it allows you to wrap the run function with custom code.
-An error thrown in `middleware` is just like an uncaught error in the run function: it will propagate through to `handleError()` and then will fail the attempt (causing a retry).
+ An error thrown in `middleware` is just like an uncaught error in the run function: it will
+ propagate through to `handleError()` and then will fail the attempt (causing a retry).
### `onStart` function
@@ -216,9 +229,7 @@ export default defineConfig({
});
```
-
-Errors thrown in the `onStart` function are ignored.
-
+Errors thrown in the `onStart` function are ignored.
### `onSuccess` function
@@ -249,9 +260,7 @@ export default defineConfig({
});
```
-
-Errors thrown in the `onSuccess` function are ignored.
-
+Errors thrown in the `onSuccess` function are ignored.
### `onFailure` function
@@ -282,9 +291,7 @@ export default defineConfig({
});
```
-
-Errors thrown in the `onFailure` function are ignored.
-
+Errors thrown in the `onFailure` function are ignored.
### `handleError` functions
@@ -292,9 +299,7 @@ You can define a function that will be called when an error is thrown in the `ru
Read more about `handleError` in our [Errors and Retrying guide](/errors-retrying).
-
-Uncaught errors will throw a special internal error of the type `HANDLE_ERROR_ERROR`.
-
+Uncaught errors will throw a special internal error of the type `HANDLE_ERROR_ERROR`.
## Next steps
diff --git a/docs/triggering.mdx b/docs/triggering.mdx
index 7a042be562..c8d524fb6e 100644
--- a/docs/triggering.mdx
+++ b/docs/triggering.mdx
@@ -742,6 +742,10 @@ View our [tags doc](/tags) for more information.
View our [metadata doc](/runs/metadata) for more information.
+### `maxDuration`
+
+View our [maxDuration doc](/runs/max-duration) for more information.
+
## Large Payloads
We recommend keeping your task payloads as small as possible. We currently have a hard limit on task payloads above 10MB.
diff --git a/packages/cli-v3/templates/examples/schedule.mjs.template b/packages/cli-v3/templates/examples/schedule.mjs.template
index 6d7c686c0a..d3d3d75fb9 100644
--- a/packages/cli-v3/templates/examples/schedule.mjs.template
+++ b/packages/cli-v3/templates/examples/schedule.mjs.template
@@ -4,6 +4,7 @@ export const firstScheduledTask = schedules.task({
id: "first-scheduled-task",
//every hour
cron: "0 * * * *",
+ maxDuration: 300, // 5 minutes
run: async (payload, { ctx }) => {
// The payload contains the last run timestamp that you can use to check if this is the first run
// And calculate the time since the last run
diff --git a/packages/cli-v3/templates/examples/schedule.ts.template b/packages/cli-v3/templates/examples/schedule.ts.template
index 6d7c686c0a..d3d3d75fb9 100644
--- a/packages/cli-v3/templates/examples/schedule.ts.template
+++ b/packages/cli-v3/templates/examples/schedule.ts.template
@@ -4,6 +4,7 @@ export const firstScheduledTask = schedules.task({
id: "first-scheduled-task",
//every hour
cron: "0 * * * *",
+ maxDuration: 300, // 5 minutes
run: async (payload, { ctx }) => {
// The payload contains the last run timestamp that you can use to check if this is the first run
// And calculate the time since the last run
diff --git a/packages/cli-v3/templates/examples/simple.mjs.template b/packages/cli-v3/templates/examples/simple.mjs.template
index 23267ac6b5..0bb4fce272 100644
--- a/packages/cli-v3/templates/examples/simple.mjs.template
+++ b/packages/cli-v3/templates/examples/simple.mjs.template
@@ -2,6 +2,7 @@ import { logger, task, wait } from "@trigger.dev/sdk/v3";
export const helloWorldTask = task({
id: "hello-world",
+ maxDuration: 300, // 5 minutes
run: async (payload, { ctx }) => {
logger.log("Hello, world!", { payload, ctx });
diff --git a/packages/cli-v3/templates/examples/simple.ts.template b/packages/cli-v3/templates/examples/simple.ts.template
index de2b316097..b6986d0bba 100644
--- a/packages/cli-v3/templates/examples/simple.ts.template
+++ b/packages/cli-v3/templates/examples/simple.ts.template
@@ -2,6 +2,7 @@ import { logger, task, wait } from "@trigger.dev/sdk/v3";
export const helloWorldTask = task({
id: "hello-world",
+ maxDuration: 300, // 5 minutes
run: async (payload: any, { ctx }) => {
logger.log("Hello, world!", { payload, ctx });
diff --git a/packages/cli-v3/templates/trigger.config.mjs.template b/packages/cli-v3/templates/trigger.config.mjs.template
index 7b5c122ddd..b170cae171 100644
--- a/packages/cli-v3/templates/trigger.config.mjs.template
+++ b/packages/cli-v3/templates/trigger.config.mjs.template
@@ -4,6 +4,8 @@ export default defineConfig({
project: "${projectRef}",
runtime: "${runtime}",
logLevel: "log",
+ // Set the maxDuration to 300 seconds for all tasks. See https://trigger.dev/docs/runs/max-duration
+ // maxDuration: 300,
retries: {
enabledInDev: true,
default: {
diff --git a/packages/cli-v3/templates/trigger.config.ts.template b/packages/cli-v3/templates/trigger.config.ts.template
index 7b5c122ddd..b170cae171 100644
--- a/packages/cli-v3/templates/trigger.config.ts.template
+++ b/packages/cli-v3/templates/trigger.config.ts.template
@@ -4,6 +4,8 @@ export default defineConfig({
project: "${projectRef}",
runtime: "${runtime}",
logLevel: "log",
+ // Set the maxDuration to 300 seconds for all tasks. See https://trigger.dev/docs/runs/max-duration
+ // maxDuration: 300,
retries: {
enabledInDev: true,
default: {
diff --git a/references/v3-catalog/trigger.config.ts b/references/v3-catalog/trigger.config.ts
index 92350853d3..fe6ae1200f 100644
--- a/references/v3-catalog/trigger.config.ts
+++ b/references/v3-catalog/trigger.config.ts
@@ -17,7 +17,8 @@ export default defineConfig({
machine: "medium-1x",
instrumentations: [new OpenAIInstrumentation()],
additionalFiles: ["wrangler/wrangler.toml"],
- maxDuration: 60,
+ // Set the maxDuration to 300s for all tasks. See https://trigger.dev/docs/runs/max-duration
+ // maxDuration: 300,
retries: {
enabledInDev: false,
default: {