Skip to Content

enqueue

Queue slow work to run later in a *.job.ts file, with retries on failure. The request answers at once instead of waiting.

Usage

app/invoices/[id:int]/send/post.ts
import { enqueue } from "manh/jobs"; import { markSending } from "../../repository"; export default async (c, { user, param }) => { const inv = await markSending(user, param.id); enqueue("app/invoices/send.job.ts", { invoiceId: inv.id }); // as soon as possible return inv; };
app/invoices/send.job.ts
import { email } from "manh/email"; import { getInvoice } from "./repository"; export default async function (payload: { invoiceId: number }) { const inv = await getInvoice(payload.invoiceId); await email({ to: inv.customerEmail, subject: `Invoice ${inv.code}`, html: renderInvoice(inv) }); }

Use it in .ts files only: handlers, crons, jobs.

Signature

manh/jobs
function enqueue(path: string, payload?: unknown, opts?: { delaySeconds?: number }): string | null;

Parameters

  • path: the full path of the job file, as app/…/<name>.job.ts. A different shape throws.
  • payload: JSON data for the job. Send ids only; the payloads of all jobs queued in one request are capped at 32 KB together.
  • opts.delaySeconds: how many seconds to wait before running, at most 30 days (2,592,000 seconds). When omitted, the job runs as soon as possible.

Returns

The job id (a string). The job is queued when the request returns its response. Called outside a request of the app, the function does nothing and returns null.

Examples

A due-date reminder in 3 days:

app/invoices/repository.ts
enqueue("app/reminders/due.job.ts", { invoiceId: inv.id }, { delaySeconds: 3 * 86400 });
  • A failing job (it throws or answers 5xx) is retried, up to 3 attempts in total, 30 then 60 seconds apart. A job whose file was removed in a later deploy fails at once, with no retry.
  • Because it can run again, a job must be safe to run twice.
  • See runs and errors in the workspace under Vận hành › Việc nền (Operations › Background jobs).

Next steps

Last updated