//this Job sends an event that triggers the second job
client.defineJob({
id: "job-1",
name: "First job",
version: "0.0.1",
trigger: cronTrigger({
cron: "0 9 * * *", // 9am every day (UTC)
}),
run: async (payload, io, ctx) => {
//sends the "new.user" event with a userId in the payload
await io.sendEvent("send-event", {
name: "new.user",
payload: {
userId: "u_1234567890",
},
});
},
});
client.defineJob({
id: "job-2",
name: "Second job",
version: "0.0.1",
//subscribes to the "new.user" event
trigger: eventTrigger({
name: "new.user",
schema: z.object({
userId: z.string(),
}),
}),
run: async (payload, io, ctx) => {
await io.logger.log("New user created", { userId: payload.userId });
//do stuff with the new user
},
});
io.sendEvent()
allows you to send an event from inside a Job run. The sent event will trigger any Jobs that are listening for that event (based on the name).
//this Job sends an event that triggers the second job
client.defineJob({
id: "job-1",
name: "First job",
version: "0.0.1",
trigger: cronTrigger({
cron: "0 9 * * *", // 9am every day (UTC)
}),
run: async (payload, io, ctx) => {
//sends the "new.user" event with a userId in the payload
await io.sendEvent("send-event", {
name: "new.user",
payload: {
userId: "u_1234567890",
},
});
},
});
client.defineJob({
id: "job-2",
name: "Second job",
version: "0.0.1",
//subscribes to the "new.user" event
trigger: eventTrigger({
name: "new.user",
schema: z.object({
userId: z.string(),
}),
}),
run: async (payload, io, ctx) => {
await io.logger.log("New user created", { userId: payload.userId });
//do stuff with the new user
},
});
run()
. See
resumability for more information.Hide properties
name
property must exactly match any subscriptions you want to
trigger.payload
property will be sent to any matching Jobs and will appear
as the payload
param of the run()
function. You can leave this
parameter out if you just want to trigger a Job without any input data.context
property will be sent to any matching Jobs and will
be passed through as the context.event.context
param of the run()
function. This is optional but can be useful if you want to pass through
some additional context to the Job.id
property uniquely identify this particular event. If unset it
will be set automatically using ulid
.eventTrigger()
.Hide properties
Hide properties
id
of the event that was sent.name
of the event that was sent.payload
of the event that was senttimestamp
of the event that was sentcontext
of the event that was sent. Is undefined
if no context was
set when sending the event.undefined
if deliverAt
or deliverAfter
wasn't set when sending the
event.undefined
if deliverAt
or deliverAfter
were set when sending the event.//this Job sends an event that triggers the second job
client.defineJob({
id: "job-1",
name: "First job",
version: "0.0.1",
trigger: cronTrigger({
cron: "0 9 * * *", // 9am every day (UTC)
}),
run: async (payload, io, ctx) => {
//sends the "new.user" event with a userId in the payload
await io.sendEvent("send-event", {
name: "new.user",
payload: {
userId: "u_1234567890",
},
});
},
});
client.defineJob({
id: "job-2",
name: "Second job",
version: "0.0.1",
//subscribes to the "new.user" event
trigger: eventTrigger({
name: "new.user",
schema: z.object({
userId: z.string(),
}),
}),
run: async (payload, io, ctx) => {
await io.logger.log("New user created", { userId: payload.userId });
//do stuff with the new user
},
});
Was this page helpful?