Worker Tasks and Background Processing
Overview
The Rhesis backend uses Celery to handle asynchronous background tasks. This allows the API to offload time-consuming operations and improve responsiveness. The task processing system is designed to be scalable, fault-tolerant, and context-aware.
Celery Configuration
The Celery application is configured in worker.py:
The application uses Redis as both the broker and result backend with TLS support:
Note: The rediss:// protocol indicates Redis with TLS/SSL encryption. The ssl_cert_reqs=CERT_NONE parameter is used when connecting to managed Redis services that use self-signed certificates.
Base Task Class
All tasks inherit from a BaseTask class that provides retry logic, error handling, and most importantly, context awareness for multi-tenant operations:
This enhanced BaseTask ensures that:
- Tasks have access to organization_id and user_id for proper multi-tenant operations
- Context is automatically propagated through the task execution
- Error handling and retry logic are standardized
- Logging and monitoring include context information
Tenant Context Decorator
A task decorator is provided to automatically handle database sessions with proper tenant context:
Using this decorator simplifies working with database operations in tasks:
Task Launcher Utility
A task_launcher utility method is provided to easily launch tasks with proper context from FastAPI routes:
Task Organization
Tasks are organized in the tasks/ directory:
Creating Tasks
When creating a task, you no longer need to explicitly require organization_id and user_id as parameters. The context system handles this automatically:
Simple Task without Database Access
Task with Automatic Database Context
Task with Manual Database Session Control
Using Tasks in FastAPI Routes
The most common way to launch tasks is from FastAPI route handlers:
Worker Configuration
Celery workers are configured with Redis-optimized performance settings:
Redis-Specific Configuration
The Celery app includes Redis-optimized settings:
The worker startup script applies these configurations:
Optional monitoring is available through Flower:
Task Monitoring
Task status can be monitored through several interfaces:
API Endpoint
Flower Dashboard
Access the Flower web UI at http://localhost:5555 when enabled.
Error Handling
The enhanced BaseTask provides improved error handling:
- Exceptions are logged with tenant context information
- Failed tasks are automatically retried with exponential backoff
- After maximum retries, the error is recorded in the result backend
- Both success and failure callbacks include context information
- Task execution time and other metrics are tracked automatically
Troubleshooting
For detailed information about troubleshooting common worker issues, including:
- Dealing with stuck tasks and chord_unlock zombies
- Fixing tenant context errors
- Connection problems with the broker
- Task execution failures
Please refer to the Worker Troubleshooting Guide.
Task Monitoring
Task status can be monitored through several interfaces: