---
title: Worker jobs
description: How a processing job is claimed, run and retried, and how the number of workers grows with the backlog.
sidebar:
  label: Worker jobs
  order: 4
---

Mail adds one job per email to the processing queue, as described on [Mail to processing](/engineering/mail-to-processing). This page covers what happens next: which worker takes the job, what it reads and writes, what happens when it fails, and how the number of workers grows with the backlog. The listener, its retries and the sweeper are built. The child-process pool and scaling on backlog are not: each job runs in the listener's own process, and one worker process runs until it is stopped. What a job does, step by step, is on [Sync to labels](/engineering/sync-to-labels).

## One job, start to finish

[![One job from hand-off through claim, run and retry, with the stores it reads and writes](/blume-assets/content/content/engineering/worker-takes-a-job-preview.png)](/engineering/worker-takes-a-job-diagram)

<a href="/engineering/worker-takes-a-job-diagram">Open the interactive diagram</a>

- Every worker task runs one listener that waits on the queue. Redis hands a new job to the listener that has waited longest, and the listener claims it with a lock that it keeps renewing while the job runs. The same task runs a second listener, on the `enquiry-requirements` queue, three jobs at a time and never two for the same enquiry: the later one goes back to wait for five seconds and asks again. An enquiry's jobs usually run in the order they were queued, but a job retried after a failure, or one still waiting its five seconds when a later job arrives, can run after that later job. Only a job queued from Mail Debug replaces requirements, so the order decides the result only when two emails of one enquiry are reprocessed at the same time. Its jobs are added by the email job, for an enquiry it has read and linked that has no requirements, while requirement gathering is switched on and covers the email's mailboxes, by the sweeper for read enquiries that still wait, and by the gather action on Mail Debug. They are tried three times, and wait while requirement-gathering calls are switched off ([Requirement gathering](/business/workflows/requirement-gathering)).
- The listener runs the job, two at a time by default. The job loads the `email_message` row by id, reads the raw email from blob storage, processes it, and saves its artifacts to the pipeline tables. It records the email's origin first; a copy of an email already recorded is linked to it and finishes there, with no documents converted and no model call (see [Sync to labels](/engineering/sync-to-labels)). Running each job in a child process from a small pool is planned, so that a crash fails one job instead of the task.
- If the job fails, or the whole task dies and its lock expires, the job waits out a backoff and returns to the waiting list. After three failed tries it stays failed, in the queue's failed list on mail's `/queues` page.
- Every five minutes, and at once when model calls are switched on, a sweeper re-adds jobs for stored emails that have no pipeline run and, with model calls on, for those whose run is open with no job on the queue: at once when the email was never classified, after 30 minutes when it was. It does so at most three times per run. Jobs are deduplicated by the email's id, so a re-added job cannot create a duplicate, and an email waiting in the queue is never counted as stalled. A run swept three times that is still stalled is taken off the queue: the email is marked for review and the run is failed. While requirement gathering is on, the same sweep, and one asked for when gathering resumes, queues gathering for every read, linked enquiry email whose enquiry has no requirements and whose mailboxes gathering covers, the oldest first, each with a priority that puts it behind every job added for newly read mail; an email sent for review after a failed gathering is left out, and a reading whose own job failed is never queued again by a sweep, even when the worker died before it could mark the email. The detail is on [Sync to labels](/engineering/sync-to-labels).
- Mail from a client that is linked to an enquiry is handed on to two further queues, `client-changes` and `purchase-orders`, once it is processed. The processing job hands on a revision request, a follow-up, a purchase order, mail typed Other and an enquiry email corrected to a revision request right after its link is saved: a purchase order goes to both queues, the others to `client-changes`. A later enquiry email joined to its enquiry reaches `client-changes` through the sweep below; the email that starts an enquiry is never queued. Answers to our queries go to `client-changes` whatever their kind once they are linked as replies to our queries, which nothing does yet. Our own quotations, sales orders and other mail of ours go to neither, and mail in Needs Review or not linked is never queued.
- There is one job per email per queue, its id the email's id, tried three times with a five-second exponential backoff, and the last 1,000 failed jobs are kept; an email that already has a job on a queue, a failed one included, is not added to it again. If an add fails, the processing job fails and is retried; the retry keeps the saved link and hands nothing on, so the sweep re-adds the email. Each queue's reader marks the link handled for its own queue when it saves its result, so a purchase order the purchase order reader has handled still waits for the change reader. Both queues are listed on Mail Debug, an email's own activity included, and on mail's `/queues` page.
- The change reader takes `client-changes` jobs two at a time. It reads the client's own lines, the comments typed on the PDFs they attach, each picture pasted among their words and each file they attach that grades or revises our offer, and saves each change it finds with its state. A picture is a JPEG, PNG, WebP or GIF at least 200 px long and 20 px high and at most 512 KB, at most five an email, transcribed by the OCR model only when the reader may send the email to its model and put in the lines where it sat. A picture in quoted history or a signature is neither sent nor listed; one that is not read is named in the reading's unreadable list, such as `Image 2 in the email body: unreadable`. A change that names a line by its tag or number is placed on that line; one that names nothing it can place is left for a person.
- On saving its reading the change reader also fills the answers a client gave to our open questions into the lines they name, marks the enquiry ordered on the earliest sign of an order, refreshes the lines a change touched, and queues a gathering job for the lines the client adds in writing when the enquiry has requirements ([Requirement gathering](/business/workflows/requirement-gathering)). A job that adds lines appends them to the current requirements and replaces nothing.
- While model calls are off, a `client-changes` job whose email still waits and may be read by the model is delayed a minute on its own, spending no attempt, and the jobs behind it run; a stale email and our own mail are settled at once, and mail delivered outside the requirements allow-list, or read with no model configured, is read by keyword rule instead. Only a rate limit from the model holds the whole queue, for a minute. The reader checks again that the email still waits before saving, and marks the link handled for its queue in the same save.
- The purchase order reader takes `purchase-orders` jobs two at a time, only while requirement-gathering calls are on and only for mail delivered to a mailbox on the requirements allow-list; mail outside it fails for good, and while the calls are off the whole queue is held a minute. It reads the order's documents, pictures and the email's own text, and saves each order it finds with the evidence for each value, with the PO rows and the link's handled mark in one save. An email with no purchase order, or with a file that could be one and was not read, goes to Needs Review; the orders it did read are saved either way. A provider rate limit holds the queue a minute ([Checking a purchase order](/business/workflows/checking-a-purchase-order)).
- The same sweep re-adds linked, processed client mail that a queue its kind routes to still waits for, such as a later enquiry email, an add that failed or mail linked before this shipped, oldest first and at most 500 at a time. Its summary line ends `client mail queued <n>`, the number of emails it added a job for; an email already waiting on its queues is not counted. A queue whose reader has handled the email is left out.
- Where fetching earlier mail is on, an unlinked client revision request, purchase order or follow-up that names mail FlowOS does not hold has that mail fetched by the mail service from the mailboxes the email reached ([Mail to processing](/engineering/mail-to-processing)). Once all of it is stored, a history job processes it oldest first, rebuilds the enquiry from it and links every email naming it again. Earlier mail is never sent to review or to the client-mail queues. The sweep re-adds a history job whose mail is all stored.

## The worker fleet

This part is planned; no worker runs on ECS yet.

[![Worker tasks pulling from one queue, the shared stores, and autoscaling on backlog](/blume-assets/content/content/engineering/worker-fleet-scaling-preview.png)](/engineering/worker-fleet-scaling-diagram)

<a href="/engineering/worker-fleet-scaling-diagram">Open the interactive diagram</a>

- Workers run as tasks of one ECS service, and every task pulls from the same queue. Adding a task adds capacity with no other change.
- Each task runs a set number of jobs at once, one per child process. Capacity is the number of tasks times that number.
- The sweeper reports the backlog per task to CloudWatch. When it passes the target, Auto Scaling starts another task, and that task's listener starts claiming jobs.
- When the queue drains, Auto Scaling stops tasks. A stopping task finishes its running jobs first; a job cut off after 120 seconds is retried on another task.
