Skip to Content

*.job.ts

Each *.job.ts file is a background job. Server code queues it with enqueue, and manh.run runs it later and retries it on failure.

app/invoices/ ├── repository.ts ├── send.job.ts # enqueue("app/invoices/send.job.ts", …) └── [id:int]/ └── send/ └── post.ts # answers at once, the job sends the invoice

What it does

A request has to answer fast. Slow work (mailing many people, calling AI, processing many rows, exporting a big file) moves into a job: the handler writes the main change, calls enqueue with ids, then returns its response. The file can live in any folder, usually next to the repository.ts of the resource it works on.

Exports

  • default: an async function that receives the payload passed to enqueue. Its return value is not used.

Example

app/invoices/send.job.ts
import { email } from "manh/email"; import { getInvoice, markSent } from "./repository"; export default async function (payload: { invoiceId: number }) { const inv = await getInvoice(payload.invoiceId); if (inv.sentAt) return; // already sent by an earlier run await email({ to: inv.customerEmail, subject: `Invoice ${inv.code}`, html: renderInvoice(inv) }); await markSent(inv.id); }
app/invoices/[id:int]/send/post.ts
import { enqueue } from "manh/jobs"; export default async (c, { param }) => { enqueue("app/invoices/send.job.ts", { invoiceId: param.id }); return { queued: true }; };

Good to know

  • A job shares app/db and manh/* with handlers, but has no c and no user. Pass ids in the payload and load the data inside the job.
  • A failing job is retried, up to 3 attempts in total, 30 then 60 seconds apart. Jobs must be safe to run twice.
  • Schedule a job for later with delaySeconds, at most 30 days. For work on a fixed schedule, use *.cron.ts.
  • See runs and errors in the workspace under Operations › Background jobs.

Next steps

  • enqueue — parameters, delays and payload size limits.
  • *.cron.ts — work that repeats on a fixed schedule instead of per request.
Last updated