Skip to content

Commit 4d7fbf0

Browse files
authored
docs: add task-level and config-level TTL documentation (#3200)
Documents TTL support at task-level and config-level. Companion to #3196 - merge after new packages are released.
1 parent 5ea36e0 commit 4d7fbf0

File tree

4 files changed

+91
-1
lines changed

4 files changed

+91
-1
lines changed

docs/config/config-file.mdx

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,22 @@ export default defineConfig({
350350

351351
See our [maxDuration guide](/runs/max-duration) for more information.
352352

353+
## TTL
354+
355+
You can set a default time-to-live (TTL) for all task runs in your project. If a run is not dequeued within this time, it will expire and never execute.
356+
357+
```ts trigger.config.ts
358+
import { defineConfig } from "@trigger.dev/sdk";
359+
360+
export default defineConfig({
361+
project: "<project ref>",
362+
// Your other config settings...
363+
ttl: "1h", // Also accepts a number of seconds, e.g. 3600
364+
});
365+
```
366+
367+
You can override this on a per-task basis by setting `ttl` on the task definition, or per-trigger by passing `ttl` in the trigger options. To opt a specific task out of the config-level TTL, set `ttl: 0` on the task. See [Time-to-live (TTL)](/runs#time-to-live-ttl) for more information.
368+
353369
## Process keep alive
354370

355371
Keep the process alive after the task has finished running so the next task doesn't have to wait for the process to start up again.

docs/runs.mdx

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,12 +153,58 @@ When a run is canceled:
153153

154154
### Time-to-live (TTL)
155155

156-
TTL is a time-to-live setting that defines the maximum duration a run can remain in a queued state before being automatically expired. You can set a TTL when triggering a run:
156+
TTL is a time-to-live setting that defines the maximum duration a run can remain in a queued state before being automatically expired. You can set a TTL at three levels, with the following precedence:
157+
158+
1. **Per-trigger** (highest priority):
157159

158160
```ts
159161
await yourTask.trigger({ foo: "bar" }, { ttl: "10m" });
160162
```
161163

164+
2. **Task-level default**:
165+
166+
```ts
167+
export const myTask = task({
168+
id: "my-task",
169+
ttl: "10m",
170+
run: async (payload) => {
171+
//...
172+
},
173+
});
174+
```
175+
176+
3. **Global config default** (lowest priority):
177+
178+
```ts trigger.config.ts
179+
export default defineConfig({
180+
project: "<project ref>",
181+
ttl: "1h",
182+
});
183+
```
184+
185+
To opt out of a config-level or task-level TTL for a specific trigger, pass `ttl: 0`:
186+
187+
```ts
188+
// This run will not have a TTL, even if the task or config defines one
189+
await yourTask.trigger({ foo: "bar" }, { ttl: 0 });
190+
```
191+
192+
Similarly, to opt out of a config-level TTL for a specific task, set `ttl: 0` on the task definition:
193+
194+
```ts
195+
export const longRunningTask = task({
196+
id: "long-running-task",
197+
ttl: 0, // Opt out of the config-level TTL
198+
run: async (payload) => {
199+
//...
200+
},
201+
});
202+
```
203+
204+
<Note>
205+
Setting `ttl: 0` removes the config-level or task-level TTL, but the platform-level maximum still applies. See [Limits - Maximum run TTL](/limits#maximum-run-ttl) for details.
206+
</Note>
207+
162208
If the run hasn't started within the specified TTL, it will automatically expire, returning the status `Expired`. This is useful for time-sensitive tasks where immediate execution is important. For example, when you queue many runs simultaneously and exceed your concurrency limits, some runs might be delayed - using TTL ensures they only execute if they can start within your specified timeframe.
163209

164210
Dev runs automatically have a 10-minute TTL. On Trigger.dev Cloud, staging and production runs have a maximum TTL of 14 days applied automatically (runs without an explicit TTL get 14 days; longer TTLs are clamped). See [Limits — Maximum run TTL](/limits#maximum-run-ttl) for details.

docs/tasks/overview.mdx

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,22 @@ export const longTask = task({
146146

147147
See our [maxDuration guide](/runs/max-duration) for more information.
148148

149+
### `ttl` option
150+
151+
You can set a default time-to-live (TTL) on a task. If a run is not dequeued within this time, it will expire and never execute. This is useful for time-sensitive tasks where stale runs should be discarded.
152+
153+
```ts /trigger/time-sensitive-task.ts
154+
export const timeSensitiveTask = task({
155+
id: "time-sensitive-task",
156+
ttl: "10m", // Also accepts a number of seconds, e.g. 600
157+
run: async (payload: any, { ctx }) => {
158+
//...
159+
},
160+
});
161+
```
162+
163+
You can override this per-trigger by passing `ttl` in the trigger options, or set a project-wide default in your [trigger.config.ts](/config/config-file#ttl). Set `ttl: 0` to opt out of a config-level TTL. See [Time-to-live (TTL)](/runs#time-to-live-ttl) for more information.
164+
149165
## Global lifecycle hooks
150166

151167
<Note>When specifying global lifecycle hooks, we recommend using the `init.ts` file.</Note>

docs/tasks/scheduled.mdx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,18 @@ You can see from the comments that the payload has several useful properties:
6767

6868
Like all tasks they don't have timeouts, they should be placed inside a [/trigger folder](/config/config-file), and you [can configure them](/tasks/overview#defining-a-task).
6969

70+
You can set a [TTL](/runs#time-to-live-ttl) on a scheduled task to automatically expire runs that aren't dequeued in time. This is useful when a schedule fires while the previous run is still executing - rather than queueing up stale runs, they'll expire:
71+
72+
```ts
73+
export const frequentTask = schedules.task({
74+
id: "frequent-task",
75+
ttl: "5m",
76+
run: async (payload) => {
77+
//...
78+
},
79+
});
80+
```
81+
7082
## How to attach a schedule
7183

7284
Now that we've defined a scheduled task, we need to define when it will actually run. To do this we need to attach one or more schedules.

0 commit comments

Comments
 (0)