What this thread does
Do first: replace an in-request report call with apply_async.
Short answer
As of November 2026, this path pins FastAPI 0.162.2 and Celery 5.6.3, March 26, 2026. Return HTTP 202 with a status URL; read the durable owner-scoped job record before querying Celery. PENDING alone cannot establish existence.
Choose / don't choose
- Celery worker: choose for long rendering, isolation and durable delivery/status requirements; don't assume broker delivery gives exactly-once output.
- BackgroundTasks: choose for small in-process follow-up using local objects; don't use it as durable report scheduling across API restarts (FastAPI caveat).
- Queue selection: if Celery is not already selected, compare alternatives (see celery-dramatiq-or-arq-for-fastapi-background-jobs-2026).
Settings that matter
| Setting or flag | Value / default | Applies to | Source |
|---|---|---|---|
worker_concurrency | Default CPU-core count; for CPU-bound reports keep near allocated CPUs | Celery 5.6.2 documentation | source |
task_time_limit | Default no hard limit; choose a finite seconds budget from the report SLA; exceeding it kills/replaces the child | Celery 5.6.2 documentation | source |
autoretry_for; max_retries | Retry only transient exception classes; default no automatic classes; set a finite max_retries; never None for a bounded policy | Celery 5.6.2 documentation | source |
retry_backoff; retry_backoff_max; retry_jitter | Enable True for exponential backoff; set a maximum delay and use jitter to spread retries | Celery 5.6.2 documentation | source |
result_backend | Default none; configure the same shared database or Redis result backend in API and worker | Celery 5.6.2 documentation | source |
result_expires | Default expire after 1 day; retain beyond polling period, then rely on durable terminal records | Celery 5.6.2 documentation; cleanup depends on backend | source |
task_track_started; task_ignore_result | True (default False); False (default False) to expose running and completion results | Celery 5.6.2 documentation | source |
task_always_eager | Keep False so the caller publishes rather than executes the report locally | Celery 5.6.2 documentation | source |
Order of operations
Application design around Celery publication APIs:
2. A recoverable dispatcher publishes generate_report.apply_async(args=[report_id], task_id=job_id). Use the same broker/backend configuration in API and worker; make output idempotent by job ID. 3. Start a separate celery -A report_tasks worker --loglevel=INFO. Open worker-owned database connections; save artifacts in shared storage. Do not call .get() in the request handler. 4. Store terminal outcome and artifact reference durably before relying on backend cleanup. Map STARTED to running, SUCCESS to completed, FAILURE to failed; retain RETRY and REVOKED. 5. The status endpoint authorizes owner and existence before AsyncResult(job_id, app=jobs). Compare a paused worker, an unknown ID, a failed report and an expired result. Result API.
Known failures and fixes
| Error (short quote) | Cause | Fix | Source |
|---|---|---|---|
PENDING | ID unknown, awaiting worker, or expired backend state; not proof of a queued job | Check durable registry first, then broker/queue routing and worker receipt logs keyed by ID; preserve terminal status after expiry | result API |
FAILURE | Task execution raised an exception | Use stored failure metadata and worker log; expose a stable public error code and retry only classified transient errors | source |
OperationalError | Broker connection failed during publication | Record submission failure; inspect broker reachability and publish retries before resubmission with the same durable job ID | source |
Missing
- A universal concurrency number or time budget cannot be sourced for an unknown report workload. Set the actual limit from measured resource allocation and deadline; network I/O needs its own timeouts.
- No outbox implementation or exactly-once guarantee is supplied by the task APIs.
Checked
2026-11-12; Linked releases and FastAPI guides read; linked Celery configuration/task/result/calling pages display a 5.6.2 documentation banner. No API or worker commands executed.
Related references: background-work guide; application and worker path; response-status path; calling API; state definitions; HTTPException path; synchronous def route.
Not for: Node.js job queues; see bullmq-or-pg-boss-for-node-background-jobs.
Started from a post by @FastAPI, packaged by the editor. Original post