You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
You can also return data from the `init` function that will be available in the params of the `run`, `cleanup`, `onSuccess`, and `onFailure` functions.
194
-
195
-
```ts /trigger/init-return.ts
196
-
exportconst taskWithInitReturn =task({
197
-
id: "task-with-init-return",
198
-
init: async ({ payload, ctx }) => {
199
-
return { someData: "someValue" };
200
-
},
201
-
run: async (payload:any, { ctx, init }) => {
202
-
console.log(init.someData); // "someValue"
203
-
},
204
-
});
205
-
```
206
-
207
-
<Info>Errors thrown in the `init` function are ignored.</Info>
208
-
209
-
### `cleanup` function
210
-
211
-
This function is called after the `run` function is executed, regardless of whether the run was successful or not. It's useful for cleaning up resources, logging, or other side effects.
212
-
213
-
```ts /trigger/cleanup.ts
214
-
exportconst taskWithCleanup =task({
215
-
id: "task-with-cleanup",
216
-
cleanup: async ({ payload, ctx }) => {
217
-
//...
218
-
},
219
-
run: async (payload:any, { ctx }) => {
220
-
//...
221
-
},
222
-
});
223
-
```
224
-
225
-
<Info>Errors thrown in the `cleanup` function will fail the attempt.</Info>
226
-
227
177
### `middleware` and `locals` functions
228
178
229
179
Our task middleware system runs at the top level, executing before and after all lifecycle hooks. This allows you to wrap the entire task execution lifecycle with custom logic.
230
180
231
181
<Info>
232
182
An error thrown in `middleware` is just like an uncaught error in the run function: it will
233
-
propagate through to `catchError()` function and then will fail the attempt (causing a retry).
183
+
propagate through to `catchError()` function and then will fail the attempt (either causing a
184
+
retry or failing the run).
234
185
</Info>
235
186
236
187
The `locals` API allows you to share data between middleware and hooks.
@@ -298,7 +249,12 @@ export const myTask = task({
298
249
299
250
### `onStart` function
300
251
301
-
When a task run starts, the `onStart` function is called. It's useful for sending notifications, logging, and other side effects. This function will only be called one per run (not per retry). If you want to run code before each retry, use the `init` function.
252
+
When a task run starts, the `onStart` function is called. It's useful for sending notifications, logging, and other side effects.
253
+
254
+
<Warning>
255
+
This function will only be called once per run (not per attempt). If you want to run code before
When a task run succeeds, the `onSuccess` function is called. It's useful for sending notifications, logging, syncing state to your database, or other side effects.
You can also define a global `onComplete` function using `tasks.onComplete()`.
368
+
369
+
```tsinit.ts
370
+
import { tasks } from"@trigger.dev/sdk";
371
+
372
+
tasks.onComplete(({ ctx, payload, output }) => {
373
+
console.log("Task completed", ctx.task.id);
374
+
});
375
+
```
376
+
377
+
<Info>
378
+
Errors thrown in the `onComplete` function will be ignored, but you will still be able to see them
379
+
in the dashboard.
380
+
</Info>
381
+
400
382
### `onFailure` function
401
383
402
384
When a task run fails, the `onFailure` function is called. It's useful for sending notifications, logging, or other side effects. It will only be executed once the task run has exhausted all its retries.
You can also define an `onFailure` function in your `trigger.config.ts` file to get notified when any task fails.
398
+
You can also define a global `onFailure` function using `tasks.onFailure()`.
417
399
418
-
```tstrigger.config.ts
419
-
import { defineConfig } from"@trigger.dev/sdk";
400
+
```tsinit.ts
401
+
import { tasks } from"@trigger.dev/sdk";
420
402
421
-
exportdefaultdefineConfig({
422
-
project: "proj_1234",
423
-
onFailure: async ({ payload, error, ctx }) => {
424
-
console.log("Task failed", ctx.task.id);
425
-
},
403
+
tasks.onFailure(({ ctx, payload, error }) => {
404
+
console.log("Task failed", ctx.task.id);
426
405
});
427
406
```
428
407
429
-
<Info>Errors thrown in the `onFailure` function are ignored.</Info>
408
+
<Info>
409
+
Errors thrown in the `onFailure` function will be ignored, but you will still be able to see them
410
+
in the dashboard.
411
+
</Info>
430
412
431
413
<Note>
432
414
`onFailure` doesn’t fire for some of the run statuses like `Crashed`, `Systemfailures`, and
@@ -441,7 +423,7 @@ Read more about `catchError` in our [Errors and Retrying guide](/errors-retrying
441
423
442
424
<Info>Uncaught errors will throw a special internal error of the type `HANDLE_ERROR_ERROR`.</Info>
443
425
444
-
### onCancel
426
+
### `onCancel` function
445
427
446
428
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.
You can also return data from the `init` function that will be available in the params of the `run`, `cleanup`, `onSuccess`, and `onFailure` functions.
547
+
548
+
```ts/trigger/init-return.ts
549
+
exportconsttaskWithInitReturn=task({
550
+
id: "task-with-init-return",
551
+
init: async ({ payload, ctx }) => {
552
+
return { someData: "someValue" };
553
+
},
554
+
run: async (payload:any, { ctx, init }) => {
555
+
console.log(init.someData); // "someValue"
556
+
},
557
+
});
558
+
```
559
+
560
+
<Info>Errors thrown in the `init` function will cause the attempt to fail.</Info>
561
+
562
+
### `cleanup` function (deprecated)
563
+
564
+
<Warning>
565
+
The `cleanup` hook is deprecated and will be removed in the future. Use
This function is called after the `run` function is executed, regardless of whether the run was successful or not. It's useful for cleaning up resources, logging, or other side effects.
570
+
571
+
```ts/trigger/cleanup.ts
572
+
exportconsttaskWithCleanup=task({
573
+
id: "task-with-cleanup",
574
+
cleanup: async ({ payload, ctx }) => {
575
+
//...
576
+
},
577
+
run: async (payload:any, { ctx }) => {
578
+
//...
579
+
},
580
+
});
581
+
```
582
+
583
+
<Info>Errors thrown in the `cleanup` function will cause the attempt to fail.</Info>
0 commit comments