"""Redis-backed task log store for SSE streaming.""" import json import time import logging from app.config import settings logger = logging.getLogger(__name__) TASK_LOG_TTL = 3600 # 1 hour def log_task_event(task_id: str, message: str, level: str = "info") -> None: """Append a log line to Redis list and publish to channel. Safe to call from Celery tasks.""" try: import redis r = redis.from_url(settings.redis_url) entry = json.dumps({"ts": time.time(), "level": level, "msg": message, "task_id": task_id}) pipe = r.pipeline() pipe.rpush(f"task_logs:{task_id}", entry) pipe.expire(f"task_logs:{task_id}", TASK_LOG_TTL) pipe.publish(f"task_logs_ch:{task_id}", entry) pipe.execute() r.close() except Exception as exc: logger.debug("log_task_event failed: %s", exc)