five-on-it / apps/api/src/queue
apps/api/src/queue
The platform's durable job queue (TECHNICAL_SPECS §9.6): jobs kept in the store (store.ts's Job, the jobs table in Postgres), so a job outlives the process that queued it. A process runs one kind of job (builds now; the creations of the platform's model later, TECHNICAL_SPECS §9.8) a number at a time: it claims the next one in line (atomically: Postgres's FOR UPDATE SKIP LOCKED, a memory or file store between two awaits), holds it with a lease it renews every third of the lease while the job runs, and settles it when it is over, which only the attempt that still holds it can. A job whose lease ran out (its process went away) is queued again while it has attempts left, else failed; the queue looks for those, and for jobs another process queued, every pollMs once started, and every time a job is queued or finishes here.
The line is fair: the highest priority first, then the owner who had the fewest jobs of the kind started that day (UTC), then the oldest; one owner's ten builds take turns with another's one. lineOrder plays the claims out to give each queued job its place in line (1 is the next to start), which a creator sees while a build waits.