|
| 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 | + |
| 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