Skip to content

Background jobs

This page is for the operator who keeps Adiutor’s background work running. It covers the tick that starts the jobs, what each job does, the ways to start the tick, the endpoint a cron service calls, what happens when a job fails, and how to check on them.

Adiutor does some of its work in the background: cleaning up data nobody can use any more, keeping data only as long as it may be kept, reminding people of invitations, and sending emails that can wait a few minutes. Nothing starts these by itself. You have to start a tick regularly, or reminders, security emails and clean-ups wait.

A tick is one pass over the jobs. Start it every 5 minutes. Each time, Adiutor runs the scheduled jobs that are due, one after another, and then the queued jobs that are due, until it runs out of time. What’s left waits for the next tick.

  • A job is due when it has never run, or when its schedule named a moment since its last run started. After a long silence a job runs once, not once for every moment it missed.
  • A job never runs twice at once. A tick that overlaps the next one leaves a running job alone, so it’s safe to start a tick at any time.
  • A job a staff Admin paused is skipped by the tick. A few jobs that keep Adiutor secure can’t be paused.
  • A failing job doesn’t stop the others.

Scheduled jobs run on their own schedule, in UTC:

Job Schedule What it does
leftover-provider-tokens every 5 minutes Clears the sign-in tokens a failed GitHub or Google sign-in left behind, after 10 minutes
finished-sign-ins-as-users every 5 minutes Ends, and records, the staff sign-ins as a user whose time is up or whose staff session is gone
expired-sign-in-data hourly Deletes expired sign-in links, codes and sessions, ended links in emails about requests, and finished rate-limit windows
invitation-reminders hourly, at :10 Emails a reminder 48 hours before an invitation expires; what doesn’t fit goes in the next run
attachment-files hourly, at :25 Purges files uploaded but never sent with a text (after 25 hours), unfinished uploads (after an hour), and deletes removed files the storage still holds
attachment-previews every 5 minutes Draws the previews of sent files that have none, such as files attached before previews existed
missed-deadlines every 5 minutes Notes the SLA deadlines that passed: a line in the request’s conversation, and news for the webhooks that chose it. It emails nobody
retention daily, at 03:15 Deletes audit events after a year, errors after 7 days, and the history of job runs, finished queued jobs and deliveries to webhooks after 30 days

The attachment jobs do nothing while no file storage is chosen or its folder isn’t set up. They also run from a jobs container, so that container needs the same storage as the app: the same volume, for a folder on disk. See File storage.

Queued jobs are work the app hands off to run once, at the next tick:

Kind What it does
security-notice Emails a user about a security change on their account
added-to-company Emails a user that staff added them to a company
staff-admin-added Emails a staff Admin that someone became a staff Admin
smtp-changed Emails a staff Admin that the mail server changed, when that couldn’t go out through the previous one
request-update Emails the people on a request what happened to it, and tells the webhooks that want the news
request-deadlines Works the SLA deadlines of open requests out again after a change to an SLA, a company’s SLA, a company’s deactivation, the business hours, the closed days or the default time zone, 50 requests a run
webhook-delivery Tries a delivery to a webhook again after a failed attempt
webhook-turned-off Emails a staff Admin that Adiutor turned a webhook off because it kept failing

Pick the way that fits your host. All of them start the same tick.

With Docker Compose, a jobs service runs the tick on a schedule next to the app: see Run it with Docker Compose. Without Compose, start a one-off container from the host’s cron every 5 minutes. It must have the same settings and, for files on disk, the same volume as the app (leave out the two -e and the -v when the files aren’t on disk):

*/5 * * * * docker run --rm --env-file /srv/adiutor/.env -e ATTACHMENTS_STORAGE=local -e ATTACHMENTS_DIR=/data/attachments -v adiutor-attachments:/data/attachments bastolen/adiutor:0.5.0 pnpm jobs:run >> /var/log/adiutor-jobs.log 2>&1

The log holds personal data (see Logs and errors), so create it first, readable by root only: sudo install -m 600 /dev/null /var/log/adiutor-jobs.log.

On any host you can also have a cron service call the endpoint instead of running the command.

GET /api/jobs runs one tick. Send the header Authorization: Bearer <CRON_SECRET>. Set CRON_SECRET to a random value of at least 32 characters, for example from openssl rand -base64 32, and use a different one for each environment.

Answer Meaning
200 The tick ran, and every job that ran succeeded
500 The tick ran, and at least one job failed
503 The endpoint is off, because CRON_SECRET isn’t set
401 The secret was wrong. Too many wrong ones from one address (more than 20 in 15 minutes) answer 429

The right secret always runs the tick, even after wrong guesses. The answer is never cached.

When a tick stops at its time limit with work still due, the endpoint calls itself again after answering, so a backlog doesn’t wait for the next tick. It does this up to 16 times in a row, to Adiutor’s own address (BETTER_AUTH_URL), with the secret. A call that continues a tick answers 202 at once.

  • The command runs every job, then exits with 1 if one failed. Cron’s mail, or your monitoring, notices that exit code. The endpoint answers 500 for the same.
  • A scheduled job tries again at its next scheduled moment.
  • A queued job that fails is attempted again later: after 5 minutes, then 15 minutes, an hour and 6 hours. After its last attempt (5 by default) it’s given up. Staff Admins can retry a job that was given up from Logs › Jobs, or you can with pnpm jobs:retry.
  • When a job keeps failing (its third failed run in a row, or queued jobs given up), every staff Admin gets an email with the error’s reference, at most once a day per job.
  • A run that never finishes, for example because the platform stopped it, is recorded as failed by the next tick.

The failure is recorded as an error, so you look it up by its reference: see Looking up an error reference.

  • In the app. Staff Admins and Managers see Logs › Jobs: when the last tick started, every scheduled job with its last and next run, the queued jobs waiting and failed, and the recent runs. Staff Admins can also run a job now, pause and resume it, and retry or discard failed queued jobs. See Logs.
  • A warning. When no tick has started in the last 45 minutes, or none ever has, the dashboard and the Jobs page tell staff that “Background jobs aren’t running”.
  • On the command line, from the app’s folder or a one-off container (see Commands):
    • pnpm jobs:list shows every job with its schedule, state, last and next run, and each kind of queued job with how many wait and failed.
    • pnpm jobs:run <name> runs one scheduled job now, even if it’s paused.
    • pnpm jobs:retry <id> or pnpm jobs:retry --kind <kind> puts failed queued jobs back in the queue.
    • pnpm jobs:discard <id> or pnpm jobs:discard --kind <kind> discards them.

Adiutor at https://adiutor.dev runs from Docker Compose, whose jobs service ticks every 5 minutes. Alex Admin opens Logs › Jobs and sees the last tick a minute ago and no failed jobs. When the storage’s access key is revoked, the attachment-files job fails three runs in a row, and Alex gets an email with the error’s reference. A broken mail server can’t send that email, so Alex also keeps an eye on the Jobs page.

  • Jobs only work once the tick runs. Until it does, no reminders or security emails go out.
  • Run the tick from one place: cron, a timer, the Compose service or a cron service. Two at once are safe, but add nothing. Vercel’s own schedule is the exception: it’s there as a fallback for the external service.
  • Times in the Jobs page are in your own time zone. The schedules here are UTC.

Adiutor's code is released under the MIT License. The Adiutor name and logo aren’t covered by it; see the notice.

Set in Inter and JetBrains Mono, under the SIL Open Font License (Inter, JetBrains Mono).