Environment:1Panel dev MaxKB Celery Redis
| Knowledge Sources | |
|---|---|
| Domains | Infrastructure, Async_Tasks, RAG |
| Last Updated | 2026-10-10 00:00 GMT |
Overview
Redis 6+ as Celery broker and result backend with celery-once for task deduplication, powering MaxKB's async document embedding pipeline.
Description
This environment provides a Redis instance serving as both the Celery message broker and result backend. MaxKB runs two Celery queues: `celery` (general tasks) and `model` (model/long-running tasks). The celery-once library provides single-execution locking via Redis to prevent duplicate embedding tasks. Redis Sentinel is supported for high-availability deployments. The Celery app uses a custom HMAC-signed pickle serializer for secure task serialization.
Usage
Use this environment for Async Embedding workflows and any background task processing. It is the mandatory prerequisite for the embedding_by_document implementation and all Celery-decorated tasks across MaxKB's apps. Without Redis, no async document processing, embedding generation, or scheduled jobs will function.
System Requirements
| Category | Requirement | Notes |
|---|---|---|
| OS | Linux (Ubuntu 20.04+ recommended) | Container-based deployment supported |
| Hardware | 1+ CPU cores, 1GB+ RAM | Redis `maxmemory` configured at 1GB by default |
| Disk | 5GB SSD | For Redis AOF persistence and Celery task state data |
| Software | Redis 6+ | Redis Sentinel optional for HA deployments |
Dependencies
System Packages
- `redis-server` >= 6.0
- `redis-sentinel` (optional, for HA mode)
Python Packages
- `celery[sqlalchemy]` == 5.5.3
- `celery-once` == 3.0.1
- `django-redis` == 6.0.0
- `django-celery-beat` == 2.8.1
- `django-apscheduler` == 0.7.0
- `redis` (Python client, transitive via celery)
- `kombu` (transitive via celery)
Credentials
The following environment variables must be set (via `MAXKB_` env prefix or YAML config):
- `MAXKB_REDIS_HOST`: Redis host (default `127.0.0.1`)
- `MAXKB_REDIS_PORT`: Redis port (default `6379`)
- `MAXKB_REDIS_PASSWORD`: Redis password
- `MAXKB_REDIS_DB`: Redis database number (default `0`)
- `MAXKB_REDIS_MAX_CONNECTIONS`: Max connection pool size (default `100`)
- `MAXKB_REDIS_SENTINEL_SENTINELS`: Comma-separated sentinel addresses (optional, for HA)
- `MAXKB_REDIS_SENTINEL_MASTER`: Sentinel master name (optional, for HA)
- `MAXKB_REDIS_SENTINEL_PASSWORD`: Sentinel password (optional, for HA)
- `MAXKB_SECRET_KEY`: Used as HMAC signing key for task serialization
- `MAXKB_HMAC_SIGNED_SERIALIZER_SECRET_KEY`: Override HMAC key (defaults to `MAXKB_SECRET_KEY`)
Quick Install
# Install Redis (Ubuntu/Debian)
apt-get install -y redis-server redis-sentinel
# Python dependencies:
pip install "celery[sqlalchemy]==5.5.3" celery-once==3.0.1 django-redis==6.0.0 \
django-celery-beat==2.8.1 django-apscheduler==0.7.0
# Start Celery worker (dev mode):
python main.py dev celery
Code Evidence
Celery app definition with two queues from `apps/ops/celery/__init__.py:16-27`:
app = Celery('MaxKB')
configs['worker_concurrency'] = 5
configs["task_queues"] = [
Queue("celery", Exchange("celery"), routing_key="celery"),
Queue("model", Exchange("model"), routing_key="model")
]
Redis broker URL configuration from `apps/maxkb/settings/lib.py:53-60`:
CELERY_BROKER_URL = CELERY_BROKER_URL_FORMAT % {
'protocol': 'redis',
'password': CONFIG.get('REDIS_PASSWORD'),
'host': CONFIG.get('REDIS_HOST'),
'port': CONFIG.get('REDIS_PORT'),
'db': redis_celery_db
}
celery-once configuration with Redis backend from `apps/maxkb/settings/lib.py:85-90`:
celery_once_settings['default_timeout'] = 3600 # 锁的默认超时时间(秒)
CELERY_ONCE = {
'backend': 'celery_once.backends.Redis',
'settings': celery_once_settings
}
HMAC-signed serializer for secure task serialization from `apps/ops/celery/hmac_signed_serializer.py:15-29`:
def secure_dumps(obj):
data = pickle.dumps(obj)
signature = hmac.new(_local_secret_key.encode(), data, hashlib.sha256).digest()
return signature + data
def secure_loads(signed_data):
if len(signed_data) < 32:
raise ValueError("Invalid signed data packet")
signature = signed_data[:32]
payload = signed_data[32:]
expected_signature = hmac.new(_local_secret_key.encode(), payload, hashlib.sha256).digest()
if hmac.compare_digest(signature, expected_signature):
return pickle.loads(payload)
else:
raise ValueError("Security Alert: Task signature mismatch!")
Celery serializer config from `apps/maxkb/settings/lib.py:74-77`:
CELERY_task_serializer = 'hmac_signed_serializer'
CELERY_result_serializer = 'hmac_signed_serializer'
CELERY_accept_content = ['json', 'hmac_signed_serializer']
Common Errors
| Error Message | Cause | Solution |
|---|---|---|
| `Connection refused` (Redis) | Redis not running | Start Redis: `redis-server /opt/maxkb/conf/redis.conf` or check `MAXKB_REDIS_HOST` |
| `Security Alert: Task signature mismatch!` | HMAC key mismatch between workers | Ensure `MAXKB_SECRET_KEY` or `MAXKB_HMAC_SIGNED_SERIALIZER_SECRET_KEY` is consistent across all processes |
| `QueueOnce: Already running` | Duplicate task for same key | celery-once prevents concurrent execution; wait for the first task to finish or increase `default_timeout` |
| `celery-once lock not released` | Worker crashed during task | Lock auto-expires after `default_timeout` (3600s); manually clear Redis keys if needed |
| `Result backend not available` | Redis unreachable for results | Verify `CELERY_BROKER_URL` matches Redis connection; check Sentinel config if HA |
Compatibility Notes
- Redis Sentinel: Supported via `MAXKB_REDIS_SENTINEL_SENTINELS` env var. When enabled, `DJANGO_REDIS_CONNECTION_FACTORY` is set to `SentinelConnectionFactory` and celery-once discovers the master via `Sentinel.discover_master()`.
- Redis persistence: Default config uses AOF (`appendonly yes`, `appendfsync everysec`) with RDB snapshots. See `start-redis.sh` for full config.
- Celery soft time limit: Set to 3600s (`CELERY_TASK_SOFT_TIME_LIMIT`); long-running model tasks should use the `model` queue.
- Worker concurrency: Fixed at 5 workers (`worker_concurrency = 5`). Adjust based on available CPU and model API rate limits.
- Task result expiry: 600 seconds (`CELERY_RESULT_EXPIRES`). Results are not persisted long-term.