feature/pedimentos-correccion-partidas
This commit is contained in:
42
core/redis_events.py
Normal file
42
core/redis_events.py
Normal file
@@ -0,0 +1,42 @@
|
||||
import json
|
||||
import os
|
||||
import logging
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
CHANNEL_PREFIX = "audit_task:"
|
||||
STATE_PREFIX = "audit_task_state:"
|
||||
STATE_TTL = 7200 # 2 horas
|
||||
|
||||
|
||||
def _get_client():
|
||||
import redis
|
||||
return redis.Redis(
|
||||
host=os.getenv("REDIS_HOST", "localhost"),
|
||||
port=int(os.getenv("REDIS_PORT", 6379)),
|
||||
db=int(os.getenv("REDIS_DB", 0)),
|
||||
decode_responses=True,
|
||||
socket_connect_timeout=2,
|
||||
socket_timeout=2,
|
||||
)
|
||||
|
||||
|
||||
def publish_task_event(task_id: str, status: str, message: str = "", resultado: dict = None, progress: int = None):
|
||||
"""
|
||||
Publica un evento de progreso de tarea en Redis Pub/Sub.
|
||||
El microservicio SSE usa el mismo canal para streamear al frontend.
|
||||
"""
|
||||
payload: dict = {"task_id": task_id, "status": status, "message": message}
|
||||
if resultado is not None:
|
||||
payload["resultado"] = resultado
|
||||
if progress is not None:
|
||||
payload["progress"] = progress
|
||||
|
||||
try:
|
||||
client = _get_client()
|
||||
serialized = json.dumps(payload)
|
||||
client.publish(f"{CHANNEL_PREFIX}{task_id}", serialized)
|
||||
client.setex(f"{STATE_PREFIX}{task_id}", STATE_TTL, serialized)
|
||||
client.close()
|
||||
except Exception as exc:
|
||||
logger.error(f"[redis_events] Error publicando evento para tarea {task_id}: {exc}")
|
||||
Reference in New Issue
Block a user