From 677fe94a4f28f853b167f256189d2b1d270c8e36 Mon Sep 17 00:00:00 2001 From: Mirko Stamm Date: Tue, 20 Oct 2026 15:01:00 +0100 Subject: [PATCH] fix: route pending notifications delivery through Celery worker --- core/socket_manager.py | 21 +++++++------------ tasks.py | 46 +++++++++++++++++++++++++++++++++++++----- 2 files changed, 48 insertions(+), 19 deletions(-) diff --git a/core/socket_manager.py b/core/socket_manager.py index 296a6c4..7c743fd 100644 --- a/core/socket_manager.py +++ b/core/socket_manager.py @@ -3,7 +3,7 @@ from .logger import logger from db.database import redis_client, SessionLocal from redis.asyncio import Redis -from tasks import save_notification_in_db, get_pending_notifications_for_user +from tasks import save_notification_in_db, deliver_pending_notifications from core.event_ws import format_notification import asyncio import json @@ -146,16 +146,9 @@ async def connect_to_user_projects(self, websocket, user_id: int, session): async def send_pending_notifications(user_id: int): - session = SessionLocal() - pending = get_pending_notifications_for_user(user_id) - if not pending: - return {"detail": "No habian notificaciones a enviar"} - else: - for n in pending: - notification_to_send = format_notification( - notification_type=n.type, message=n.payload - ) - await manager.send_to_user(user_id=user_id, message=notification_to_send) - n.status = Notify_State.ENVIADO - session.commit() - logger.info(f"[send_pending_notifications] Notifications were updated") + try: + deliver_pending_notifications.delay(user_id) + return {"detail": "Pending notifications delivery enqueued"} + except Exception as e: + logger.error(f"[send_pending_notifications] Error enqueuing delivery: {str(e)}") + return {"detail": "Error enqueuing pending notifications"} diff --git a/tasks.py b/tasks.py index b18d581..7fe5e1c 100644 --- a/tasks.py +++ b/tasks.py @@ -19,8 +19,7 @@ app = Celery("tasks", broker=url) -@app.task(retry_backoff=5) -def get_pending_notifications_for_user(user_id: int) -> List[db_models.Notifications]: +def get_pending_notifications_for_user_sync(user_id: int) -> List[db_models.Notifications]: session = SessionLocal() try: stmt = select(db_models.Notifications).where( @@ -29,22 +28,61 @@ def get_pending_notifications_for_user(user_id: int) -> List[db_models.Notificat ) notify_found = session.exec(stmt).all() - + session.close() return notify_found except SQLAlchemyError as e: + session.close() logger.error( f"[get_pending_notifications_for_user] Internal Error | Error: {str(e)}." ) raise except Exception as e: + session.close() logger.error( f"[get_pending_notifications_for_user] Notification Error | Error: {str(e)}." ) return [] +@app.task(retry_backoff=5) +def get_pending_notifications_for_user(user_id: int) -> List[db_models.Notifications]: + return get_pending_notifications_for_user_sync(user_id) + + +@app.task +def deliver_pending_notifications(user_id: int): + from core.event_ws import format_notification + from core.socket_manager import manager + import asyncio + + session = SessionLocal() + try: + pending = get_pending_notifications_for_user_sync(user_id) + if not pending: + session.close() + return {"detail": "No habian notificaciones a enviar"} + loop = asyncio.get_event_loop() + except RuntimeError: + loop = asyncio.new_event_loop() + asyncio.set_event_loop(loop) + + async def _deliver(): + nonlocal pending, session + for n in pending: + notification_to_send = format_notification( + notification_type=n.type, message=n.payload + ) + await manager.send_to_user(user_id=user_id, message=notification_to_send) + n.status = db_models.Notify_State.ENVIADO + session.commit() + session.close() + logger.info(f"[deliver_pending_notifications] Notifications were updated") + + return loop.run_until_complete(_deliver()) + + @app.task def save_notification_in_db(message, user_id: int): session = SessionLocal() @@ -52,8 +90,6 @@ def save_notification_in_db(message, user_id: int): message_decoded = json.loads(message)["payload"] notice_payload = schemas.NotificationPayload(**message_decoded) - notice_payload = schemas.NotificationPayload(**message_decoded) - new_notice = db_models.Notifications( user_id=user_id, type=notice_payload.notification_type,