Up until now, you could only trigger a new job run when sending an event to Trigger.dev using eventTrigger():
client.defineJob({ id: "payment-accepted", name: "Payment Accepted", version: "1.0.0", trigger: eventTrigger({ name: "payment.accepted", schema: z.object({ id: z.string(), amount: z.number(), currency: z.string(), userId: z.string(), }), }), run: async (payload, io, ctx) => { // Do something when a payment is accepted },});
Now with io.waitForEvent(), you can wait for an event to be sent in the middle of a job run:
const event = await io.waitForEvent("🤑", { name: "payment.accepted", schema: z.object({ id: z.string(), amount: z.number(), currency: z.string(), userId: z.string(), }), filter: { userId: ["user_1234"], // only wait for events from this specific user },});
By default, io.waitForEvent() will wait for 1 hour for an event to be sent. If no event is sent within that time, it will throw an error. You can customize the timeout by passing a second argument:
const event = await io.waitForEvent( "🤑", { name: "payment.accepted", schema: z.object({ id: z.string(), amount: z.number(), currency: z.string(), userId: z.string(), }), filter: { userId: ["user_1234"], // only wait for events from this specific user }, }, { timeoutInSeconds: 60 * 60 * 24 * 7, // wait for 1 week });
This will allow you to build more complex workflows that simply were not possible before, or were at least a pain to implement. We're excited to see what you build with this new feature!
Read more about it in the docs.
How to update
The trigger.dev/* packages are now at v2.2.6. You can update using the following command:
npx @trigger.dev/cli@latest update

