|
1 | | -import { logger, task, timeout, usage, wait } from "@trigger.dev/sdk/v3"; |
| 1 | +import { batch, logger, task, timeout, usage, wait } from "@trigger.dev/sdk/v3"; |
2 | 2 | import { setTimeout } from "timers/promises"; |
3 | 3 |
|
4 | 4 | export const helloWorldTask = task({ |
@@ -30,24 +30,54 @@ export const batchParentTask = task({ |
30 | 30 | id: "batch-parent", |
31 | 31 | run: async (payload: any, { ctx }) => { |
32 | 32 | logger.log("Hello, world from the parent", { payload }); |
33 | | - await childTask.batchTriggerAndWait([{ payload: { message: "Hello, world!" } }]); |
| 33 | + |
| 34 | + const results = await childTask.batchTriggerAndWait([ |
| 35 | + { payload: { message: "Hello, world!" } }, |
| 36 | + { payload: { message: "Hello, world 2!" } }, |
| 37 | + ]); |
| 38 | + logger.log("Results", { results }); |
| 39 | + |
| 40 | + const results2 = await batch.triggerAndWait<typeof childTask>([ |
| 41 | + { id: "child", payload: { message: "Hello, world !" } }, |
| 42 | + { id: "child", payload: { message: "Hello, world 2!" } }, |
| 43 | + ]); |
| 44 | + logger.log("Results 2", { results2 }); |
| 45 | + |
| 46 | + const results3 = await batch.triggerByTask([ |
| 47 | + { task: childTask, payload: { message: "Hello, world !" } }, |
| 48 | + { task: childTask, payload: { message: "Hello, world 2!" } }, |
| 49 | + ]); |
| 50 | + logger.log("Results 3", { results3 }); |
| 51 | + |
| 52 | + const results4 = await batch.triggerByTaskAndWait([ |
| 53 | + { task: childTask, payload: { message: "Hello, world !" } }, |
| 54 | + { task: childTask, payload: { message: "Hello, world 2!" } }, |
| 55 | + ]); |
| 56 | + logger.log("Results 4", { results4 }); |
34 | 57 | }, |
35 | 58 | }); |
36 | 59 |
|
37 | 60 | export const childTask = task({ |
38 | 61 | id: "child", |
39 | | - run: async (payload: any, { ctx }) => { |
40 | | - logger.info("Hello, world from the child", { payload }); |
| 62 | + run: async ( |
| 63 | + { message, failureChance = 0.3 }: { message?: string; failureChance?: number }, |
| 64 | + { ctx } |
| 65 | + ) => { |
| 66 | + logger.info("Hello, world from the child", { message, failureChance }); |
41 | 67 |
|
42 | | - if (Math.random() > 0.5) { |
| 68 | + if (Math.random() < failureChance) { |
43 | 69 | throw new Error("Random error at start"); |
44 | 70 | } |
45 | 71 |
|
46 | | - await setTimeout(10000); |
| 72 | + await setTimeout(3_000); |
47 | 73 |
|
48 | | - if (Math.random() > 0.5) { |
| 74 | + if (Math.random() < failureChance) { |
49 | 75 | throw new Error("Random error at end"); |
50 | 76 | } |
| 77 | + |
| 78 | + return { |
| 79 | + message, |
| 80 | + }; |
51 | 81 | }, |
52 | 82 | }); |
53 | 83 |
|
|
0 commit comments