-
-
Notifications
You must be signed in to change notification settings - Fork 728
docs: Max duration #1379
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
docs: Max duration #1379
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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: | ||
|
||
 | ||
|
||
<Note>The minimum maxDuration is 5 seconds. The maximum is ~68 years.</Note> | ||
|
||
## 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. | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.