Skip to content

Defining Jobs

Use job() to define a job and createJobRouter() to group them into a router.

import { createJobRouter, job } from "@queuebase/nextjs"; // or @queuebase/node
import { z } from "zod";
export const jobs = createJobRouter({
myJob: job({
input: z.object({ /* Zod schema */ }),
handler: async (ctx) => { /* returns output */ },
defaults: { /* optional EnqueueOptions */ },
}),
});

The handler receives a JobContext object:

FieldTypeDescription
inputInferred from Zod schemaValidated input data
jobIdstringUnique job ID
attemptnumberCurrent attempt (1-indexed)
maxAttemptsnumberMaximum attempts allowed
handler: async ({ input, jobId, attempt, maxAttempts }) => {
console.log(`Job ${jobId}, attempt ${attempt}/${maxAttempts}`);
// use input (fully typed from your Zod schema)
return { success: true };
}

You can set default enqueue options per job via the defaults field. These are merged with (and overridden by) options passed to .enqueue().

sendEmail: job({
input: z.object({ to: z.string().email() }),
handler: async ({ input }) => {
// ...
return { sent: true };
},
defaults: {
retries: 3,
backoff: "exponential",
backoffDelay: 2000,
},
}),

See Enqueueing Jobs for the full list of options.