Background jobs¶
Any request that takes background: true returns at once with a job_id.
The work then runs as a job, stored in Postgres (the jobs table). A
restart does not lose it: the job picks up where it stopped.
These requests take background:
| Request | Job kind |
|---|---|
POST /projects/{id}/documents (form field background=true) |
upload |
POST /projects/{id}/documents/text |
text |
POST /projects/{id}/documents/url |
url |
POST /projects/{id}/documents/s3 |
s3 |
POST /projects/{id}/sync |
sync |
POST /projects/{id}/reindex |
reindex |
POST /projects/{id}/documents/{doc}/process |
process |
POST /projects/{id}/graph/build (background defaults to true) |
graph |
A project with the graph's auto_build
on also gets a graph job queued after each ingestion that leaves chunks
to extract.
Follow a job¶
curl -X POST localhost:8000/api/v1/projects/1/documents \
-F files=@handbook.pdf -F files=@policies.zip -F background=true
{
"project_id": 1,
"processing": "background",
"job_id": 42,
"results": [ /* each file, "created": stored, waiting for the job */ ],
"counts": {"created": 12}
}
Uploads and text are stored before the response, so their results already
list the documents. Web, S3 and sync jobs fetch their content later, so
their results are empty until the job runs.
Poll the job:
{
"job_id": 42,
"kind": "upload",
"status": "running", // queued, running, succeeded, failed, cancelled
"progress": {"stage": "processing", "total": 12, "done": 5, "indexed": 5, "failed": 0},
"attempts": 1,
"cancel_requested": false,
"error": null,
"result": null, // set when the job ends
"created_at": "2026-11-29T10:00:00Z",
"started_at": "2026-11-29T10:00:01Z",
"finished_at": null
}
When the job ends, result holds what the same request would have returned
inline: counts, one entry per item in results, and for web and S3 also
stats and warnings. A job whose documents fail one by one still
succeeded; the failures are in counts and in each document's error. A
job is failed only when it could not run at all (for example, the bucket
was gone), or when a process job's document failed. In both cases error
says why.
Each document records the job that last processed it (job_id). With
background: true, POST .../documents/{doc}/process returns the document
as it is now, with job_id set to the job queued to process it, like the
other background requests.
List and cancel¶
# The project's jobs, newest first (page, page_size; filter by status and kind)
curl 'localhost:8000/api/v1/projects/1/jobs?status=running'
# Cancel
curl -X POST localhost:8000/api/v1/projects/1/jobs/42/cancel
A queued job is cancelled at once, and its uploaded files are deleted. A
running job finishes the document it is on, then stops; a web, S3 or sync
job still importing stops before its next page, object or document. It
becomes cancelled, with a partial result; documents already indexed stay
indexed. A cancel that arrives once a job has nothing left to do (for
example, an import with process: false that has stored everything) does
not change its outcome: it is succeeded. Cancelling a finished job
changes nothing.
A job that is cancelled (or fails) before processing all its documents
marks the ones it never processed failed, with an error saying so; they
do not stay pending. For an upload or text job, these are the documents
its request created; for a web, S3 or sync job, the ones it stored (or
found again) and was going to process. Upload or import them again, or
process them with POST .../documents/{doc}/process. (One gap: a web, S3
or sync job interrupted JOB_MAX_ATTEMPTS times leaves what it stored
pending, since it never reported what that was.)
Listing and getting jobs needs a read key. Cancelling needs write. A key
limited to some projects only sees their jobs
(Authentication).
How jobs run¶
flowchart LR
A["request<br/>background: true"] -->|"queued"| Q[("jobs table")]
Q -->|"claim<br/>FOR UPDATE SKIP LOCKED"| W["worker thread"]
W -->|"heartbeat, progress"| Q
W -->|"succeeded / failed / cancelled"| Q
- The API process runs
JOB_WORKERSworker threads (2 by default). Each claims the oldest queued job and runs it, one document after another. Postgres row locks (FOR UPDATE SKIP LOCKED) make sure no job runs twice, even with several API processes on one database. - A
graphjob is not claimed while anothergraphjob of the same project runs: it waits its turn, and other jobs run meanwhile. - While a job runs, its worker refreshes the job's heartbeat. If the
process dies, the heartbeat stops. After
JOB_STALE_SECONDSanother worker puts the job back in the queue. When it resumes, documents it indexed are not redone; documents it failed on are tried again. - A document the job was on when it was interrupted:
- was
indexedand its index was not replaced yet: staysindexed(and searchable); - had never been processed: goes back to
pending; - otherwise:
failed, with anerrorsaying the job was interrupted, andchunk_count0. Search does not return the chunks it had partly written (it reads only indexed documents), but they are kept until the job runs again, so the chunk contexts the LLM wrote (llm_context) are reused instead of paid for again.
The resumed job processes it again in all three cases.
- A job interrupted JOB_MAX_ATTEMPTS times is marked failed instead of
run again. A document it was on then stays indexed (with
Re-processing failed: ... in its error) if its index was not replaced,
and is failed otherwise, with the chunks it had written deleted.
- If a worker was only slow (its heartbeat late, not dead), and its job was
queued again, it stops before writing anything more to the job's
documents; the job's new run does the work. Each write to a document's
index (its chunks, vectors and status) holds the job's row
(FOR KEY SHARE), so a job is never queued again in the middle of one:
the write ends first. The embeddings are computed before the old index is
replaced, so an embedding failure on re-processing keeps the document
indexed, with Re-processing failed: ... in its error. Deleting a
project waits for a write in progress.
- On a normal shutdown, each running job finishes its current document and
goes back to the queue. That run does not count as an attempt. If a
document is still running when the shutdown stops waiting (8 seconds),
it fails as the app disconnects (or, when re-processed, keeps its index
with the failure noted), and the job goes back to the queue all the same
to process it again; it does not end succeeded or failed. A job cancelled just
before the shutdown is cancelled when a worker next picks it up, without
running again.
- Uploaded files wait in src/assets/jobs/ (not /tmp), so they survive a
restart. In Docker Compose, this folder is on the fastapi_data volume.
They are deleted when the job ends. Web, S3 and sync jobs
download again when they resume; pages and objects already stored are
unchanged.
- Finished jobs are deleted after JOB_RETENTION_DAYS.
With JOB_WORKERS=0, an API process only queues jobs, and another process
on the same database must run them. That process needs the same
src/assets/ folder, for the uploaded files.
Settings¶
| Setting | Default | |
|---|---|---|
JOB_WORKERS |
2 |
Worker threads in the API process. 0: this process only queues jobs |
JOB_POLL_SECONDS |
2 |
How often an idle worker checks the queue. Jobs queued by the same process start at once |
JOB_STALE_SECONDS |
120 |
A running job with no heartbeat for this long is taken as dead and queued again |
JOB_MAX_ATTEMPTS |
3 |
Runs before an interrupted job is failed instead of queued again |
JOB_RETENTION_DAYS |
14 |
Finished jobs are deleted after this many days |