Skip to content

Commit 374edef

Browse files
authored
Updates the trigger, batchTrigger and their *AndWait variants to use the first parameter for the payload/items, and the second parameter for options (#1045)
Also always returns a `TaskRunResult` object from `triggerAndWait` instead of rethrowing subtask errors in the parent
1 parent b82db67 commit 374edef

18 files changed

+373
-322
lines changed

.changeset/shaggy-spoons-taste.md

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
---
2+
"@trigger.dev/sdk": patch
3+
"@trigger.dev/core": patch
4+
---
5+
6+
Updates the `trigger`, `batchTrigger` and their `*AndWait` variants to use the first parameter for the payload/items, and the second parameter for options.
7+
8+
Before:
9+
10+
```ts
11+
await yourTask.trigger({ payload: { foo: "bar" }, options: { idempotencyKey: "key_1234" } });
12+
await yourTask.triggerAndWait({ payload: { foo: "bar" }, options: { idempotencyKey: "key_1234" } });
13+
14+
await yourTask.batchTrigger({ items: [{ payload: { foo: "bar" } }, { payload: { foo: "baz" } }] });
15+
await yourTask.batchTriggerAndWait({ items: [{ payload: { foo: "bar" } }, { payload: { foo: "baz" } }] });
16+
```
17+
18+
After:
19+
20+
```ts
21+
await yourTask.trigger({ foo: "bar" }, { idempotencyKey: "key_1234" });
22+
await yourTask.triggerAndWait({ foo: "bar" }, { idempotencyKey: "key_1234" });
23+
24+
await yourTask.batchTrigger([{ payload: { foo: "bar" } }, { payload: { foo: "baz" } }]);
25+
await yourTask.batchTriggerAndWait([{ payload: { foo: "bar" } }, { payload: { foo: "baz" } }]);
26+
```
27+
28+
We've also changed the API of the `triggerAndWait` result. Before, if the subtask that was triggered finished with an error, we would automatically "rethrow" the error in the parent task.
29+
30+
Now instead we're returning a `TaskRunResult` object that allows you to discriminate between successful and failed runs in the subtask:
31+
32+
Before:
33+
34+
```ts
35+
try {
36+
const result = await yourTask.triggerAndWait({ foo: "bar" });
37+
38+
// result is the output of your task
39+
console.log("result", result);
40+
41+
} catch (error) {
42+
// handle subtask errors here
43+
}
44+
```
45+
46+
After:
47+
48+
```ts
49+
const result = await yourTask.triggerAndWait({ foo: "bar" });
50+
51+
if (result.ok) {
52+
console.log(`Run ${result.id} succeeded with output`, result.output);
53+
} else {
54+
console.log(`Run ${result.id} failed with error`, result.error);
55+
}
56+
```

docs/v3/errors-retrying.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export const myTask = task({
3535
maxAttempts: 10,
3636
},
3737
run: async (payload: string) => {
38-
const result = await otherTask.triggerAndWait({ payload: "some data" });
38+
const result = await otherTask.triggerAndWait("some data");
3939
//...do other stuff
4040
},
4141
});

docs/v3/migration-defer.mdx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,15 +71,15 @@ export async function runLongRunningTask() {
7171
}
7272
```
7373

74-
In Trigger.dev your logic goes in the `run` function of a task. You can then `trigger` and `batchTrigger` that task, with a payload and options.
74+
In Trigger.dev your logic goes in the `run` function of a task. You can then `trigger` and `batchTrigger` that task, with a payload as the first argument.
7575

7676
```ts /app/actions/actions.ts
7777
"use server";
7878

7979
import { longRunningTask } from "@/trigger/someTasks";
8080

8181
export async function runLongRunningTask() {
82-
return await longRunningTask.trigger({ payload: { foo: "bar" } });
82+
return await longRunningTask.trigger({ foo: "bar" });
8383
}
8484
```
8585

@@ -243,7 +243,7 @@ export const longRunningTask = task({
243243
import { longRunningTask } from "@/trigger/longRunningTask";
244244

245245
export async function runLongRunningTask() {
246-
return await longRunningTask.trigger({ payload: { foo: "bar" } });
246+
return await longRunningTask.trigger({ foo: "bar" });
247247
}
248248
```
249249

docs/v3/queue-concurrency.mdx

