Skip to content

Commit b3cebee

Browse files
committed
docs: add task-level and config-level TTL documentation
1 parent c0b6309 commit b3cebee

File tree

4 files changed

+68
-1
lines changed

4 files changed

+68
-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. 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: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,12 +153,35 @@ 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+
162185
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.
163186

164187
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
@@ -136,6 +136,22 @@ export const longTask = task({
136136

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

139+
### `ttl` option
140+
141+
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.
142+
143+
```ts /trigger/time-sensitive-task.ts
144+
export const timeSensitiveTask = task({
145+
id: "time-sensitive-task",
146+
ttl: "10m", // Also accepts a number of seconds, e.g. 600
147+
run: async (payload: any, { ctx }) => {
148+
//...
149+
},
150+
});
151+
```
152+
153+
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). See [Time-to-live (TTL)](/runs#time-to-live-ttl) for more information.
154+
139155
## Global lifecycle hooks
140156

141157
<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)