Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 7 additions & 14 deletions core/socket_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"}
46 changes: 41 additions & 5 deletions tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -29,31 +28,68 @@ 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()
try:
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,
Expand Down
Loading