Lines changed: 19 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -107,24 +107,19 @@ export async function POST(request: Request) {
107107

108108
if (data.branch === "main") {
109109
//trigger the task, with a different queue
110-
const handle = await generatePullRequest.trigger({
111-
payload: data,
112-
options: {
113-
queue: {
114-
//the "main-branch" queue will have a concurrency limit of 10
115-
//this triggered run will use that queue
116-
name: "main-branch",
117-
concurrencyLimit: 10,
118-
},
110+
const handle = await generatePullRequest.trigger(data, {
111+
queue: {
112+
//the "main-branch" queue will have a concurrency limit of 10
113+
//this triggered run will use that queue
114+
name: "main-branch",
115+
concurrencyLimit: 10,
119116
},
120117
});
121118

122119
return Response.json(handle);
123120
} else {
124121
//triggered with the default (concurrency of 1)
125-
const handle = await generatePullRequest.trigger({
126-
payload: data,
127-
});
122+
const handle = await generatePullRequest.trigger(data);
128123
return Response.json(handle);
129124
}
130125
}
@@ -146,32 +141,26 @@ export async function POST(request: Request) {
146141

147142
if (data.isFreeUser) {
148143
//free users can only have 1 PR generated at a time
149-
const handle = await generatePullRequest.trigger({
150-
payload: data,
151-
options: {
152-
queue: {
153-
//every free user gets a queue with a concurrency limit of 1
154-
name: "free-users",
155-
concurrencyLimit: 1,
156-
},
157-
concurrencyKey: data.userId,
144+
const handle = await generatePullRequest.trigger(data, {
145+
queue: {
146+
//every free user gets a queue with a concurrency limit of 1
147+
name: "free-users",
148+
concurrencyLimit: 1,
158149
},
150+
concurrencyKey: data.userId,
159151
});
160152

161153
//return a success response with the handle
162154
return Response.json(handle);
163155
} else {
164156
//trigger the task, with a different queue
165-
const handle = await generatePullRequest.trigger({
166-
payload: data,
167-
options: {
168-
queue: {
169-
//every paid user gets a queue with a concurrency limit of 10
170-
name: "paid-users",
171-
concurrencyLimit: 10,
172-
},
173-
concurrencyKey: data.userId,
157+
const handle = await generatePullRequest.trigger(data, {
158+
queue: {
159+
//every paid user gets a queue with a concurrency limit of 10
160+
name: "paid-users",
161+
concurrencyLimit: 10,
174162
},
163+
concurrencyKey: data.userId,
175164
});
176165

177166
//return a success response with the handle

docs/v3/tasks-overview.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ import { helloWorldTask } from "./trigger/hello-world";
3737

3838
async function triggerHelloWorld() {
3939
//This triggers the task and return a handle
40-
const handle = await helloWorld.trigger({ payload: { message: "Hello world!" } });
40+
const handle = await helloWorld.trigger({ message: "Hello world!" });
4141

4242
//You can use the handle to check the status of the task, cancel and retry it.
4343
console.log("Task is running with handle", handle.id);

docs/v3/triggering.mdx

Lines changed: 36 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ export async function POST(request: Request) {
4848
const data = await request.json();
4949

5050
//trigger your task
51-
const handle = await emailSequence.trigger({ payload: { to: data.email, name: data.name } });
51+
const handle = await emailSequence.trigger({ to: data.email, name: data.name });
5252

5353
//return a success response with the handle
5454
return Response.json(handle);
@@ -67,7 +67,7 @@ export async function action({ request, params }: ActionFunctionArgs) {
6767
const data = await request.json();
6868

6969
//trigger your task
70-
const handle = await emailSequence.trigger({ payload: { to: data.email, name: data.name } });
70+
const handle = await emailSequence.trigger({ to: data.email, name: data.name });
7171

7272
//return a success response with the handle
7373
return json(handle);
@@ -91,9 +91,9 @@ export async function POST(request: Request) {
9191
const data = await request.json();
9292

9393
//batch trigger your task
94-
const batchHandle = await emailSequence.batchTrigger({
95-
items: data.users.map((u) => ({ payload: { to: u.email, name: u.name } })),
96-
});
94+
const batchHandle = await emailSequence.batchTrigger(
95+
data.users.map((u) => ({ payload: { to: u.email, name: u.name } }))
96+
);
9797

9898
//return a success response with the handle
9999
return Response.json(batchHandle);
@@ -112,9 +112,9 @@ export async function action({ request, params }: ActionFunctionArgs) {
112112
const data = await request.json();
113113

114114
//batch trigger your task
115-
const batchHandle = await emailSequence.batchTrigger({
116-
items: data.users.map((u) => ({ payload: { to: u.email, name: u.name } })),
117-
});
115+
const batchHandle = await emailSequence.batchTrigger(
116+
data.users.map((u) => ({ payload: { to: u.email, name: u.name } }))
117+
);
118118

119119
//return a success response with the handle
120120
return json(batchHandle);
@@ -137,7 +137,7 @@ import { myOtherTask } from "~/trigger/my-other-task";
137137
export const myTask = task({
138138
id: "my-task",
139139
run: async (payload: string) => {
140-
const handle = await myOtherTask.trigger({ payload: "some data" });
140+
const handle = await myOtherTask.trigger("some data");
141141

142142
//...do other stuff
143143
},
@@ -154,7 +154,7 @@ import { myOtherTask } from "~/trigger/my-other-task";
154154
export const myTask = task({
155155
id: "my-task",
156156
run: async (payload: string) => {
157-
const batchHandle = await myOtherTask.batchTrigger({ items: [{ payload: "some data" }] });
157+
const batchHandle = await myOtherTask.batchTrigger([{ payload: "some data" }]);
158158

159159
//...do other stuff
160160
},
@@ -168,16 +168,18 @@ This is where it gets interesting. You can trigger a task and then wait for the
168168
<Accordion title="Don't use this in parallel, e.g. with `Promise.all()`">
169169
Instead, use `batchTriggerAndWait()` if you can, or a for loop if you can't.
170170

171-
To control concurrency using batch triggers, you can set `queue.concurrencyLimit` on the child task.
171+
To control concurrency using batch triggers, you can set `queue.concurrencyLimit` on the child task.
172172

173173
<CodeGroup>
174+
174175
```ts /trigger/batch.ts
175176
export const batchTask = task({
176177
id: "batch-task",
177178
run: async (payload: string) => {
178-
const results = await childTask.batchTriggerAndWait({
179-
items: [{ payload: "item1" }, { payload: "item2" }],
180-
});
179+
const results = await childTask.batchTriggerAndWait([
180+
{ payload: "item1" },
181+
{ payload: "item2" },
182+
]);
181183
console.log("Results", results);
182184

183185
//...do stuff with the results
@@ -192,14 +194,15 @@ export const loopTask = task({
192194
//this will be slower than the batch version
193195
//as we have to resume the parent after each iteration
194196
for (let i = 0; i < 2; i++) {
195-
const result = await childTask.triggerAndWait({ payload: `item${i}` });
197+
const result = await childTask.triggerAndWait(`item${i}`);
196198
console.log("Result", result);
197199

198200
//...do stuff with the result
199201
}
200202
},
201203
});
202204
```
205+
203206
</CodeGroup>
204207

205208
</Accordion>
@@ -208,7 +211,7 @@ export const loopTask = task({
208211
export const parentTask = task({
209212
id: "parent-task",
210213
run: async (payload: string) => {
211-
const result = await batchChildTask.triggerAndWait({ payload: "some-data" });
214+
const result = await batchChildTask.triggerAndWait("some-data");
212215
console.log("Result", result);
213216

214217
//...do stuff with the result
@@ -223,16 +226,18 @@ You can batch trigger a task and wait for all the results. This is useful for th
223226
<Accordion title="Don't use this in parallel, e.g. with `Promise.all()`">
224227
Instead, pass in all items at once and set an appropriate `maxConcurrency`. Alternatively, use sequentially with a for loop.
225228

226-
To control concurrency, you can set `queue.concurrencyLimit` on the child task.
229+
To control concurrency, you can set `queue.concurrencyLimit` on the child task.
227230

228231
<CodeGroup>
232+
229233
```ts /trigger/batch.ts
230234
export const batchTask = task({
231235
id: "batch-task",
232236
run: async (payload: string) => {
233-
const results = await childTask.batchTriggerAndWait({
234-
items: [{ payload: "item1" }, { payload: "item2" }],
235-
});
237+
const results = await childTask.batchTriggerAndWait([
238+
{ payload: "item1" },
239+
{ payload: "item2" },
240+
]);
236241
console.log("Results", results);
237242

238243
//...do stuff with the results
@@ -247,16 +252,18 @@ export const loopTask = task({
247252
//this will be slower than a single batchTriggerAndWait()
248253
//as we have to resume the parent after each iteration
249254
for (let i = 0; i < 2; i++) {
250-
const result = await childTask.batchTriggerAndWait({
251-
items: [{ payload: `itemA${i}` }, { payload: `itemB${i}` }],
252-
});
255+
const result = await childTask.batchTriggerAndWait([
256+
{ payload: `itemA${i}` },
257+
{ payload: `itemB${i}` },
258+
]);
253259
console.log("Result", result);
254260

255261
//...do stuff with the result
256262
}
257263
},
258264
});
259265
```
266+
260267
</CodeGroup>
261268

262269
</Accordion>
@@ -265,9 +272,11 @@ export const loopTask = task({
265272
export const batchParentTask = task({
266273
id: "parent-task",
267274
run: async (payload: string) => {
268-
const results = await childTask.batchTriggerAndWait({
269-
items: [{ payload: "item4" }, { payload: "item5" }, { payload: "item6" }],
270-
});
275+
const results = await childTask.batchTriggerAndWait([
276+
{ payload: "item4" },
277+
{ payload: "item5" },
278+
{ payload: "item6" },
279+
]);
271280
console.log("Results", results);
272281

273282
//...do stuff with the result
@@ -326,9 +335,7 @@ import { createAvatar } from "@/trigger/create-avatar";
326335
export async function create() {
327336
try {
328337
const handle = await createAvatar.trigger({
329-
payload: {
330-
userImage: "http://...",
331-
},
338+
userImage: "http://...",
332339
});
333340

334341
return { handle };

docs/v3/upgrading-from-v2.mdx

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -164,9 +164,7 @@ We've unified triggering in v3. You use `trigger()` or `batchTrigger()` which yo
164164
async function yourBackendFunction() {
165165
//call `trigger()` on any task
166166
const handle = await openaiTask.trigger({
167-
payload: {
168-
prompt: "Tell me a programming joke",
169-
},
167+
prompt: "Tell me a programming joke",
170168
});
171169
}
172170
```

0 commit comments

Comments
 (0)