Skip to content

Commit 6170b3d

Browse files
committed
Add onStartAttempt hook and deprecate onStart hook
1 parent f8bce65 commit 6170b3d

File tree

7 files changed

+64
-6
lines changed

7 files changed

+64
-6
lines changed

.changeset/tiny-carrots-rest.md

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,41 @@
11
---
2-
"@trigger.dev/sdk": patch
2+
"@trigger.dev/sdk": minor
33
---
44

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+

packages/cli-v3/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,4 +155,4 @@
155155
}
156156
}
157157
}
158-
}
158+
}

packages/core/src/v3/lifecycle-hooks-api.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,5 @@ export type {
3535
TaskCancelHookParams,
3636
OnCancelHookFunction,
3737
AnyOnCancelHookFunction,
38+
AnyOnStartAttemptHookFunction,
3839
} from "./lifecycleHooks/types.js";

packages/core/src/v3/workers/taskExecutor.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1030,7 +1030,7 @@ export class TaskExecutor {
10301030
if (taskStartHook) {
10311031
const [hookError] = await tryCatch(
10321032
this._tracer.startActiveSpan(
1033-
"onStart()",
1033+
"onStartAttempt()",
10341034
async (span) => {
10351035
await taskStartHook({
10361036
payload,

packages/trigger-sdk/src/v3/hooks.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ import {
1212
type AnyOnCatchErrorHookFunction,
1313
type AnyOnMiddlewareHookFunction,
1414
type AnyOnCancelHookFunction,
15+
type AnyOnStartAttemptHookFunction,
1516
} from "@trigger.dev/core/v3";
16-
import { AnyOnStartAttemptHookFunction } from "../../../core/src/v3/lifecycleHooks/types.js";
1717

1818
export type {
1919
AnyOnStartHookFunction,

packages/trigger-sdk/src/v3/shared.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,8 @@ import type {
9090
TriggerAndWaitOptions,
9191
TriggerApiRequestOptions,
9292
TriggerOptions,
93+
AnyOnStartAttemptHookFunction,
9394
} from "@trigger.dev/core/v3";
94-
import { AnyOnStartAttemptHookFunction } from "../../../core/src/v3/lifecycleHooks/types.js";
9595

9696
export type {
9797
AnyRunHandle,

references/hello-world/src/trigger/example.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -507,3 +507,24 @@ export const throwErrorInInitHookTask = task({
507507
throw new Error("Forced error to cause a retry");
508508
},
509509
});
510+
511+
export const testStartAttemptHookTask = task({
512+
id: "test-start-attempt-hook",
513+
retry: {
514+
maxAttempts: 3,
515+
},
516+
run: async (payload: { message: string }, { ctx }) => {
517+
logger.info("Hello, world from the test start attempt hook task", { message: payload.message });
518+
519+
if (ctx.attempt.number === 1) {
520+
throw new Error("Forced error to cause a retry so we can test the onStartAttempt hook");
521+
}
522+
},
523+
onStartAttempt: async ({ payload, ctx }) => {
524+
console.log(`onStartAttempt hook called ${ctx.attempt.number}`);
525+
},
526+
});
527+
528+
tasks.onStartAttempt(({ payload, ctx }) => {
529+
console.log(`global onStartAttempt hook called ${ctx.attempt.number}`);
530+
});

0 commit comments

Comments
 (0)