|
2 | 2 | "@trigger.dev/sdk": patch
|
3 | 3 | ---
|
4 | 4 |
|
5 |
| -Prevent onSuccess, onComplete, and onFailure lifecycle hooks from failing attempts/runs |
| 5 | +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: |
| 8 | + |
| 9 | +```ts |
| 10 | +export const taskWithOnStartAttempt = task({ |
| 11 | + id: "task-with-on-start-attempt", |
| 12 | + onStartAttempt: async ({ payload, ctx }) => { |
| 13 | + //... |
| 14 | + }, |
| 15 | + run: async (payload: any, { ctx }) => { |
| 16 | + //... |
| 17 | + }, |
| 18 | +}); |
| 19 | + |
| 20 | +// Default a global lifecycle hook using tasks |
| 21 | +tasks.onStartAttempt(({ ctx, payload, task }) => { |
| 22 | + console.log( |
| 23 | + `Run ${ctx.run.id} started on task ${task} attempt ${ctx.run.attempt.number}`, |
| 24 | + ctx.run |
| 25 | + ); |
| 26 | +}); |
| 27 | +``` |
| 28 | + |
| 29 | +If you want to execute code before just the first attempt, you can use the `onStartAttempt` function and check `ctx.run.attempt.number === 1`: |
| 30 | + |
| 31 | +```ts /trigger/on-start-attempt.ts |
| 32 | +export const taskWithOnStartAttempt = task({ |
| 33 | + id: "task-with-on-start-attempt", |
| 34 | + onStartAttempt: async ({ payload, ctx }) => { |
| 35 | + if (ctx.run.attempt.number === 1) { |
| 36 | + console.log("Run started on attempt 1", ctx.run); |
| 37 | + } |
| 38 | + }, |
| 39 | +}); |
| 40 | +``` |
| 41 | + |
0 commit comments