Background jobs
How slow work like emails is queued and done by the worker, without making people wait.
Some work is too slow to do while someone waits, like emailing everyone going to an event. The API writes that work down as a job, and a small separate program, the worker, does it.
The life of a job
What the worker does today
| Job | What it does |
|---|---|
| Sends event emails, up to 100 in one go. | |
| View totals | Adds up organization profile views per day for the dashboard. |
| Clean-up | Deletes finished jobs after a week and failed ones after a month. |
Good to know
- The worker sleeps when there is nothing to do, and wakes when the API calls it, when it starts, or once an hour as a safety net.
- A job never runs twice by accident: the email service ignores a repeat of the same batch.
- There is a daily email limit. Emails over it wait for the next day.
- Event reminders, automatic waitlist moves and other timed jobs are not built yet.
- Transactional outbox: services call
transactionWithEmailsandenqueueEmail, which insert intojobsinside the same transaction; after the commitwakeWorker()callsPOST /wakewith a bearer secret. - Claim: one
UPDATEoverSELECT ... FOR UPDATE SKIP LOCKED LIMIT 1, adding an attempt and movingrun_attwo minutes ahead as a lease. Two jobs run at once, each with a one-minute timeout. - Kinds:
email({"messages":[{"to","subject","html"}]}, 1 to 100) andanalytics_rollup(deduplicated per 10 minutes withdedupe_key). - Status:
queued,done,dead. To rerun a dead job setstatustoqueuedandattemptsto 0. - Worker endpoints:
POST /wake(202) andGET /healthz(no database). Hourly safety net:worker-wake.yml.
| Piece | Where |
|---|---|
| Queue (API) | job.service.ts |
| Worker | apps/worker/, runner.go, queue.go |
| Spec | docs/specs/worker.md |