Background jobs
Slow work handed off so pages stay fast: sending emails, resizing images, imports. Failed jobs retry.
In stackly.json and the CLI it is queues. Its use, live or draft, is counted as background runs; see Live app prices.
queues · optional · needs backend · Queues (one queue per app environment) · binding APP_JOBS
Slow work handed off so pages stay fast: sending emails, resizing images, imports. Failed jobs retry.
Turn it on
"features": ["backend", "queues"]
Use it
Send a job from any server code; it returns at once:
import { enqueue } from "#/lib/jobs.server";
await enqueue("send-receipt", { orderId }); // now
await enqueue("remind", { orderId }, { delaySeconds: 3600 }); // in an hour
Handle it by name in src/server/tasks.ts:
export const tasks: TaskHandlers = {
jobs: {
"send-receipt": async (payload: { orderId: string }, context) => {
const order = await loadOrder(payload.orderId);
if (order.receiptSent) return; // safe to run twice
await sendEmail({ to: order.email, subject: "Your receipt", text: "…" });
},
},
schedules: {},
workflows: {},
};
- Stackly delivers each job to
/api/stackly/task; visitors get a 404 there. Leavesrc/lib/tasks.server.tsand the route as they are. - A handler that throws is retried up to 5 times (after 30 s, 1, 2 and 4 minutes). Write handlers that are safe to
run twice.
context.attemptsays which try it is.
Limits
Payloads are JSON of at most 64 KB: pass ids, not records. delaySeconds up to 12 hours. 10,000 jobs a day. 10 run at
a time per app; a handler gets 60 s, so split longer work into several jobs or a workflow.
Locally
stackly dev runs jobs on your machine.
Related
scheduled-tasks for a timetable, workflows for several steps with waits.