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
Prevent uncaught errors in the `onSuccess`, `onComplete`, and `onFailure` lifecycle hooks from failing attempts/runs.
6
+
7
+
Deprecated the `onStart` lifecycle hook (which only fires before the `run` function on the first attempt). Replaced with `onStartAttempt` that fires before the run function on every attempt:
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.
<Info>The `onStartAttempt` function was introduced in v4.1.0</Info>
300
253
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.
254
+
Before a task run attempt starts, the `onStartAttempt` function is called. It's useful for sending notifications, logging, and other side effects.
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()`.
381
+
382
+
```tsinit.ts
383
+
import { tasks } from"@trigger.dev/sdk";
384
+
385
+
tasks.onComplete(({ ctx, payload, output }) => {
386
+
console.log("Task completed", ctx.task.id);
387
+
});
388
+
```
389
+
390
+
<Info>
391
+
Errors thrown in the `onComplete` function will be ignored, but you will still be able to see them
392
+
in the dashboard.
393
+
</Info>
394
+
400
395
### `onFailure` function
401
396
402
397
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.
411
+
You can also define a global `onFailure` function using `tasks.onFailure()`.
417
412
418
-
```tstrigger.config.ts
419
-
import { defineConfig } from"@trigger.dev/sdk";
413
+
```tsinit.ts
414
+
import { tasks } from"@trigger.dev/sdk";
420
415
421
-
exportdefaultdefineConfig({
422
-
project: "proj_1234",
423
-
onFailure: async ({ payload, error, ctx }) => {
424
-
console.log("Task failed", ctx.task.id);
425
-
},
416
+
tasks.onFailure(({ ctx, payload, error }) => {
417
+
console.log("Task failed", ctx.task.id);
426
418
});
427
419
```
428
420
429
-
<Info>Errors thrown in the `onFailure` function are ignored.</Info>
421
+
<Info>
422
+
Errors thrown in the `onFailure` function will be ignored, but you will still be able to see them
423
+
in the dashboard.
424
+
</Info>
430
425
431
426
<Note>
432
427
`onFailure` doesn’t fire for some of the run statuses like `Crashed`, `Systemfailures`, and
@@ -441,7 +436,7 @@ Read more about `catchError` in our [Errors and Retrying guide](/errors-retrying
441
436
442
437
<Info>Uncaught errors will throw a special internal error of the type `HANDLE_ERROR_ERROR`.</Info>
443
438
444
-
### onCancel
439
+
### `onCancel` function
445
440
446
441
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.
595
+
596
+
```ts/trigger/init-return.ts
597
+
exportconsttaskWithInitReturn=task({
598
+
id: "task-with-init-return",
599
+
init: async ({ payload, ctx }) => {
600
+
return { someData: "someValue" };
601
+
},
602
+
run: async (payload:any, { ctx, init }) => {
603
+
console.log(init.someData); // "someValue"
604
+
},
605
+
});
606
+
```
607
+
608
+
<Info>Errors thrown in the `init` function will cause the attempt to fail.</Info>
609
+
610
+
### `cleanup` function (deprecated)
611
+
612
+
<Warning>
613
+
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.
618
+
619
+
```ts/trigger/cleanup.ts
620
+
exportconsttaskWithCleanup=task({
621
+
id: "task-with-cleanup",
622
+
cleanup: async ({ payload, ctx }) => {
623
+
//...
624
+
},
625
+
run: async (payload:any, { ctx }) => {
626
+
//...
627
+
},
628
+
});
629
+
```
630
+
631
+
<Info>Errors thrown in the `cleanup` function will cause the attempt to fail.</Info>
0 commit comments