Skip to content

Commit 54d22e9

Browse files
authored
docs: add per-task middleware section to tasks overview (#3197)
1 parent 8b4ac45 commit 54d22e9

File tree

1 file changed

+28
-6
lines changed

1 file changed

+28
-6
lines changed

docs/tasks/overview.mdx

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ You can override this per-trigger by passing `ttl` in the trigger options, or se
168168

169169
You can register global lifecycle hooks that are executed for all runs, regardless of the task. While you can still define these in the `trigger.config.ts` file, you can also register them anywhere in your codebase:
170170

171-
```ts
171+
```typescript
172172
import { tasks } from "@trigger.dev/sdk";
173173

174174
tasks.onStartAttempt(({ ctx, payload, task }) => {
@@ -262,7 +262,7 @@ tasks.onResume("db", async ({ ctx, payload, task }) => {
262262

263263
You can access the database client using `getDb()` in your tasks `run` function and all your hooks (global or task specific):
264264

265-
```ts
265+
```typescript
266266
import { getDb } from "./db";
267267

268268
export const myTask = task({
@@ -273,6 +273,28 @@ export const myTask = task({
273273
});
274274
```
275275

276+
#### Per-task middleware
277+
278+
You can also define middleware per task by passing the `middleware` option in the task definition. This runs after global middleware and before the `run` function. Use it when only specific tasks need certain locals or setup:
279+
280+
```typescript
281+
import { task, locals } from "@trigger.dev/sdk";
282+
283+
const myLocal = locals.create<string>("myLocal");
284+
285+
export const myTask = task({
286+
id: "my-task",
287+
middleware: async ({ payload, ctx, next }) => {
288+
locals.set(myLocal, "some-value");
289+
await next();
290+
},
291+
run: async (payload) => {
292+
const value = locals.getOrThrow(myLocal);
293+
// ...
294+
},
295+
});
296+
```
297+
276298
### `onStartAttempt` function
277299

278300
<Info>The `onStartAttempt` function was introduced in v4.1.0</Info>
@@ -323,7 +345,7 @@ export const taskWithOnStartAttempt = task({
323345

324346
These lifecycle hooks allow you to run code when a run is paused or resumed because of a wait:
325347

326-
```ts
348+
```typescript
327349
export const myTask = task({
328350
id: "my-task",
329351
onWait: async ({ wait }) => {
@@ -466,15 +488,15 @@ Read more about `catchError` in our [Errors and Retrying guide](/errors-retrying
466488
467489
You can define an `onCancel` hook that is called when a run is cancelled. This is useful if you want to clean up any resources that were allocated for the run.
468490
469-
```ts
491+
```typescript
470492
tasks.onCancel(({ ctx, signal }) => {
471493
console.log("Run cancelled", signal);
472494
});
473495
```
474496
475497
You can use the `onCancel` hook along with the `signal` passed into the run function to interrupt a call to an external service, for example using the [streamText](https://ai-sdk.dev/docs/reference/ai-sdk-core/stream-text) function from the AI SDK:
476498
477-
```ts
499+
```typescript
478500
import { logger, tasks, schemaTask } from "@trigger.dev/sdk";
479501
import { streamText } from "ai";
480502
import { z } from "zod";
@@ -528,7 +550,7 @@ export const interruptibleChat = schemaTask({
528550
529551
The `onCancel` hook can optionally wait for the `run` function to finish, and access the output of the run:
530552
531-
```ts
553+
```typescript
532554
import { logger, task } from "@trigger.dev/sdk";
533555
import { setTimeout } from "node:timers/promises";
534556

0 commit comments

Comments
 (0)