Background Tasks
The backend offloads long-running work to Celery: test execution, trace enrichment, metric evaluation, and Architect chat turns. Tasks carry tenant context (organization and user) so they run with the same isolation as an API request.
Celery configuration
The Celery app is created in celery/core.py, which applies CELERY_CONFIG from
celery/config.py and auto-discovers tasks under rhesis.backend.tasks:
Redis serves as both broker and result backend. Use rediss:// for TLS; the
ssl_cert_reqs=CERT_NONE parameter connects to managed Redis services that use self-signed
certificates.
CELERY_CONFIG sets Redis-oriented defaults: result_expires=3600, result_compression="gzip",
broker connection retries, and transport options with 30-second socket timeouts for TLS
connections.
Base task class
Tasks inherit from BaseTask, which adds retry settings and tenant-context management. It reads
organization_id and user_id from task kwargs when the task is queued, stores them in the task
headers, and restores them onto the request before the task starts:
Tenant context decorator
with_tenant_context opens a database session with the task’s tenant context and passes it to the
function as db:
Task launcher
task_launcher launches a task from a FastAPI route, pulling organization_id and user_id off
current_user so callers don’t pass them explicitly:
Writing tasks
You do not pass organization_id and user_id as explicit parameters — the context system
propagates them. Use @with_tenant_context when the task needs a database session:
Without the decorator, open a session manually via self.get_db_session(), which already carries
tenant context:
Launch tasks from a route with task_launcher:
Running workers
apps/worker/start.sh runs two Celery workers against the same broker:
- a main worker on the
celery,execution, andtelemetryqueues - an architect worker on the
architectqueue
Both use the thread pool. Concurrency and prefetch are set through environment variables
(CELERY_WORKER_CONCURRENCY, CELERY_WORKER_PREFETCH_MULTIPLIER, and the CELERY_ARCHITECT_*
equivalents):
LOG_LEVEL controls application logs; CELERY_WORKER_LOGLEVEL controls Celery’s own
task-lifecycle messages and defaults to LOG_LEVEL. Set ENABLE_FLOWER=yes to run the Flower
monitoring UI on port 5555.
Monitoring task status
Query a task’s status through the result backend:
Error handling
BaseTask logs exceptions with tenant context, retries failed tasks with exponential backoff up to
max_retries, and records the final error in the result backend once retries are exhausted.
For stuck tasks, broker connectivity, and tenant-context errors, see Troubleshooting.